Reputation: 735
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.
What should I do to get this?
Upvotes: 8
Views: 2118
Reputation: 98499
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))
Upvotes: 12