小编use*_*662的帖子

如何在箱线图上绘制加权均值?

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

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

#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 …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 boxplot

3
推荐指数
1
解决办法
7265
查看次数

标签 统计

boxplot ×1

ggplot2 ×1

r ×1