箱线图显示平均值 [英] Boxplot show the value of mean
本文介绍了箱线图显示平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这个箱线图中,我们可以看到均值,但我们如何才能在图中获得每个箱线图的每个均值的数值?
In this boxplot we can see the mean but how can we have also the number value on the plot for every mean of every box plot?
ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
stat_summary(fun.y=mean, colour="darkred", geom="point",
shape=18, size=3,show_guide = FALSE)
推荐答案
首先,你可以用aggregate
来计算组均值:
First, you can calculate the group means with aggregate
:
means <- aggregate(weight ~ group, PlantGrowth, mean)
这个数据集可以与geom_text
一起使用:
This dataset can be used with geom_text
:
library(ggplot2)
ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
stat_summary(fun=mean, colour="darkred", geom="point",
shape=18, size=3, show.legend=FALSE) +
geom_text(data = means, aes(label = weight, y = weight + 0.08))
这里,+ 0.08
用于将标签放置在表示均值的点上方.
Here, + 0.08
is used to place the label above the point representing the mean.
没有ggplot2
的替代版本:
means <- aggregate(weight ~ group, PlantGrowth, mean)
boxplot(weight ~ group, PlantGrowth)
points(1:3, means$weight, col = "red")
text(1:3, means$weight + 0.08, labels = means$weight)
这篇关于箱线图显示平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文