Boxplot显示平均值 [英] Boxplot show the value of mean

查看:1696
本文介绍了Boxplot显示平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个boxplot中,我们可以看到平均值,但是我们怎样才能得到每个盒子图的每个均值的图上的数值?

  ggplot(data = PlantGrowth,aes(x = group,y = weight,fill = group))+ geom_boxplot()+ 
stat_summary(fun.y = mean,color =darkred,geom =point,
shape = 18,size = 3,show_guide = FALSE)


解决方案

首先,您可以使用 aggregate 来计算组合方式:

<$ p $


$ p




$ >此数据集可与 geom_text 一起使用:
$ b

  library(ggplot2) 
ggplot(data = PlantGrowth,aes(x = group,y = weight,fill = group))+ geom_boxplot()+
stat_summary(fun.y = mean,color =darkred,geom = point,
shape = 18,size = 3,show_guide = FALSE)+
geom_text(data = means,aes(label = weight,y = weight + 0.08))

这里, + 0.08 是用于将标签放置在表示平均值的点上方。







另一个没有 ggplot2 :

 表示<  - 集合(重量〜组,PlantGrowth,平均值)

boxplot( (1:3,意味着$ weight + 0.08,labels =意味着$ weight)
点(1:3,意味着$ weight,col =red)
文本(1:3,weight = group,PlantGrowth) $ b


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)

解决方案

First, you can calculate the group means with aggregate:

means <- aggregate(weight ~  group, PlantGrowth, mean)

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.y=mean, colour="darkred", geom="point", 
               shape=18, size=3,show_guide = FALSE) + 
  geom_text(data = means, aes(label = weight, y = weight + 0.08))

Here, + 0.08 is used to place the label above the point representing the mean.


An alternative version without 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)

这篇关于Boxplot显示平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