如何使用geom_boxplot绘制平均值而不是中位数? [英] How do I plot the mean instead of the median with geom_boxplot?
问题描述
出于某种愚蠢的原因,我需要创建一个箱线图,其中的中线是平均值,而不是中位数.我已经检查了stackoverflow,并发现了添加平均行的示例,但并非完全符合我的需要.我尝试使用stat_smooth但没有骰子.有什么想法吗?
for some inane reason, I need to create a boxplot where the middle line is the mean instead of the median. I checked stackoverflow already and found examples of adding a mean line, but not exactly what I need. I tried using stat_smooth but no dice. Any ideas?
下面的代码:
dust <- c(4.5, 3.7, 5, 5.2, 8.5, 6.6, 4.7, 5, 5.7, 4.3, 2.3, 7.6, 5.2,
6, 8.7, 7.5, 7.7, 11, 9, 6.5, 8.7, 5, 2.2, 7.5, 7.5, 3.5)
wind <- c("Present", "Absent", "Absent", "Absent", "Absent", "Absent",
"Absent", "Absent", "Absent", "Present", "Absent", "Absent",
"Present", "Present", "Absent", "Absent", "Absent", "Absent",
"Absent", "Present", "Absent", "Present", "Absent", "Absent",
"Absent", "Present")
df <- data.frame(dust,wind)
plot <- ggplot(data=df,aes(x=wind,y=dust))+geom_boxplot()+stat_smooth()
plot
推荐答案
有几种方法可以做到这一点:
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
如您所见,上面的方法绘制得很好,但是当您评估代码时,它会输出一些警告消息,因为实际上并不期望fatten
取NULL
值.
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.
这篇关于如何使用geom_boxplot绘制平均值而不是中位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!