默认的箱型图如下所示:
但是有时,我需要将箱子中默认的中位数那条线,改为平均值。下面代码数据来源于上一篇博客:配对样本检验及绘图 - 简书 https://www.jianshu.com/p/e5a24590b5f6
> mean(dt_N)
[1] 6.370483
> median(dt_N)
[1] 6.389692
> mean(dt_T)
[1] 6.347574
> median(dt_T)
[1] 6.328644
我们可以发现在平均值这块,N组为6.37, T组为6.34, 而中位数这块,N组为6.38,T组为6.32。
- 箱型图中的横线为平均值:
library(dplyr)
library(ggplot2)
library(ggpubr)
theme_set(theme_pubclean())
plot <- ggplot(data = dt, aes(x = group, y = ERBB2)) +
geom_boxplot(fatten = NULL,aes(colour = group )) +
scale_color_manual(values=c("#137F5F", "#ED553B"))+
aes(colour = group)+
stat_summary(fun = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
width = 0.75, size = 1, linetype = "solid")+
geom_point(aes(colour = factor(group)), size=1, alpha=0.5) +
geom_line(aes(group=num), colour="gray50", linetype="11") +
theme_classic()
print(plot)
pdf(file = 'pair1.pdf', height = 4, width = 4)
print(plot)
dev.off()
- 箱型图中的横线为中位数:
library(dplyr)
library(ggplot2)
library(ggpubr)
theme_set(theme_pubclean())
plot <- ggplot(data = dt, aes(x = group, y = ERBB2)) +
geom_boxplot(fatten = NULL,aes(colour = group )) +
scale_color_manual(values=c("#137F5F", "#ED553B"))+
aes(colour = group)+
stat_summary(fun = median, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
width = 0.75, size = 1, linetype = "solid")+
geom_point(aes(colour = factor(group)), size=1, alpha=0.5) +
geom_line(aes(group=num), colour="gray50", linetype="11") +
theme_classic()
print(plot)
pdf(file = 'pair2.pdf', height = 4, width = 4)
print(plot)
dev.off()
网友评论