||
测试数据如下:
格式如下:
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对应的list与scale_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,我们以变量形式指comparisons和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 <- 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)
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-23 01:51
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社