zl程序教程

您现在的位置是:首页 >  其他

当前栏目

跟着Nature Communication学作图:R语言ggpubr包画箱线图并添加显著性P值

2023-02-19 12:27:38 时间

论文

Microbiomes in the Challenger Deep slope and bottom-axis sediments

https://www.nature.com/articles/s41467-022-29144-4#code-availability

对应代码链接

https://github.com/ucassee/Challenger-Deep-Microbes

论文里提供了大部分图的数据和代码,很好的学习材料,感兴趣的同学可以找来参考,今天的推文重复一下论文中的Figure1b

论文中提供的代码是用ggpubr这个R包实现的,如果比较着急要结果可以使用这个R包来作图,如果是学习为目的,还是推荐ggplot2的基础

部分数据集截图

image.png

读取数据集

dat<-read.delim("data/20220602/NCfigure1b.txt",
                header = TRUE,
                check.names = FALSE,
                sep="\t")
head(dat)

带有百分号读取进来是字符,我们把它转换成数字

library(tidyverse)


dat %>% 
  mutate(`Novel 16s miTags (%)` = dat$`Novel 16s miTags (%)` %>%  parse_number()) -> dat01

对表示分组的文本进行处理

dat01 %>% 
  mutate(group=case_when(
    Group == "Bottom-axis" ~ "Bottom",
    Group == "Slope" ~ "Slope",
    Group == "Mariana Water" ~ "Mariana Water",
    TRUE ~ "Deep sea"
  )) -> dat02

赋予因子水平

dat02$group <- factor(dat02$group, 
                      levels=c("Bottom", "Slope","Deep sea", "Mariana Water"), 
                      ordered=TRUE)
table(dat02$group)

作图代码

p1<- ggboxplot(dat02, x="group", y="Novel 16s miTags (%)", 
               fill = "group", width = 0.5,
               xlab = "",
               palette = c("#F8766D","#00BFC4","#FEFF99","#B14A87"))+
  ylab(label = "Novel 16S miTags (%)")+
  #scale_y_continuous(labels = scales::percent)+
  guides(fill=F)+
  scale_x_discrete(labels = c("Bottom\naxis\n(n=17)", 
                              "Slope\n(n=20)",
                              "Deep\nsed\n(n=20)",
                              "Mariana\nwater\n(n=7)"))+
  theme(axis.text = element_text(size=10,family="serif"))+
  stat_compare_means(comparisons=p1_comparisons,
                     label.y = c( 48, 53,40, 58),  
                     method = "wilcox.test",size=3) # Add pairwise comparisons p-value 
p1

image.png

试一下论文中提供的拼图代码

library(cowplot)
aligned_plots<- align_plots(p1, p1,align="h")
ggdraw( xlim = c(0, 1.1), ylim = c(0, 0.30))+
  draw_plot(aligned_plots[[1]], x=0,y=0,  width=0.5, height = 0.28)+
  draw_plot(aligned_plots[[2]], x=0.50,y=0, width = 0.5, height = 0.28)