跟着Nature Communications学作图:R语言ggplot2散点组合误差线展示响应比(Response ratio)

简介: 跟着Nature Communications学作图:R语言ggplot2散点组合误差线展示响应比(Response ratio)

论文

Meta-analysis of the impacts of global change factors on soil microbial diversity and functionality

https://www.nature.com/articles/s41467-020-16881-7#Sec15

论文里提供了数据和代码,很好的学习素材

这篇论文是公众号的一位读者留言,说这篇论文提供了数据和代码,但是代码比较长,看起来比较吃力。我看了论文中提供的代码,大体上能够看懂,争取抽时间把论文中提供的代码都复现一下。因为论文中的图都对应着提供了作图数据,我们想复现论文中的图。关于用原始数据分析的部分后续有时间在单独介绍。

今天的推文我们复现论文中的figure1

image.png

论文中提供的作图数据如下,excel存储

image.png

加载需要用到的R包

library(readxl)
library(tidyverse)
library(latex2exp)
library(ggplot2)

读取数据

metaresult<-read_excel("data/20221129/41467_2020_16881_MOESM9_ESM.xlsx",
                       sheet = 'Fig1')
colnames(metaresult)

首先是第一个小图a

论文中的代码是用RR作为Y轴,GCFs作为X轴,然后再通过coord_flip()函数整体旋转;论文中关于字体上小标是用expression函数实现的,这里我们使用latex2exp这个R包

代码我们参考论文中的代码,但是不完全按照他的写

数据整理和作图代码

data1<-metaresult %>% 
  filter(Variables=="Richness"|Variables=="Shannon")

data1$GCFs

data1<-data1 %>% 
  mutate(GCFs=factor(GCFs,
                     levels = c("N_P_K","N_P","N_PPT+",
                                "W_eCO2","LUC","N","P",
                                "PPT+","PPT-","eCO2","W"))
)

data1 %>% colnames()

ggplot(data = data1,
       aes(x=`Weighted means of RR`,
           y=`GCFs`,
           shape=Variables))+
  geom_vline(xintercept=0,linetype = "dashed",size=0.2)+
  geom_errorbarh(aes(xmin=`Lower confidence intervals`,
                     xmax=`Upper confidence intervals`),
                 position=position_dodge(0.8),
                 height=0.2)+
  geom_point(position=position_dodge(0.8), 
             size=3, stroke = 0.3,
             aes(fill=GCFs),
             show.legend = FALSE)+
  geom_text(aes(y =`GCFs` , x = `Upper confidence intervals`+0.015, 
                label = `Sample sizes`),
            position = position_dodge(width = 0.8),
            vjust = 0.4, hjust=0.4, size = 4, 
            check_overlap = FALSE)+
  geom_segment(y = 11.6, x = -Inf, 
               yend = 11.6, xend = Inf, 
               colour = "black",size=0.4)+
  scale_shape_manual(values=c("Richness"=21,"Shannon"=22))+
  scale_x_continuous(limits=c(-0.17,0.17),
                     breaks = c(-0.16,-0.08,0,0.08,0.16))+
  scale_y_discrete(breaks=c("N_P_K","N_P","N_PPT+",
                            "W_eCO2","LUC","N","P",
                            "PPT+","PPT-","eCO2","W"),
                   labels=c(TeX(r"($N \times P \times K$)"),
                            TeX(r"($N \times P$)"),
                            TeX(r"($N \times PPT$+)"),
                            TeX(r"($W \times eCO_2$)"),
                            "LUC","N","P","PPT+","PPT-",
                            TeX(r"($eCO_2$)"),
                            "W"))+
  labs(x = "Global change factors ", y = "RR of alpha diversity",colour = 'black')+
  theme(legend.title = element_blank(),
        legend.position=c(0.2,0.94),
        legend.key = element_rect(fill = "white",size = 2),
        legend.key.width = unit(0.5,"lines"),
        legend.key.height= unit(0.8,"lines"),
        legend.background = element_blank(),
        legend.text=element_text(size=6),
        panel.background = element_rect(fill = 'white', colour = 'white'),
        axis.title=element_text(size=9),
        axis.text.y = element_text(colour = 'black', size = 8),
        axis.text.x = element_text(colour = 'black', size = 8),
        axis.line = element_line(colour = 'black',size=0.4),
        axis.line.y = element_blank(),
        axis.ticks = element_line(colour = 'black',size=0.4),
        axis.ticks.y = element_blank())

输出结果

image.png

小图b

data2<-metaresult %>% 
  filter(Variables=="Beta Diversity")

data2$GCFs

