Posted on

转载自:ggplot2添加p值和显著性 – 简书

数据可视化分析中我们经常需要进行数据间的统计分析,并进行显著性标记,虽然ggpur包被大佬吐槽制造混乱,但在进行显著性标记标记方面也是有其可取之处。

下面主要展示ggpubrggsignif包对数据进行统计分析,喜欢的小伙伴可以关注我的公众号R语言数据分析指南将分享更多实用文档,先行拜谢了

加载R包

library(pacman)
pacman::p_load(tidyverse,ggpubr,rstatix,ggsci,ggsignif,reshape2)

wilcox(秩合检验)

ggplot(iris,aes(Species,Sepal.Length,fill=Species)) + 
geom_boxplot()+geom_jitter(shape=16,size=2,position=position_jitter(0.2))+
  geom_signif(comparisons = list(c("versicolor", "virginica"),
                                 c("versicolor","setosa"),
                                 c("virginica","setosa")),
              map_signif_level=T,
              textsize=6,test=wilcox.test,step_increase=0.2)+
  guides(fill=FALSE)+xlab(NULL)+theme_classic()
 

t_test分析

ggplot(iris,aes(Species,Sepal.Length,fill=Species)) + 
  geom_boxplot()+
  scale_fill_jco()+
  geom_jitter(shape=16,size=2,position=position_jitter(0.2))+
  geom_signif(comparisons = list(c("versicolor", "virginica"),
                                 c("versicolor","setosa"),
                                 c("virginica","setosa")),
              map_signif_level=T,
              textsize=6,test=t.test,step_increase=0.2)+
  guides(fill=F)+xlab(NULL)+theme_classic()
 

anova分析

ggplot(iris,aes(Species,Sepal.Length,fill=Species)) + 
  geom_boxplot()+
  scale_fill_jco()+
  geom_jitter(shape=16,size=2,position=position_jitter(0.2))+
  stat_compare_means(method = "anova")+theme_bw()
 

kruskal分析

ggplot(iris,aes(Species,Sepal.Length,fill=Species)) + 
  geom_boxplot()+
  scale_fill_jco()+
  geom_jitter(shape=16,size=2,position=position_jitter(0.2))+
  stat_compare_means()+theme_bw()
 

对多组数据进行分面

iris %>% melt() %>%
  ggplot(aes(variable,value,fill=Species)) + 
  geom_boxplot()+
  scale_fill_jco()+
  stat_compare_means(label = "p.format")+
  facet_grid(.~variable,scales = "free",space="free_x")+
  theme_bw()+theme(axis.text.x = element_blank())
 
iris %>% melt() %>%
  ggplot(aes(variable,value,fill=Species)) + 
  geom_boxplot()+
  scale_fill_jco()+
  stat_compare_means(label =  "p.signif", label.x = 1.5)+
  facet_grid(.~variable,scales = "free",space="free_x")+
  theme_bw()+theme(axis.text.x = element_blank())
 

发表评论

邮箱地址不会被公开。 必填项已用*标注