如何在箱线图上绘制加权均值? [英] How to plot weighted means on a boxplot?

查看:234
本文介绍了如何在箱线图上绘制加权均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找解决方案并摆弄手指后,我正在寻求帮助,以在箱线图上显示加权均值(我也尝试将其交叉发布到ggplot2邮件列表中。)

After searching for a solution and fiddling, I am reaching out for help in my attempt to display weighted means on a boxplot (I have tried to cross-post this to the ggplot2 mailing list as well).

我在下面提供一个玩具示例。

I provide a toy example below.

#data

value <- c(5, 7, 8, 6, 7, 9, 10, 6, 7, 10)
category <- c("one", "one", "one", "two", "two", "two",
              "three", "three", "three","three")
weight <- c(1, 1.2, 2, 3, 2.2, 2.5, 1.8, 1.9, 2.2, 1.5)
df <- data.frame(value, category, weight)

#unweighted means by category
ddply(df, .(category), summarize, mean=round(mean(value, na.rm=TRUE), 2))

  category mean
1      one 6.67
2    three 8.25
3      two 7.33

#weighted means by category
ddply(df, .(category), summarize, 
          wmean=round(wtd.mean(value, weight, na.rm=TRUE), 2))

  category wmean
1      one  7.00
2    three  8.08
3      two  7.26

#unweighted means added to boxplot (which works fine)
ggplot(df, aes(x = category, y = value, weight = weight)) + 
   geom_boxplot(width=0.6,  colour = I("#3366FF")) + 
   stat_summary( fun.y ="mean", geom ="point", shape = 23, 
                 size = 3, fill ="white") 

我的问题是,如何在箱线图上显示加权均值

My question is, how can I display weighted means on the boxplot instead of unweighted means?

推荐答案

您可以将加权均值另存为新数据框,然后使用它绘制 geom_point()。参数 inherit.aes = FALSE 将确保在不继承 ggplot()调用中提供的信息的情况下绘制点。 p>

You can save weighted means as new data frame and then use it to plot geom_point(). Argument inherit.aes=FALSE will ensure that points are plotted without inheriting information provided in ggplot() call.

library(Hmisc)
library(plyr)
library(ggplot2)
df.wm<-ddply(df, .(category), summarize, 
             wmean=round(wtd.mean(value, weight, na.rm=TRUE), 2))

ggplot(df, aes(x = category, y = value, weight = weight)) + 
  geom_boxplot(width=0.6,  colour = I("#3366FF")) + 
  geom_point(data=df.wm,aes(x=category,y=wmean),shape = 23, 
             size = 3, fill ="white",inherit.aes=FALSE)

这篇关于如何在箱线图上绘制加权均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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