简体   繁体   English

Ggplot boxplot 按组,更改显示的汇总统计信息

[英]Ggplot boxplot by group, change summary statistics shown

I want to change the summary statistics shown in the following boxplot:我想更改以下箱线图中显示的汇总统计信息:

箱形图

I have created the boxplot as follows:我创建的箱线图如下:

ggplot(as.data.frame(beta2), aes(y=var1,x=as.factor(Year))) + 
  geom_boxplot(outlier.shape = NA)+
  ylab(expression(beta[1]))+
  xlab("\nYear")+
  theme_bw()

The default is for the box is the first and third quantile.框的默认值是第一个和第三个分位数。 I want the box to show the 2.5% and 97.5% quantiles.我希望该框显示 2.5% 和 97.5% 的分位数。 I know one can easily change what is shown when one boxplot is visualised by adding the following to geom_boxplot:我知道可以通过将以下内容添加到 geom_boxplot 来轻松更改当一个箱线图可视化时显示的内容:

aes(
    ymin= min(var1),
    lower = quantile(var1,0.025),
    middle = mean(var1),
    upper = quantile(var1,0.975),
    ymax=max(var1))

However, this does not work for when boxplots are generated by group.但是,这不适用于按组生成箱线图的情况。 Any idea how one would do this?知道如何做到这一点吗? You can use the Iris data set:您可以使用 Iris 数据集:

ggplot(iris, aes(y=Sepal.Length,x=Species)) + 
  geom_boxplot(outlier.shape = NA)

EDIT:编辑:

The answer accepted does work.接受的答案确实有效。 My data-frame is really big and as such the method provided takes a bit of time.我的数据框非常大,因此提供的方法需要一些时间。 I found another solution here: SOLUTION that works for large datasets and my specific need.我在这里找到了另一个解决方案:适用于大型数据集和我的特定需求的解决方案

This could be achieved via stat_summary by setting geom="boxplot" .这可以通过stat_summary设置geom="boxplot" stat_summary geom="boxplot"来实现。 and passing to fun.data a function which returns a data frame with the summary statistics you want to display as ymin , lower , ... in your boxplot:并传递给fun.data一个函数,该函数返回一个数据框,其中包含要在箱线图中显示为yminlower 、... 的汇总统计信息:

library(ggplot2)

ggplot(iris, aes(x = Species, y = Sepal.Length)) +
  stat_summary(geom = "boxplot", fun.data = function(x) {
    data.frame(
      ymin = min(x),
      lower = quantile(x, 0.025),
      middle = mean(x),
      upper = quantile(x, 0.975),
      ymax = max(x) 
    )}, outlier.shape = NA)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM