简体   繁体   中英

ggplot2: horizontal position of stat_summary with geom_boxplot

I want to annotate mean of each boxplot using ggplot2 . However, I could not figure out how to horizontally center the symbols marking the means within their respective boxes (see image below).

MWE is below for reference:

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

在此处输入图片说明

Try with 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM