首先,您可以使用来计算组均值aggregate
:
means <- aggregate(weight ~ group, PlantGrowth, mean)
该数据集可用于geom_text
:
library(ggplot2)
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) +
geom_text(data = means, aes(label = weight, y = weight + 0.08))
在此,+ 0.08
用于将标签放置在表示平均值的点上方。
没有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)