跟着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

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

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

论文中提供的代码链接

https://static-content.springer.com/esm/art%3A10.1038%2Fs41467-020-16881-7/MediaObjects/41467_2020_16881_MOESM8_ESM.txt

今天的推文我们复现论文中的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、生物信息学入门学习资料及自己的学习笔记!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,968评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,601评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,220评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,416评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,425评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,144评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,432评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,088评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,586评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,028评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,137评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,783评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,343评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,333评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,559评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,595评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,901评论 2 345

推荐阅读更多精彩内容