繁体   English   中英

ggplot 箱线图,按组显示均值和置信区间

[英]ggplot boxplot with mean and confidence interval by group

我想用均值而不是中值制作箱线图。 此外,我希望这条线停在 5%(下)结束 95%(上)分位数。 这里的代码;

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")

我想将geom更改为errorbar ,但这不起作用。 我试过middle = mean(Mean_Gap) ,但这也不起作用。 我试过ymin = quantile(y,0.05) ,但没有任何改变。 谁能帮我?

使用 ggplot 的标准箱线图。 填充是材料: 使用 ggplot 的标准箱线图。填充是材料

以下是如何使用框和须线的自定义参数创建箱线图。 这是@lukeA 在stackoverflow.com/a/34529614/6288065 中显示的解决方案,但这个解决方案还将向您展示如何按组制作多个盒子。

名为“ToothGrowth”的 R 内置数据集与您的数据结构相似,因此我将以此为例。 我们将绘制每个维生素 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))

标准箱线图与基于均值的箱线图

在上图中,左侧的箱线图是具有规则中线和规则最小/最大须线的标准箱线图。
右侧的箱线图使用平均中线和 5%/95% 分位数须。

暂无
暂无

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

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