R语言R基本图形绘制

ggplot2优雅的创建字母显著性标记图

2021-12-11  本文已影响0人  R语言数据分析指南

本节来介绍如何使用R做单因素与多因素方差分析,并添加显著性字母标记,下面通过2个小栗子来进行展示

加载R包

library(tidyverse)
library(multcompView)
library(ggsci)

加载数据集

df <- ToothGrowth %>% as_tibble() %>% mutate(dose=as.factor(dose))

方差分析

anova <- aov(len~dose,data=df)
summary(anova)
> summary(anova)
            Df Sum Sq Mean Sq F value   Pr(>F)    
dose         2   2426    1213   67.42 9.53e-16 ***
Residuals   57   1026      18                     
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Tukey多重配对比较

tukey <- TukeyHSD(anova)
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = len ~ dose, data = df)

$dose
        diff       lwr       upr    p adj
1-0.5  9.130  5.901805 12.358195 0.00e+00
2-0.5 15.495 12.266805 18.723195 0.00e+00
2-1    6.365  3.136805  9.593195 4.25e-05

创建字母

cld <- multcompLetters4(anova,tukey)

整合数据

dt <- df %>% group_by(dose) %>%
  summarise(w=mean(len), sd = sd(len)) %>%
  arrange(desc(w)) %>%
  ungroup() %>% 
  left_join(.,as.data.frame.list(cld$dose) %>% select(1) %>% 
              rownames_to_column("dose"))
  dose      w    sd Letters
  <chr> <dbl> <dbl> <chr>  
1 2      26.1  3.77 a      
2 1      19.7  4.42 b      
3 0.5    10.6  4.50 c      

绘制条形图

ggplot(dt, aes(dose, w)) + 
  geom_bar(stat = "identity",aes(fill=dose),show.legend = FALSE,width=0.5) +
  geom_errorbar(aes(ymin = w-sd, ymax=w+sd), width = 0.1) +
  geom_text(aes(label = Letters, y = w + sd), vjust = -0.5)+
  scale_fill_jco()+
  scale_y_continuous(expand = expansion(0),
                     limits = c(0,35),
                     breaks = seq(0,35,5))+
  labs(x=NULL,y=NULL)+
  theme(
    plot.margin = unit(c(0.2,0.2,0.2,0.2), "cm"),
    panel.background = element_blank(),
    axis.line = element_line(color = "black"),
    axis.title = element_text(size = 10, color = "black",
                              face = "bold"),
    axis.text = element_text(size = 10,color = "black"),
    axis.text.x = element_text(margin=margin(t =3)),
    axis.text.y = element_text(size = 10),
    axis.title.y = element_text(margin = margin(r = 10)),
    axis.ticks.x = element_blank())
image.png

双因素方差分析

anova <- aov(len~dose*supp,data=df)
summary(anova)
dt <- df %>% group_by(dose,supp) %>%
  summarise(w=mean(len), sd = sd(len)) %>%
  arrange(desc(w)) %>%
  ungroup()

cld <- TukeyHSD(anova) %>% multcompLetters4(anova,.)
le <- as.data.frame.list(cld$`dose:supp`)
dt$Tukey <- le$Letters

dt

ggplot(dt, aes(dose, w)) + 
  geom_bar(stat = "identity",aes(fill=dose),show.legend = FALSE,width=0.5) +
  geom_errorbar(aes(ymin = w-sd, ymax=w+sd), width = 0.1) +
  geom_text(aes(label = Tukey, y = w + sd), vjust = -0.5)+
  facet_wrap(~supp)+
  scale_fill_jco()+
  scale_y_continuous(expand = expansion(0),
                     limits = c(0,35),
                     breaks = seq(0,35,5))+
  labs(x=NULL,y=NULL)+
  theme_test()+
  theme(panel.background = element_blank(),
    axis.line = element_line(color = "black"),
    axis.text = element_text(size = 10,color = "black"),
    axis.text.x = element_text(margin=margin(t =3)),
    axis.text.y = element_text(size = 10),
    axis.title.y = element_text(margin = margin(r = 10)),
    axis.ticks.x = element_blank())   

有数据可视化需求的朋友请与我联系,欢迎大家关注我的公众号R语言数据分析指南,公众号主要分享生物信息学与R语言数据可视化的经典案例,希望对大家有所帮助

上一篇下一篇

猜你喜欢

热点阅读