luria的个人博客分享 http://blog.sciencenet.cn/u/luria

博文

R boxplot显著性绘图包ggpubr的使用注意点(一)

已有 313 次阅读 2024-4-15 16:57 |个人分类:BioIT|系统分类:科研笔记

测试数据如下:

demo.xls

格式如下:

 

1. 不使用stat_compare_means

myargs <- commandArgs(TRUE)

 

if (!length(myargs)){

   print("[USAGE] Rscript cptmt_switch.r <demo_table>")

   q()

}

 

library(ggplot2)

library(ggpubr)

switch_table <- read.delim(myargs[1], as.is=TRUE)

ggboxplot(data=switch_table, x="Switch", y="GC", palette="Set2", fill="Switch", ylab="GC content (%)", outlier.size=-1, notch=TRUE, legend.title="") +

ylim(30, 85) +

theme_pubr(base_size=16, border=TRUE) + theme(axis.title.x=element_blank())

ggsave("ggpubr_debug.png", width=10, height=7, dpi=600)

x轴几种分类与label可以对应

 

2. 使用stat_compare_means

再上面的代码中添加两行,如下用红色标记

myargs <- commandArgs(TRUE)

 

if (!length(myargs)){

   print("[USAGE] Rscript cptmt_switch.r <demo_table>")

   q()

}

 

library(ggplot2)

library(ggpubr)

switch_table <- read.delim(myargs[1], as.is=TRUE)

compare <- list(c("A2A", "B2B"), c("A2A", "A2B"), c("A2A", "B2A"),

           c("B2B", "A2B"), c("B2B", "B2A"), c("A2B", "B2A"))

ggboxplot(data=switch_table, x="Switch", y="GC", palette="Set2", fill="Switch", ylab="GC content (%)", outlier.size=-1, notch=TRUE, legend.title="") +

ylim(30, 85) +

theme_pubr(base_size=16, border=TRUE) +

theme(axis.title.x=element_blank()) +

stat_compare_means(comparisons=compare, size=4, tip.length=0)

ggsave("ggpubr_debug.png", width=10, height=7, dpi=600)

 

这时候会出错!与之前不用stat_compare_means相比,推测应该是x轴标记出错

3. 强行换x

如下添加scale_x_discrete行强行换x

myargs <- commandArgs(TRUE)

 

if (!length(myargs)){

   print("[USAGE] Rscript cptmt_switch.r <demo_table>")

   q()

}

 

library(ggplot2)

library(ggpubr)

switch_table <- read.delim(myargs[1], as.is=TRUE)

compare <- list(c("A2A", "B2B"), c("A2A", "A2B"), c("A2A", "B2A"),

           c("B2B", "A2B"), c("B2B", "B2A"), c("A2B", "B2A"))

ggboxplot(data=switch_table, x="Switch", y="GC", palette="Set2", fill="Switch", ylab="GC content (%)", outlier.size=-1, notch=TRUE, legend.title="") +

scale_x_discrete(limits=c("A2A", "B2B", "A2B", "B2A")) +

ylim(30, 85) +

theme_pubr(base_size=16, border=TRUE) +

theme(axis.title.x=element_blank()) +

stat_compare_means(comparisons=compare, size=4, tip.length=0)

ggsave("ggpubr_debug.png", width=10, height=7, dpi=600)

 

可以得到正确的结果,强行换x轴后两样本间显著性线段的位置也随之变化,如下图绿色框内线的位置,而且从结果来看显著性线段是从下往上画的。

此时好像发现comparisons对应的listscale_x_discrete指定的x轴几个分类的顺序有关。因此继续测试,将

scale_x_discrete(limits=c("A2A", "B2B", "A2B", "B2A")) +

换成:

scale_x_discrete(limits=c("A2A", "A2B", "B2A", "B2B")) +

生成的结果如下:

虽然x轴几个分类顺序变了,但是x-颜色-label的对应关系是正确的,boxplot的位置是正确的,以及显著性线段的位置也标记正确。同时更大的发现是scale_x_discrete指定的x轴顺序与comparisons list中分类顺序无关!最后的结论是一定要加scale_x_discrete指定顺序,虽然这个顺序可以是任意的。

 

最终为了适应所有table,我们以变量形式指comparisonsscale_x_discrete指定x轴顺序如下:

myargs <- commandArgs(TRUE)

 

if (!length(myargs)){

   print("[USAGE] Rscript cptmt_switch.r <demo_table>")

   q()

}

 

library(ggplot2)

library(ggpubr)

switch_table <- read.delim(myargs[1], as.is=TRUE)

compare <- combn(unique(switch_table$Switch), 2, simplify=FALSE)

ggboxplot(data=switch_table, x="Switch", y="GC", palette="Set2", fill="Switch", ylab="GC content (%)", outlier.size=-1, notch=TRUE, legend.title="") +

scale_x_discrete(limits=unique(switch_table$Switch)) +

ylim(30, 85) + theme_pubr(base_size=16, border=TRUE) +

theme(axis.title.x=element_blank()) + stat_compare_means(comparisons=compare, size=4, tip.length=0)

ggsave("ggpubr_debug.png", width=10, height=7, dpi=600)

 



https://wap.sciencenet.cn/blog-2970729-1429796.html

上一篇:Hi-C Insulation score计算原理解析
收藏 IP: 223.76.222.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-30 05:11

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部