Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overdraw mean points in grouped boxplot with ggplot2

Tags:

r

ggplot2

I have a grouped boxplot with ggplot2 like this

p <- qplot(factor(cyl), mpg, data=mtcars, geom="boxplot", fill=factor(gear)) 

and I want to overdraw the mean points. I've tried this

p+ stat_summary(fun.y=mean, colour="red", geom="point")

but, I don't get the mean points over the boxes of each group. enter image description here

What should I do to get this?

like image 634
Alfredo Sánchez Avatar asked Aug 30 '13 12:08

Alfredo Sánchez


1 Answers

You should set position to position_dodge() and width to 0.75 - so points will be placed in the same way as boxplots.

qplot(factor(cyl), mpg, data=mtcars, geom="boxplot", fill=factor(gear)) +
  stat_summary(fun.y=mean, colour="red", 
             geom="point",position=position_dodge(width=0.75))
like image 161
Didzis Elferts Avatar answered Oct 07 '22 02:10

Didzis Elferts