data2<-data2 %>% 
  mutate(GCFs=factor(GCFs,
                     levels = c("N_P_K","N_P","N_PPT+",
                                "W_eCO2","LUC","N","P",
                                "PPT+","PPT-","eCO2","W"))
  )

data2 %>% colnames()


ggplot(data = data2,
       aes(x=`Weighted means of RR`,
           y=`GCFs`))+
  geom_vline(xintercept=0,linetype = "dashed",size=0.2)+
  geom_errorbarh(aes(xmin=`Lower confidence intervals`,
                     xmax=`Upper confidence intervals`),
                 height=0.2)+
  geom_point(size=3, stroke = 0.3,
             shape=21,
             aes(fill=GCFs),
             show.legend = FALSE)+
  geom_text(aes(y =`GCFs` , x = `Upper confidence intervals`+0.1, 
                label = `Sample sizes`),
            #position = position_dodge(width = 0.8),
            vjust = 0.4, hjust=0.4, size = 4, 
            check_overlap = FALSE)+
  geom_segment(y = 11.6, x = -Inf, 
               yend = 11.6, xend = Inf, 
               colour = "black",size=0.4)+
  scale_x_continuous(limits=c(-0.6,1.1),breaks = c(-0.5,0,0.5,1))+
  scale_y_discrete(breaks=c("N_P_K","N_P","N_PPT+",
                            "W_eCO2","LUC","N","P",
                            "PPT+","PPT-","eCO2","W"),
                   labels=c(TeX(r"($N \times P \times K$)"),
                            TeX(r"($N \times P$)"),
                            TeX(r"($N \times PPT$+)"),
                            TeX(r"($W \times eCO_2$)"),
                            "LUC","N","P","PPT+","PPT-",
                            TeX(r"($eCO_2$)"),
                            "W"))+
  labs(y = "Global change factors ", 
       x = "RR of alpha diversity",
       colour = 'black')+
  theme(legend.title = element_blank(),
        legend.position=c(0.2,0.9),
        legend.key = element_rect(fill = "white",size = 2),
        legend.key.width = unit(0.5,"lines"),
        legend.key.height= unit(0.8,"lines"),
        legend.background = element_blank(),
        legend.text=element_text(size=6),
        panel.background = element_rect(fill = 'white', colour = 'white'),
        axis.title=element_text(size=9),
        axis.text.y = element_text(colour = 'black', size = 8),
        axis.text.x = element_text(colour = 'black', size = 8),
        axis.line = element_line(colour = 'black',size=0.4),
        axis.line.y = element_blank(),
        axis.ticks = element_line(colour = 'black',size=0.4),
        axis.ticks.y = element_blank())

image.png

小图c

data3<-metaresult %>% 
  filter(Variables=="Community structure")

data3$GCFs

data3<-data3 %>% 
  mutate(GCFs=factor(GCFs,
                     levels = c("N_P_K","N_P","N_PPT+",
                                "W_eCO2","LUC","N","P",
                                "PPT+","PPT-","eCO2","W"))
  )

data3 %>% colnames()


ggplot(data = data3,
       aes(x=`Weighted means of RR`,
           y=`GCFs`))+
  geom_vline(xintercept=0,linetype = "dashed",size=0.2)+
  geom_errorbarh(aes(xmin=`Lower confidence intervals`,
                     xmax=`Upper confidence intervals`),
                 height=0.2)+
  geom_point(size=3, stroke = 0.3,
             shape=21,
             aes(fill=GCFs),
             show.legend = FALSE)+
  geom_text(aes(y =`GCFs` , x = `Upper confidence intervals`+0.1, 
                label = `Sample sizes`),
            #position = position_dodge(width = 0.8),
            vjust = 0.4, hjust=0.4, size = 4, 
            check_overlap = FALSE)+
  geom_segment(y = 11.6, x = -Inf, 
               yend = 11.6, xend = Inf, 
               colour = "black",size=0.4)+
  scale_x_continuous(limits=c(-0.6,2.0),breaks = c(-0.5,0,0.5,1,1.5,2.0))+
  scale_y_discrete(breaks=c("N_P_K","N_P","N_PPT+",
                            "W_eCO2","LUC","N","P",
                            "PPT+","PPT-","eCO2","W"),
                   labels=c(TeX(r"($N \times P \times K$)"),
                            TeX(r"($N \times P$)"),
                            TeX(r"($N \times PPT$+)"),
                            TeX(r"($W \times eCO_2$)"),
                            "LUC","N","P","PPT+","PPT-",
                            TeX(r"($eCO_2$)"),
                            "W"))+
  labs(y = "Global change factors ", 
       x = "RR of community structure",
       colour = 'black')+
  theme(legend.title = element_blank(),
        legend.position=c(0.2,0.9),
        legend.key = element_rect(fill = "white",size = 2),
        legend.key.width = unit(0.5,"lines"),
        legend.key.height= unit(0.8,"lines"),
        legend.background = element_blank(),
        legend.text=element_text(size=6),
        panel.background = element_rect(fill = 'white', colour = 'white'),
        axis.title=element_text(size=9),
        axis.text.y = element_text(colour = 'black', size = 8),
        axis.text.x = element_text(colour = 'black', size = 8),
        axis.line = element_line(colour = 'black',size=0.4),
        axis.line.y = element_blank(),
        axis.ticks = element_line(colour = 'black',size=0.4),
        axis.ticks.y = element_blank())

