ggplot2优雅对并排条形图添加显著性标记

2022-09-21 15:08:25 浏览数 (1)

❝本节来介绍如何使用「rstatix」来进行统计分析,并使用「ggpubr」来添加显著性标记,下面通过一个小例子来进行展示;本次使用R内置数据集;

加载R包

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

统计分析

代码语言:javascript复制
  stat.test <- iris %>% pivot_longer(-Species) %>%
    filter(Species !="versicolor") %>% 
    mutate(group=str_sub(name,start = 1,end = 5)) %>% 
    group_by(group,name) %>% 
    t_test(value ~ Species) %>%
    adjust_pvalue() %>% add_significance("p.adj") %>% 
    add_xy_position(x="name",scales="free",fun = "max") %>% 
    select(-3,-6,-7,-8,-9,-10) %>% 
    mutate(across("xmin",str_replace,"2.8","0.8"),
           across("xmin",str_replace,"3.8","1.8"),
           across("xmax",str_replace,"3.2","1.2"),
           across("xmax",str_replace,"4.2","2.2")) %>% 
    mutate(xmin=as.numeric(xmin),xmax=as.numeric(xmax))

数据可视化

代码语言:javascript复制
  iris %>% pivot_longer(-Species) %>%
    filter(Species !="versicolor") %>% 
    mutate(group=str_sub(name,start = 1, end = 5)) %>% 
    ggplot(.,aes(x =name , y = value))  
    stat_summary(geom = "bar",position = "dodge",aes(fill=Species))  
    stat_summary(geom = "errorbar",fun.data = "mean_sdl",
                 fun.args = list(mult = 1),
                 aes(fill=Species),
                 position=position_dodge(0.9),width=0.2,color="black")  
    stat_pvalue_manual(stat.test,label = "p.adj.signif",label.size=6,hide.ns = T,
                       tip.length = 0.01) 
    facet_wrap(.~group,scale="free_x",nrow = 1) 
    labs(x=NULL,y=NULL) 
    scale_fill_manual(values=c("#BA7A70","#829BAB")) 
    scale_y_continuous(limits = c(0, 9), expand = c(0, 0)) 
    theme(axis.title.x = element_blank(),
          axis.title.y = element_text(color="black",size=12,margin = margin(r=3)),
          axis.ticks.x=element_blank(),
          axis.text.y=element_text(color="black",size = 10,margin = margin(r =2)),
          axis.text.x=element_text(color="black"),
          panel.background = element_rect(fill = NA,color = NA),
          panel.grid.minor= element_line(size=0.2,color="#e5e5e5"),
          panel.grid.major = element_line(size=0.2,color="#e5e5e5"),
          panel.border = element_rect(fill=NA,color="black",size=0.3,linetype="solid"),
          legend.key=element_blank(),
          legend.title = element_blank(),
          legend.text = element_text(color="black",size=8),
          legend.spacing.x=unit(0.1,'cm'),
          legend.key.width=unit(0.5,'cm'),
          legend.key.height=unit(0.5,'cm'),
          legend.position = c(1,1),legend.justification=c(1,1),
          legend.background=element_blank(),
          legend.box.margin = margin(0,0,0,0),
          strip.text = element_text(color="black",size=10),
          panel.spacing.x=unit(0.3,"cm"))

❝此图看起来很是简单,但是难点在于如何使用代码构建统计结果;其中也有不少细节;关于此问题日后再写推文详细介绍

0 人点赞