且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用geom_boxplot绘制平均值而不是中位数?

更新时间:2023-11-23 23:18:16

有几种方法可以做到这一点:

There are a few ways to do this:

最简单的方法是简单地致电:

The easiest is to simply call:

plot <- ggplot(data = df, aes(y = dust, x = wind)) + 
        geom_boxplot(aes(middle = mean(dust))

2.使用fatten = NULL

您还可以利用geom_boxplot()中的fatten参数.这控制中线的粗细.如果将其设置为NULL,则不会绘制中线,并且可以使用stat_summary为平均值插入一条线.

2. Using fatten = NULL

You can also take advantage of the fatten parameter in geom_boxplot(). This controls the thickness of the median line. If we set it to NULL, then it will not plot a median line, and we can insert a line for the mean using stat_summary.

plot <- ggplot(data = df, aes(y = dust, x = wind)) + 
  geom_boxplot(fatten = NULL) +
  stat_summary(fun.y = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
               width = 0.75, size = 1, linetype = "solid")
print(plot)

使用fatten = NULL

如您所见,上面的方法绘制得很好,但是当您评估代码时,它会输出一些警告消息,因为实际上并不期望fattenNULL值.

As you can see, the above method plots just fine, but when you evaluate the code it will output some warning messages because fatten is not really expected to take a NULL value.

好处是该方法可能更灵活一些,因为我们本质上是在擦除"中间线并添加所需的任何内容.例如,我们还可以选择保留中位数,并将平均值添加为虚线.

The upside is that this method is possibly a bit more flexible, as we are essentially "erasing" the median line and adding in whatever we want. For example, we could also choose to keep the median, and add the mean as a dashed line.