在ggplot框图中显示平均值

时间:2012-09-06 20:37:03

标签: r ggplot2

我需要能够在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")

2 个答案:

答案 0 :(得分:10)

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

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

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

嘿user1471980,我认为如果你有一个独特的用户名,人们更倾向于帮助,但你又有很多分数:)

答案 1 :(得分: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)

enter image description here