Alfredo Sánchez
Alfredo Sánchez

Reputation: 735

Overdraw mean points in grouped boxplot with 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?

Upvotes: 8

Views: 2118

Answers (1)

Didzis Elferts
Didzis Elferts

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

Related Questions