[英]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 的標准箱線圖。 填充是材料:
以下是如何使用框和須線的自定義參數創建箱線圖。 這是@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.