[英]ggplot boxplot with mean and confidence interval by group
I'd like to make a boxplot with mean instead of median.我想用均值而不是中值制作箱线图。 Moreover, I would like the line to stop at 5% (lower) end 95% (upper) quantile.
此外,我希望这条线停在 5%(下)结束 95%(上)分位数。 Here the code;
这里的代码;
ggplot(data, aes(x=Cement, y=Mean_Gap, fill=Material)) +
geom_boxplot(fatten = NULL,aes(fill=Material), position=position_dodge(.9)) +
xlab("Cement") + ylab("Mean cement layer thickness") +
stat_summary(fun=mean, geom="point", aes(group=Material), position=position_dodge(.9),color="black")
I'd like to change geom
to errorbar
, but this doesn't work.我想将
geom
更改为errorbar
,但这不起作用。 I tried middle = mean(Mean_Gap)
, but this doesn't work either.我试过
middle = mean(Mean_Gap)
,但这也不起作用。 I tried ymin = quantile(y,0.05)
, but nothing was changing.我试过
ymin = quantile(y,0.05)
,但没有任何改变。 Can anyone help me?谁能帮我?
The standard boxplot using ggplot.使用 ggplot 的标准箱线图。 fill is Material:
填充是材料:
Here is how you can create the boxplot using custom parameters for the box and whiskers.以下是如何使用框和须线的自定义参数创建箱线图。 It's the solution shown by @lukeA in stackoverflow.com/a/34529614/6288065 , but this one will also show you how to make several boxes by groups.
这是@lukeA 在stackoverflow.com/a/34529614/6288065 中显示的解决方案,但这个解决方案还将向您展示如何按组制作多个盒子。
The R built-in data set called "ToothGrowth" is similar to your data structure so I will use that as an example.名为“ToothGrowth”的 R 内置数据集与您的数据结构相似,因此我将以此为例。 We will plot the length of tooth growth (
len
) for each vitamin C supplement group ( supp
), separated/filled by dosage level ( dose
).我们将绘制每个维生素 C 补充剂组 (
supp
) 的牙齿生长长度 ( len
),按剂量水平 ( dose
) 分隔/填充。
# "ToothGrowth" at a glance
head(ToothGrowth)
# len supp dose
#1 4.2 VC 0.5
#2 11.5 VC 0.5
#3 7.3 VC 0.5
#4 5.8 VC 0.5
#5 6.4 VC 0.5
#6 10.0 VC 0.5
library(dplyr)
# recreate the data structure with specific "len" coordinates to plot for each group
df <- ToothGrowth %>%
group_by(supp, dose) %>%
summarise(
y0 = quantile(len, 0.05),
y25 = quantile(len, 0.25),
y50 = mean(len),
y75 = quantile(len, 0.75),
y100 = quantile(len, 0.95))
df
## A tibble: 6 x 7
## Groups: supp [2]
# supp dose y0 y25 y50 y75 y100
# <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 OJ 0.5 8.74 9.7 13.2 16.2 19.7
#2 OJ 1 16.8 20.3 22.7 25.6 26.9
#3 OJ 2 22.7 24.6 26.1 27.1 30.2
#4 VC 0.5 4.65 5.95 7.98 10.9 11.4
#5 VC 1 14.0 15.3 16.8 17.3 20.8
#6 VC 2 19.8 23.4 26.1 28.8 33.3
# boxplot using the mean for the middle and 95% quantiles for the whiskers
ggplot(df, aes(supp, fill = as.factor(dose))) +
geom_boxplot(
aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
stat = "identity"
) +
labs(y = "len", title = "Boxplot with Mean Middle Line") +
theme(plot.title = element_text(hjust = 0.5))
In the figure above, the boxplot on the left is the standard boxplot with regular median line and regular min/max whiskers.在上图中,左侧的箱线图是具有规则中线和规则最小/最大须线的标准箱线图。
The boxplot on the right uses the mean middle line and 5%/95% quantile whiskers.右侧的箱线图使用平均中线和 5%/95% 分位数须。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.