我有以下数据
structure(list(id = 1:7, date = c(2019L, 2019L, 2019L, 2019L,
2019L, 2019L, 2019L), station = structure(1:7, .Label = c("41B004",
"41B011", "41MEU1", "41N043", "41R001", "41R012", "41WOL1"), class = "factor"),
days = c(6L, 21L, 5L, 9L, 13L, 14L, 3L), mean3y = c(8.33,
21.3, NA, 10, 11.3, 16.3, 3.67), environ = structure(c(3L,
4L, 2L, 1L, 3L, 4L, 3L), .Label = c("Industriel avec influence modérée du trafic",
"Urbain avec faible influence du trafic", "Urbain avec influence modérée du trafic",
"Urbain avec très faible influence du trafic"), class = "factor")), class = "data.frame", row.names = c(NA,
-7L))
使用以下 ggplot 代码绘制
ggplot(data, aes(x = reorder(station, -days),
y = days, fill = environ)) +
geom_col(width = 0.5, colour = "black", size = 0.5) +
guides(fill = guide_legend(ncol = 2)) +
geom_text(aes(label = days),
vjust=-0.3, color="black", size = 3.5) +
geom_hline(aes(yintercept = 25),
linetype = 'dashed', colour = 'red', size = 1) +
labs(x = '', y = bquote("Nombre de jours de dépassement de NET60" ~ O[3] ~ "en 2019")) +
theme_minimal() +
theme(legend.position="bottom", legend.title = element_blank(),
legend.margin=margin(l = -2, unit='line'),
legend.text = element_text(size = 11),
axis.text.y = element_text(size = 12),
axis.title.y = element_text(size = 11),
axis.text.x = element_text(size = 11),
panel.grid.major.x = element_blank()) +
geom_hline(yintercept = 0)
生成这个数字。
我还想在这个图中添加mean3y
除了days
每个 x 值之外的变量geom_col
,例如
p <- ggplot(data, aes(x = reorder(station, -days),
y = days, fill = environ)) +
geom_col(width = 0.5, colour = "black", size = 0.5) +
guides(fill = guide_legend(ncol = 2)) +
geom_text(aes(label = days),
vjust=-0.3, color="black", size = 3.5) +
geom_col(aes(x = reorder(station, -days),
y = mean3y, fill = environ),
inherit.aes = FALSE,
width = 0.5, colour = "black", size = 0.5) +
geom_hline(aes(yintercept = 25),
linetype = 'dashed', colour = 'red', size = 1) +
labs(x = '', y = bquote("Nombre de jours de dépassement de NET60" ~ O[3] ~ "en 2019")) +
theme_minimal() +
theme(legend.position="bottom",
legend.title = element_blank(),
legend.margin=margin(l = -2, unit='line'),
legend.text = element_text(size = 11),
axis.text.y = element_text(size = 12),
axis.title.y = element_text(size = 11),
axis.text.x = element_text(size = 11),
panel.grid.major.x = element_blank()) +
geom_hline(yintercept = 0)
但是,尽管使用了 ,但我无法达到预期的结果,position = "dodge"
如图所示,两个变量都重叠。
请问有没有办法做到这一点?非常感谢。