我希望我的Crossbars也能躲闪,就像我的箱形图一样,在我的例子中它不起作用,任何人都可以解释我做错了什么或修改我的代码?我以mtcars为例,将结果包含在我的Crossbars不躲闪的图片中。
library(ggplot2)
mtcars$am = factor(mtcars$am)
mtcars$vs = factor(mtcars$vs)
cleanup = theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
legend.key = element_rect(fill = "white"),
text = element_text(size = 10))
p = ggplot(data = mtcars, aes(x = am , y = mpg, colour = vs)) +
geom_boxplot(aes(colour = vs)) +
stat_summary(aes(colour = vs),
fun.data = "mean_cl_normal",
geom = "crossbar",
position = position_dodge(width = 0.90),
width = .2,
col = "red")
p +
cleanup +
xlab("AM") +
ylab("Miles per Gallon") +
scale_colour_manual(name = "VS",
values = c("Light Gray",
"Dark Grey"))
这给了我这张图:
答案 0 :(得分:3)
原因很简单:指定col = "red"
会将aes
映射覆盖为颜色。实际上只有一组用于横杆,因此无需躲闪。
您可以通过映射到group
:
ggplot(mtcars, aes(x = am , y = mpg, colour = vs)) +
#geom_boxplot() +
stat_summary(aes(group = vs),
fun.data = "mean_cl_normal",
geom = "crossbar",
position = position_dodge(width = 0.9),
width = .2,
col = "red")
然而,仅丢弃横杆的色标显然不会产生良好的情节。