编程语言相关性

ggplot2绘制有显著性标注的箱线图

2021-10-28  本文已影响0人  Wei_Sun

1.箱线图

近似正态分布的箱线图与正态分布的概率密度函数的比较:

2.基础绘图

ggplot()+geom_boxplot()

# 导入绘图数据
> phe<-read.csv("phe.csv")
# 查看数据,第一列为分组,后四列为四个性状
> head(phe)
   group    TL    SA    AD    NR
1   weak 24.12  6.67 0.804 20.23
2   weak 24.12  6.67 0.804 20.23
3   weak 24.12  6.67 0.804 20.23
4 strong 43.31 12.96 0.875 25.23
5 strong 43.31 12.96 0.875 25.23
6 strong 43.31 12.96 0.875 25.23
# 载入ggplot2 R包
> library(ggplot2)
# 绘图
> ggplot(phe,aes(x=group,y=TL,fill=group))+geom_boxplot()

3.显著性差异分析

# 载入显著性分析R包ggpubr
> library(ggpubr)
# 绘图
> ggplot(phe,aes(x=group,y=TL,fill=group)) 
  + geom_boxplot()+stat_compare_means(method = "t.test")

想把t值换为星号,在stat_compare_means()中加入label="p.signif"

> ggplot(phe,aes(x=group,y=TL,fill=group)) + 
  geom_boxplot() + stat_compare_means(method = "t.test", label="p.signif" )

4. 添加显著性线段,ggsignif

# 用到ggsignif这个包
> library(ggsignif)
> ggplot(phe,aes(x=group,y=TL,fill=group)) + geom_boxplot() + 
  geom_signif(comparisons = list(c("strong","weak")), 
  map_signif_level = TRUE, test = t.test, y_position = c(80,30), 
  tip_length = c(0.05,0.4))

5.美化

> ggplot(phe,aes(x=group,y=TL,fill=group)) + 
 geom_boxplot()+  geom_signif(comparisons = list(c("strong","weak")),map_signif_level = TRUE,test = t.test,y_position = c(80,30),tip_length = c(0.05,0.4))+  theme_bw()+  
 scale_fill_manual(values = c("#DE6757","#5B9BD5"))+ 
 theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank())+  labs(x="Group", y="TL value")+ 
 theme(axis.title.x =element_text(size=12,face = "bold"), axis.title.y=element_text(size=12,face = "bold"),axis.text = element_text(size = 12,face = "bold"))

6.合并出图

用到ggpubr包中的ggarrange()函数

> library(ggpubr)
> a<-ggplot(phe,aes(x=group,y=TL,fill=group)) +geom_boxplot()+geom_signif(comparisons = list(c("strong","weak")),map_signif_level = TRUE,test = t.test,y_position = c(80,30),tip_length = c(0.05,0.4))+theme_bw()+ scale_fill_manual(values = c("#DE6757","#5B9BD5"))+theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank())+labs(x="Group", y="TL value")+theme(axis.title.x =element_text(size=12,face = "bold"), axis.title.y=element_text(size=12,face = "bold"),axis.text = element_text(size = 12,face = "bold"))
> b<-ggplot(phe,aes(x=group,y=SA,fill=group)) +geom_boxplot()+geom_signif(comparisons = list(c("strong","weak")),map_signif_level = TRUE,test = t.test,y_position = c(17,5),tip_length = c(0.05,0.4))+theme_bw()+ scale_fill_manual(values = c("#DE6757","#5B9BD5"))+theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank())+labs(x="Group", y="SA value")+theme(axis.title.x =element_text(size=12,face = "bold"), axis.title.y=element_text(size=12,face = "bold"),axis.text = element_text(size = 12,face = "bold"))
> c<-ggplot(phe,aes(x=group,y=AD,fill=group)) +geom_boxplot()+geom_signif(comparisons = list(c("strong","weak")),map_signif_level = TRUE,test = t.test,y_position = c(0.9,0),tip_length = c(0.05,0.05))+theme_bw()+ scale_fill_manual(values = c("#DE6757","#5B9BD5"))+theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank())+labs(x="Group", y="AD value")+theme(axis.title.x =element_text(size=12,face = "bold"), axis.title.y=element_text(size=12,face = "bold"),axis.text = element_text(size = 12,face = "bold"))
> d<-ggplot(phe,aes(x=group,y=NR,fill=group)) +geom_boxplot()+geom_signif(comparisons = list(c("strong","weak")),map_signif_level = TRUE,test = t.test,y_position = c(40,0),tip_length = c(0.05,0.4))+theme_bw()+ scale_fill_manual(values = c("#DE6757","#5B9BD5"))+theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank())+labs(x="Group", y="NR value")+theme(axis.title.x =element_text(size=12,face = "bold"), axis.title.y=element_text(size=12,face = "bold"),axis.text = element_text(size = 12,face = "bold"))
> pdf("phe.pdf",width = 8,height = 8)
> ggarrange(a,b,c,d + rremove("x.text"), ncol = 2, nrow = 2)
> dev.off()
上一篇下一篇

猜你喜欢

热点阅读