4

I need to be able to show the mean value in ggplot box plot. Below works for a point but I need the white dashed lines? Any body help?

x

Team     Value
A        10
B        5
C        29
D        35
ggplot(aes(x = Team , y = Value), data = x) 
+ geom_boxplot (aes(fill=Team), alpha=.25, width=0.5, position = position_dodge(width = .9)) 
+ stat_summary(fun.y=mean, colour="red", geom="point")
user1471980
  • 10,127
  • 48
  • 136
  • 235
  • 1
    If you made a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that demonstrates your question / problem, we would find it easier to answer. – Andrie Sep 06 '12 at 21:04

2 Answers2

12

Here's my way of adding mean to boxplots:

ggplot(RQA, aes(x = Type, y = engagementPercent)) + 
geom_boxplot(aes(fill = Type),alpha = .6,size = 1) + 
scale_fill_brewer(palette = "Set2") + 
stat_summary(fun.y = "mean", geom = "text", label="----", size= 10, color= "white") +
ggtitle("Participation distribution by type") + 
theme(axis.title.y=element_blank()) + theme(axis.title.x=element_blank()) 

enter image description here

ggplot(df, aes(x = Type, y = scorepercent)) + 
geom_boxplot(aes(fill = Type),alpha = .6,size = 1) + 
scale_fill_brewer(palette = "Set2") + 
stat_summary(fun.y = "mean", geom = "point", shape= 23, size= 3, fill= "white") +
ggtitle("score distribution by type") + 
theme(axis.title.y=element_blank()) + theme(axis.title.x=element_blank()) 

enter image description here

I would caution against using text to this and do geom_line instead as text is offset slightly and gives the wrong portrayal of the mean.

Hey user1471980, I think people are more inclined to help if you have a unique user name but then again you have a lot of points :)

BrodieG
  • 51,669
  • 9
  • 93
  • 146
KLDavenport
  • 659
  • 8
  • 24
1

this is a hack but does this help:

Value<-c(1,2,3,4,5,6)
Team<-c("a","a","a","b","b","b")
x<-data.frame(Team,Value) #note means for a=2, mean for b=5


ggplot(aes(x = Team , y = Value), data = x) + geom_boxplot (aes(fill=Team), alpha=.25, width=0.5, position = position_dodge(width = .9)) + 
annotate(geom="text", x=1, y=2, label="----", colour="white", size=7, fontface="bold", angle=0) + 
annotate(geom="text", x=2, y=5, label="----", colour="white", size=7, fontface="bold", angle=0)

enter image description here

user1317221_G
  • 15,087
  • 3
  • 52
  • 78