图b和图c是一样的

最后是拼图

论文中提供的拼图代码是用ggpubr这个R包做的

ggpubr::ggarrange(p1, p2, p3, 
          widths = c(7, 5, 5),
          ncol = 3, nrow = 1, 
          labels = c("a", "b", "c"), 
          font.label=list(size=12),
          hjust = 0, vjust = 1)

我自己更习惯使用patchwork这个R包

library(patchwork)

p1+
  p2+theme(axis.text.y = element_blank(),
           axis.title.y = element_blank())+
  p3+theme(axis.text.y = element_blank(),
           axis.title.y = element_blank())+
  plot_annotation(tag_levels = "a")+
  plot_layout(widths = c(7, 5, 5))

最终结果

image.png

示例数据和代码可以自己到论文中下载,如果需要我推文中的代码和数据可以给公众号推文点赞,点击在看,最后留言获取

查rma()函数找到了这个链接

http://www.simonqueenborough.info/R/specialist/meta-analysis#:~:text=The%20function%20rma()%20(random,compute%20effect%20sizes%20before%20modelling.&text=Random%20effect%20model%20can%20be,%2D%2D%2DFixed%20effect%20model%20cannot.

http://www.simonqueenborough.info/R/intro/index.html

欢迎大家关注我的公众号

小明的数据分析笔记本

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!
相关文章
|
3月前
|
数据可视化 数据挖掘 图形学
R语言基础可视化:使用ggplot2构建精美图形的探索
【8月更文挑战第29天】 `ggplot2`是R语言中一个非常强大的图形构建工具,它基于图形语法提供了一种灵活且直观的方式来创建各种统计图形。通过掌握`ggplot2`的基本用法和美化技巧,你可以轻松地将复杂的数据转化为直观易懂的图形,从而更好地理解和展示你的数据分析结果。希望本文能够为你探索`ggplot2`的世界提供一些帮助和启发。
|
3月前
|
数据可视化
R语言自定义图形:ggplot2中的主题与标签设置
【8月更文挑战第30天】`ggplot2`作为R语言中功能强大的绘图包,其自定义能力让数据可视化变得更加灵活和多样。通过合理使用`theme()`函数和`labs()`函数,以及`geom_text()`和`geom_label()`等几何对象,我们可以轻松创建出既美观又富有表达力的图形。希望本文的介绍能够帮助你更好地掌握`ggplot2`中的主题与标签设置技巧。
|
6月前
|
存储 数据可视化 数据挖掘
R语言可视化:ggplot2冲积/桑基图sankey分析大学录取情况、泰坦尼克幸存者数据
R语言可视化:ggplot2冲积/桑基图sankey分析大学录取情况、泰坦尼克幸存者数据
|
6月前
|
算法 数据可视化
R语言社区检测算法可视化网络图:ggplot2绘制igraph对象分析物种相对丰度
R语言社区检测算法可视化网络图:ggplot2绘制igraph对象分析物种相对丰度
|
6月前
r语言ggplot2误差棒图快速指南
r语言ggplot2误差棒图快速指南
|
6月前
|
数据可视化
R语言ggplot2 对Facebook用户数据可视化分析
R语言ggplot2 对Facebook用户数据可视化分析
|
编解码 数据可视化 数据挖掘
R语言之 ggplot 2 和其他图形
R语言之 ggplot 2 和其他图形
96 0
|
存储 Go
速绘丨GO富集气泡图绘制方法,利用R语言ggplot2包快速绘制,完整脚本可重复绘图
速绘丨GO富集气泡图绘制方法,利用R语言ggplot2包快速绘制,完整脚本可重复绘图
|
数据采集 机器学习/深度学习 SQL
绝不可错过!R语言与ggplot2实现SCI论文数据分析神器
绝不可错过!R语言与ggplot2实现SCI论文数据分析神器
211 0
|
数据可视化 数据挖掘 定位技术
跟着Nature Communications学作图:R语言ggplot2画世界地图并用md语法添加文字标签
跟着Nature Communications学作图:R语言ggplot2画世界地图并用md语法添加文字标签

热门文章

最新文章