gpt4 book ai didi

r - ggplot2 标签不会躲避 geom_col 条

转载 作者:行者123 更新时间:2023-12-02 16:26:51 24 4
gpt4 key购买 nike

我已经解决了相关问题,但这些答案并没有解决我的标签没有躲避以匹配 geom_col 条的问题:

数据

x <- structure(
list(capacity = c(0, 0, 0, 2.1, 3.1, 4, 4.6, 5.6, 6,
1.9, 2.3, 3.8),
year = c("FY21", "FY21", "FY21", "FY21", "FY21",
"FY20", "FY20", "FY20", "FY20", "FY19", "FY19", "FY19"),
unified_date = structure(c(18536, 18567, 18597, 18628, 18659,
18567, 18597, 18628, 18659, 18536,
18567, 18597), class = "Date")),
row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"))

代码

ggplot2::ggplot(x, aes(x = unified_date, y = capacity, fill = year)) +
geom_col(position = "dodge") +
geom_text(aes(label = capacity),
position = position_dodge(width = 1),
vjust = -0.5, size = 4)

图表

not-dodged ggplot

我已经尝试将 fill = year 添加到 geom_text aesgroup = year,移动aes 周围的值,position_dodge() 的变体 - 没有。

最佳答案

试试这个:

#Code
ggplot2::ggplot(x, aes(x = factor(unified_date), y = capacity, fill = year)) +
geom_bar(stat='identity',position = "dodge") +
geom_text(aes(label = capacity),
position=position_dodge(width=0.9), size = 4,vjust=-0.5)+
xlab('Date')

输出:

enter image description here

如果您想正确处理月份,请尝试以下操作:

#Code 2
x %>% mutate(Month=format(unified_date,'%b')) %>%
mutate(Month=factor(Month,levels = unique(Month),ordered = T)) %>%
ggplot2::ggplot(aes(x = Month, y = capacity, fill = year)) +
geom_bar(stat='identity',position = "dodge") +
geom_text(aes(label = capacity),
position=position_dodge(width=0.9), size = 4,vjust=-0.5)+
xlab('Date')

输出:

enter image description here

关于r - ggplot2 标签不会躲避 geom_col 条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64284012/

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