前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ggplot2优雅对并排条形图添加显著性标记

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

作者头像
R语言数据分析指南
发布2022-09-21 15:08:25
1.4K0
发布2022-09-21 15:08:25
举报
文章被收录于专栏:R语言数据分析指南

❝本节来介绍如何使用「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"))

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-06-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 R语言数据分析指南 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 加载R包
  • 统计分析
  • 数据可视化
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档