4

我需要能够在 ggplot 箱线图中显示平均值。下面适用于一个点,但我需要白色虚线?任何身体帮助?

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")
4

2 回答 2

12

这是我为箱线图添加均值的方法:

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

在此处输入图像描述

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

在此处输入图像描述

我会警告不要对此使用文本并改为使用 geom_line,因为文本会稍微偏移并给出错误的均值描述。

嘿,user1471980,我认为如果你有一个独特的用户名,人们会更愿意提供帮助,但是你又有很多要点 :)

于 2013-05-30T22:02:20.867 回答
1

这是一个技巧,但这有帮助吗:

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)

在此处输入图像描述

于 2012-09-06T21:24:54.200 回答