我想使用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()

r - ggplot2:stat_summary与geom_boxplot的水平位置-LMLPHP

最佳答案

尝试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()

关于r - ggplot2:stat_summary与geom_boxplot的水平位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37764348/

10-11 13:48