R:ggplot给箱图添加散点和连线 您所在的位置:网站首页 马云创新的事例 R:ggplot给箱图添加散点和连线

R:ggplot给箱图添加散点和连线

#R:ggplot给箱图添加散点和连线| 来源: 网络整理| 查看: 265

导读

在一幅图里展示箱、散点和折线。

一、模拟输入数据 set.seed(1995) # 随机种子 data=matrix(abs(round(rnorm(20, mean=20, sd=5))), 5, 4) # 随机正整数,5行,4列 colnames(data)=paste("指标", 1:4, sep=".") # 列名 rownames(data)=paste("样品", 1:5, sep=".") # 行名 data # 查看数据,如下: 指标.1 指标.2 指标.3 指标.4 样品.1 25 18 20 20 样品.2 18 20 19 8 样品.3 21 20 21 13 样品.4 22 19 25 7 样品.5 28 28 13 20 二、调整格式 library(reshape) input=melt(data) input # 结果如下: Var1 Var2 value 1 样品.1 指标.1 25 2 样品.2 指标.1 18 3 样品.3 指标.1 21 4 样品.4 指标.1 22 5 样品.5 指标.1 28 6 样品.1 指标.2 18 7 样品.2 指标.2 20 8 样品.3 指标.2 20 9 样品.4 指标.2 19 10 样品.5 指标.2 28 11 样品.1 指标.3 20 12 样品.2 指标.3 19 13 样品.3 指标.3 21 14 样品.4 指标.3 25 15 样品.5 指标.3 13 16 样品.1 指标.4 20 17 样品.2 指标.4 8 18 样品.3 指标.4 13 19 样品.4 指标.4 7 20 样品.5 指标.4 20 三、ggplot画箱图

geom_boxplot():箱图

ggplot(input, aes(x=Var2, y=value, fill=Var2)) + geom_boxplot() + labs(x="指标(x轴)", y="值", fill="指标(图例)") + theme(panel.grid=element_blank(), panel.background=element_rect(fill='transparent', color='black')) ggplot(input, aes(x=Var2, y=value, color=Var2)) + geom_boxplot() + labs(x="指标(x轴)", y="值", color="指标(图例)") + theme(panel.grid=element_blank(), panel.background=element_rect(fill='transparent', color='black')) 四、添加散点

geom_point():散点图

ggplot(input, aes(x=Var2, y=value, fill=Var2)) + geom_boxplot() + labs(x="指标(x轴)", y="值", color="指标(图例)") + theme(panel.grid=element_blank(), panel.background=element_rect(fill='transparent', color='black')) + geom_point(pch=21, fill="black", color="deepskyblue") ggplot(input, aes(x=Var2, y=value, color=Var2)) + geom_boxplot() + labs(x="指标(x轴)", y="值", color="指标(图例)") + theme(panel.grid=element_blank(), panel.background=element_rect(fill='transparent', color='black')) + geom_point() ggplot(input, aes(x=Var2, y=value)) + geom_boxplot(aes(color=Var2)) + labs(x="指标(x轴)", y="值", color="指标(图例)") + theme(panel.grid=element_blank(), panel.background=element_rect(fill='transparent', color='black')) + geom_dotplot(binaxis='y', stackdir='center', dotsize=0.7, color="white", aes(fill=Var2)) + theme(legend.title=element_blank()) 五、添加连线

geom_line():折线图

ggplot(input, aes(x=Var2, y=value)) + geom_boxplot(aes(color=Var2)) + labs(x="指标(x轴)", y="值", color="指标(图例)") + theme(panel.grid=element_blank(), panel.background=element_rect(fill='transparent', color='black')) + geom_dotplot(binaxis='y', stackdir='center', dotsize=0.7, color="white", aes(fill=Var2)) + theme(legend.title=element_blank()) + geom_line(aes(group=Var1), linetype="dashed", col="skyblue")

实战1:

## length length = read.table("length.txt", header=T, sep="\t") ## wilcox uhgg = length[length$group=="UHGG",]$length cgr2 = length[length$group=="CGR2",]$length mean(uhgg) mean(cgr2) wilcox.test(uhgg, cgr2, paired = TRUE) wilcox.test(uhgg, cgr2, paired = TRUE)$p.value ## Title = "Wilcoxon p value < 2e-16" p = ggplot(length, aes(x = group, y = length, color=group)) + labs(x="", y="Genome length", title=Title) + theme_classic() + geom_boxplot(width=0.5, outlier.colour = NA) + # 不显示离群点 geom_jitter(width = 0.1) + # 添加散点 geom_line(aes(group=pair), color="gray", alpha=0.4) + scale_color_manual(values = c('orangered3','deepskyblue3')) + theme(panel.grid = element_line(colour = 'white')) + theme(legend.position = "none") + theme(text = element_text(family="serif")) + theme(axis.title = element_text(size = 15), axis.text = element_text(size = 15), axis.line = element_line(size = 1), axis.ticks = element_line(size = 1), title = element_text(size = 12)) ggsave(p, filename="length.pdf")

实战2:

ggplot(data, aes(x=Group, y=Ratio)) + geom_boxplot(outlier.colour = NA, width=0.8) + theme_classic() + geom_jitter(width = 0.2, aes(color=Family)) + scale_color_manual(values = colors) + labs(x="", y="Relvative ratio") + theme(axis.title = element_text(size = 20), axis.text = element_text(size = 17), axis.line = element_line(size = 1), axis.ticks = element_line(size = 1), title = element_text(size = 15)) + theme(legend.title=element_text(face="bold"), legend.text=element_text(size=rel(1.1))) + scale_y_continuous(labels = scales :: percent)

一组不错的参数:

ggplot(alpha, aes(x=source, y=alpha, color=source)) + geom_boxplot(width = 0.5, size = 1, outlier.colour = NA) + # size粗细 width宽度 position_dodge间距 geom_jitter(width = 0.1, size = 3) + # width点距 size点大小 theme_classic() + scale_color_manual(values = c("Colon"="#00BA38", "Stool"="#F8766D", "Ileum"="#619CFF")) + theme(legend.text=element_text(size=15), legend.title=element_text(face='bold', size=20)) + labs(x="", y="Shannon index", color="Source") + theme(title = element_text(size = 15, face="bold"), legend.position = "none") + scale_y_continuous(expand = c(0, 0), limits = c(0, 5)) + theme(axis.title = element_text(size = 25), axis.text.y = element_text(size = 18), axis.text.x = element_text(size = 20), axis.line = element_line(size = 1), axis.ticks = element_line(size = 1))

参数汇总

geom_boxplot(width = 0.5, size = 1, outlier.colour = NA) # 箱宽,线条粗细,不显示离群点 geom_jitter(width = 0.1, size = 3) # 抖动点,点距,点大小

参考: https://www.cnblogs.com/ljhdo/p/4921588.html 添加两组比较显著性结果 R语言ggboxplot-一文掌握箱线图绘制所有细节 配对样本基因表达趋势:优化前后的散点连线图+拼图绘制 How to Connect Data Points on Boxplot with Lines?

\color{green}{😀😀原创文章,码字不易,转载请注明出处😀😀}



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有