Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to show mean value in ggplot box plot

Tags:

r

ggplot2

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")
like image 255
user1471980 Avatar asked Dec 12 '22 22:12

user1471980


2 Answers

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

like image 112
KLDavenport Avatar answered Jan 05 '23 11:01

KLDavenport


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

like image 35
user1317221_G Avatar answered Jan 05 '23 12:01

user1317221_G