ggplot2添加躺平版P_value

2022-09-21 15:33:00 浏览数 (1)

❝今天来主要介绍如何在常见的箱线图基础上水平添加显著性标记,也许恰好您正好有此特殊需求,希望对各位观众老爷有所帮助;下面来看具体案例; ❞

加载R包

代码语言:javascript复制
library(tidyverse)
library(rstatix)
library(ggprism)
library(ggpubr)

定义主题

代码语言:javascript复制
theme_niwot <- function(){
  theme_test()  
    theme(axis.title.x = element_blank(),
          axis.line = element_line(color = "#3D4852"),
          axis.ticks = element_line(color = "#3D4852"),
          panel.grid.major.y = element_line(color = "#DAE1E7"),
          panel.grid.major.x = element_blank(),
          plot.margin = unit(rep(0.2,4),"cm"),
          axis.text = element_text(size = 10, color = "#22292F"),
          axis.title = element_text(size = 10, hjust = 1),
          axis.title.y = element_blank(),
          axis.text.y = element_text(margin = margin(r = 5)),
          axis.text.x = element_text(margin = margin(t = 5)),
          legend.position = "non")
}

统计分析

代码语言:javascript复制
stat.test2 <- ToothGrowth %>% mutate(dose=as.factor(dose)) %>% group_by(dose) %>% 
  t_test(len ~ supp) %>%
  adjust_pvalue() %>% add_significance("p.adj") %>% add_xy_position(x="dose") 

stat.test3 <- ToothGrowth %>% 
  t_test(len ~ dose,p.adjust.method = "bonferroni") %>%
  adjust_pvalue() %>% add_significance("p.adj") %>% add_xy_position() 


res.aov <- ToothGrowth %>% mutate(dose=as.factor(dose)) %>% anova_test(len ~ dose)

数据可视化

代码语言:javascript复制
ToothGrowth %>% mutate(dose=as.factor(dose)) %>% 
  ggplot(aes(dose,len)) 
  stat_boxplot(geom = "errorbar",width=0.2,aes(fill = supp),position = position_dodge(1))  
  geom_boxplot(aes(fill= supp),position = position_dodge(1)) 
  stat_pvalue_manual(stat.test2,label = "p.adj.signif",label.size=6,hide.ns = T, coord.flip = TRUE) 
  stat_pvalue_manual(stat.test3,label = "p.adj.signif",label.size=6,hide.ns = T, coord.flip = TRUE) 
  coord_flip() 
  labs(subtitle = get_test_label(res.aov, detailed = TRUE)) 
  scale_y_continuous(expand=c(0,0),limits = c(0,42)) 
  theme_niwot() 
  scale_fill_manual(values=c("#00AFBB", "#E7B800"))

❝本节介绍到此结束,主要在于「stat_pvalue_manual」函数添加「coord.flip = TRUE」参数使得图形得以躺平添加显著性标记,一点很小的细节;那么若要在分面图上进行躺平该如何操作,这个我们以后在进行讨论;

0 人点赞