在ggplot箱图中使用平均值而不是中位数 [英] Use mean in ggplot boxplots instead of median

查看:579
本文介绍了在ggplot箱图中使用平均值而不是中位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在ggplot箱图中使用平均值而不是中位数? 我问的原因是,在我的数据中,中位数= 0.0,平均值= 0.40,我对平均值感兴趣.

Is it possible to use the mean in a ggplot boxplot instead of the median? Reason I ask is that in my data the median = 0.0 and mean = 0.40 and I am interested in the mean.

推荐答案

从帮助?geom_boxplot:

library(ggplot2)
# It's possible to draw a boxplot with your own computations if you
# use stat = "identity":
y <- rnorm(100)
df <- data.frame(
  x = 1,
  y0 = min(y),
  y25 = quantile(y, 0.25),
  y50 = median(y),   # <=== replace by mean
  y75 = quantile(y, 0.75),
  y100 = max(y)
)
ggplot(df, aes(x)) +
  geom_boxplot(
    aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
    stat = "identity"
  )

因此,您可以预先计算框值,使用stat="identity"并将median替换为mean.

So you could pre-compute the box values, use stat="identity" and replace median by mean.

这篇关于在ggplot箱图中使用平均值而不是中位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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