基本图形绘制

配对数据wilcox.test检验及可视化

2021-11-18  本文已影响0人  bioYIYI

用途

配对样本秩和检验及可视化(箱线图)

代码

加载包

library(plyr)
library(ggplot2)
library("ggpubr")
library(ggplot2)
library(cowplot)

第一组数据检验及画图(不单独生成图片)

Standardizing_functional<-read.table("1_shannon_Standardizing/functional/delta_shannon_patient.xls",header=T,sep="\t")
c<-Standardizing_functional$delta.H_P-Standardizing_functional$delta.H_S
ylim=max(c)+0.1
Standardizing_functional$Standardizing_functional<-c
p<-wilcox.test(Standardizing_functional$delta.H_P,Standardizing_functional$delta.H_S,paired = T)
p_Standardizing_functional<-ggplot(Standardizing_functional, aes(x="Standardizing functional", y=Standardizing_functional)) + geom_boxplot(color='blue',outlier.colour="grey", outlier.shape=8, outlier.alpha=0.8,outlier.size= 0.7, width = 0.2) + guides(fill=F, color=F) +theme(panel.grid.major.x=element_blank(), axis.text.x=element_text(angle=0, hjust=0.5))  +ylab('shannon (P) - shannon (S)') +xlab('')+ annotate(geom="text", x=1.2, y=ylim, label=paste('Wilcoxon, p =',round(p$p.value,5),sep=""),colour='black', size=2)

第2/3/4/5/6组数据检验及画图

Standardizing_none_functional<-read.table("1_shannon_Standardizing/none_functional/delta_shannon_patient.xls",header=T,sep="\t")
c<-Standardizing_none_functional$delta.H_P-Standardizing_none_functional$delta.H_S
ylim=max(c)+0.1
Standardizing_none_functional$Standardizing_none_functional<-c
p<-wilcox.test(Standardizing_none_functional$delta.H_P,Standardizing_none_functional$delta.H_S,paired = T)
p_Standardizing_none_functional<-ggplot(Standardizing_none_functional, aes(x="Standardizing none_functional", y=Standardizing_none_functional)) + geom_boxplot(color='blue',outlier.colour="grey", outlier.shape=8, outlier.alpha=0.8,outlier.size= 0.7, width = 0.2) + guides(fill=F, color=F) +theme(panel.grid.major.x=element_blank(), axis.text.x=element_text(angle=0, hjust=0.5))  +ylab('shannon (P) - shannon (S)') +xlab('')+ annotate(geom="text", x=1.2, y=ylim, label=paste('Wilcoxon, p =',round(p$p.value,5),sep=""),colour='black', size=2)

all<-read.table("1_shannon_Standardizing/all/delta_shannon_patient.xls",header=T,sep="\t")
c<-all$delta.H_P-all$delta.H_S
ylim=max(c)+0.1
all$all<-c
p<-wilcox.test(all$delta.H_P,all$delta.H_S,paired = T)
p_all<-ggplot(all, aes(x="Standardizing all", y=all)) + geom_boxplot(color='blue',outlier.colour="grey", outlier.shape=8, outlier.alpha=0.8,outlier.size= 0.7, width = 0.2) + guides(fill=F, color=F) +theme(panel.grid.major.x=element_blank(), axis.text.x=element_text(angle=0, hjust=0.5))  +ylab('shannon (P) - shannon (S)') +xlab('')+ annotate(geom="text", x=1.2, y=ylim, label=paste('Wilcoxon, p =',round(p$p.value,5),sep=""),colour='black', size=2)

