【问题标题】:Use mean in ggplot boxplots instead of median在 ggplot 箱线图中使用均值而不是中位数
【发布时间】:2015-12-30 12:26:01
【问题描述】:

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

【问题讨论】:

标签: r ggplot2 mean median


【解决方案1】:

来自?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

【讨论】:

    猜你喜欢
    • 2018-08-15
    • 2012-08-31
    • 2023-04-03
    • 2021-07-01
    • 2018-07-15
    • 1970-01-01
    • 2017-08-17
    • 2019-04-02
    • 2018-06-17
    相关资源
    最近更新 更多