ggplot2:stat_summary与geom_boxplot的水平位置

时间:2016-06-11 13:33:40

标签: r ggplot2 boxplot

我想使用ggplot2注释每个箱图的平均值。但是,我无法弄清楚如何在各自的方框内水平居中标记手段的符号(见下图)。

以下是MWE供参考:

library(ggplot2)
ggplot(data=mpg, mapping=aes(x=class, y=hwy)) + 
  geom_boxplot(aes(color = drv), outlier.shape = NA) +
  stat_summary(fun.y = mean, geom = "point", size=2, aes(shape = drv, color = drv)) + 
  theme_bw()

enter image description here

1 个答案:

答案 0 :(得分:4)

尝试使用position_dodge()

ggplot(data=mpg, mapping=aes(x=class, y=hwy)) + 
       geom_boxplot(aes(color = drv), outlier.shape = NA) +
       stat_summary(fun.y = mean, geom = "point", size=2, aes(shape = drv, color = drv), 
                    position = position_dodge(width = .75)) + 
       theme_bw()