我有以下数据
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)
生成此 figure .
除了 days
之外,我还想在此图中使用另一个 geom_col
添加变量 mean3y
,例如
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"
,但我还是无法获得预期的结果,如 figure 所示其中两个变量重叠。
请问有什么办法可以实现吗? 非常感谢。
最佳答案
位置闪避仅在单层中起作用,在多层之间不起作用。您可以通过手动微调它们或以可以躲避的方式格式化数据来解决问题。以下代码中的示例。
很难将您的数据复制到我的 R session 中,而且您的代码比演示该问题所需的更详细,因此我将两者都保持在最低限度。
library(ggplot2)
df <- data.frame(
x = c("A", "B"),
y = c(10, 15),
z = c(12, 9)
)
# Example of nudging
# Choose width and nudge values manually to fit your data
ggplot(df, aes(x, y)) +
geom_col(aes(fill = "first col"),
width = 0.45,
position = position_nudge(x = -0.225)) +
geom_col(aes(y = z, fill = "second_col"),
width = 0.45,
position = position_nudge(x = 0.225))
library(dplyr)
#> Warning: package 'dplyr' was built under R version 3.6.3
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
# Example of dodging + data formatting
ggplot(mapping = aes(x, y)) +
geom_col(data = rbind(mutate(df, a = "first_col"),
mutate(df, y = z, a = "second_col")),
aes(fill = a),
position = "dodge")
由 reprex package 创建于 2020-04-16 (v0.3.0)
关于r - 如何使用 geom_col 在同一个 ggplot 中并排绘制两个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61260050/