Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: horizontal position of stat_summary with geom_boxplot

Tags:

r

ggplot2

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

enter image description here

like image 243
MYaseen208 Avatar asked Jun 11 '16 13:06

MYaseen208


1 Answers

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()
like image 66
Boris Leroy Avatar answered Nov 04 '22 17:11

Boris Leroy