without_normalize_functional<-read.table("2_shannon_without_normalize/functional/delta_shannon_patient.xls",header=T,sep="\t")
c<-without_normalize_functional$delta.H_P-without_normalize_functional$delta.H_S
ylim=max(c)+0.1
without_normalize_functional$without_normalize_functional<-c
p<-wilcox.test(without_normalize_functional$delta.H_P,without_normalize_functional$delta.H_S,paired = T)
p_without_normalize_functional<-ggplot(without_normalize_functional, aes(x="NoneStandardizing functional", y=without_normalize_functional)) + geom_boxplot(color='blue',outlier.colour="grey", outlier.shape=8, outlier.alpha=0.8,outlier.size= 0.7, width = 0.2) + guides(fill=F, color=F) +theme(panel.grid.major.x=element_blank(), axis.text.x=element_text(angle=0, hjust=0.5))  +ylab('shannon (P) - shannon (S)') +xlab('')+ annotate(geom="text", x=1.2, y=ylim, label=paste('Wilcoxon, p =',round(p$p.value,5),sep=""),colour='black', size=2)

without_normalize_none_functional<-read.table("2_shannon_without_normalize/none_functional/delta_shannon_patient.xls",header=T,sep="\t")
c<-without_normalize_none_functional$delta.H_P-without_normalize_none_functional$delta.H_S
ylim=max(c)+0.1
without_normalize_none_functional$without_normalize_none_functional<-c
p<-wilcox.test(without_normalize_none_functional$delta.H_P,without_normalize_none_functional$delta.H_S,paired = T)
p_without_normalize_none_functional<-ggplot(without_normalize_none_functional, aes(x="NoneStandardizing none_functional", y=without_normalize_none_functional)) + geom_boxplot(color='blue',outlier.colour="grey", outlier.shape=8, outlier.alpha=0.8,outlier.size= 0.7, width = 0.2) + guides(fill=F, color=F) +theme(panel.grid.major.x=element_blank(), axis.text.x=element_text(angle=0, hjust=0.5))  +ylab('shannon (P) - shannon (S)') +xlab('')+ annotate(geom="text", x=1.2, y=ylim, label=paste('Wilcoxon, p =',round(p$p.value,5),sep=""),colour='black', size=2)

without_normalize_all<-read.table("2_shannon_without_normalize/all/delta_shannon_patient.xls",header=T,sep="\t")
c<-without_normalize_all$delta.H_P-without_normalize_all$delta.H_S
ylim=max(c)+0.1
without_normalize_all$without_normalize_all<-c
p<-wilcox.test(without_normalize_all$delta.H_P,without_normalize_all$delta.H_S,paired = T)
p_without_normalize_all<-ggplot(without_normalize_all, aes(x="NoneStandardizing all", y=without_normalize_all)) + geom_boxplot(color='blue',outlier.colour="grey", outlier.shape=8, outlier.alpha=0.8,outlier.size= 0.7, width = 0.2) + guides(fill=F, color=F) +theme(panel.grid.major.x=element_blank(), axis.text.x=element_text(angle=0, hjust=0.5))  +ylab('shannon (P) - shannon (S)') +xlab('')+ annotate(geom="text", x=1.2, y=ylim, label=paste('Wilcoxon, p =',round(p$p.value,5),sep=""),colour='black', size=2)

把多个图片组合到一起

方法1:

pdf("Standardizing.pdf",width=12,height=8)
g <- ggdraw() + draw_plot(p_Standardizing_functional, 0, 0.5, 0.33, 0.5) + draw_plot(p_Standardizing_none_functional, 0.33, 0.5, 0.33, 0.5) +draw_plot(p_all, 0.66, 0.5, 0.33, 0.5)+ draw_plot(p_without_normalize_functional, 0, 0, 0.33, 0.5) + draw_plot(p_without_normalize_none_functional, 0.33, 0, 0.33, 0.5) +draw_plot(p_without_normalize_all, 0.66, 0, 0.33, 0.5)
print(g)
dev.off()

方法2:

pdf("Standardizing_method2.pdf",width=12,height=8)
ggarrange(p_Standardizing_functional,p_Standardizing_none_functional,p_all,p_without_normalize_functional,p_without_normalize_none_functional,p_without_normalize_all,ncol=3,nrow=2)
dev.off()

输入数据示例

image.png

结果示例

image.png
上一篇下一篇

猜你喜欢

热点阅读