gpt4 book ai didi

r - 如何整齐地对齐堆叠条形图标签,使条形的每一侧对齐方式不同?

转载 作者:行者123 更新时间:2023-12-05 01:54:57 25 4
gpt4 key购买 nike

我的标签大致与堆叠条形图的每一侧对齐。问题是它们看起来一团糟,因为它们在栏的两边都没有左右对齐。我该如何解决这个问题,让它们看起来很专业?

df3 <- data.frame(
Label = c("Dasher", "Dancer", "Comet", "Cupid", "Prancer", "Blitzen", "Rudolph"),
Amount = c(650.01, 601.01, 340.05, 330.20, 260.01, 250.80, 10.10)
)

# Sort order
level_order <- df3 %>%
arrange(desc(Amount))

ggplot(level_order, aes(fill=fct_inorder(Label), y=Amount, x="")) +
geom_bar(position="stack", stat="identity", width = 0.55) +
scale_fill_brewer(palette = "Blues", direction = -1) +
theme_void() +
geom_text(aes(label = paste0("$", Amount)),
position = position_stack(vjust = 0.5),
hjust = -3.1,
size = 5) +
geom_text(aes(label = Label),
position = position_stack(vjust = 0.5),
hjust = 5,
size = 5) +
theme(legend.position = "none") +
theme(plot.title = element_text(size = 50, hjust = .5, vjust = 0)) +
ggtitle("Food Costs by Reindeer")

enter image description here

最佳答案

hjust 确定文本对齐方式(0 左对齐,1 右对齐)。您的 geom_text 的 x 坐标目前默认为 1,因此更改它会改变文本的位置。

ggplot(level_order, aes(fill=fct_inorder(Label), y=Amount, x="")) + 
geom_bar(position="stack", stat="identity", width = 0.55) +
scale_fill_brewer(palette = "Blues", direction = -1) +
theme_void() +
geom_text(aes(x=0.6, label = paste0("$", Amount)),
position = position_stack(vjust = 0.5),
hjust = 0.5,
size = 5) +
geom_text(aes(x=1.4, label = Label),
position = position_stack(vjust = 0.5),
hjust = 0.5,
size = 5) +
theme(legend.position = "none") +
theme(plot.title = element_text(size = 50, hjust = .5, vjust = 0)) +
ggtitle("Food Costs by Reindeer")

关于r - 如何整齐地对齐堆叠条形图标签,使条形的每一侧对齐方式不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70414552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com