跟著Nature Communications學(xué)作圖:R語(yǔ)言ggplot2散點(diǎn)組合誤差線展示響應(yīng)比(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

論文里提供了數(shù)據(jù)和代碼栓始,很好的學(xué)習(xí)素材

這篇論文是公眾號(hào)的一位讀者留言岔擂,說(shuō)這篇論文提供了數(shù)據(jù)和代碼,但是代碼比較長(zhǎng)裁替,看起來(lái)比較吃力阀溶。我看了論文中提供的代碼抽莱,大體上能夠看懂葵陵,爭(zhēng)取抽時(shí)間把論文中提供的代碼都復(fù)現(xiàn)一下。因?yàn)檎撐闹械膱D都對(duì)應(yīng)著提供了作圖數(shù)據(jù)蘸泻,我們想復(fù)現(xiàn)論文中的圖琉苇。關(guān)于用原始數(shù)據(jù)分析的部分后續(xù)有時(shí)間在單獨(dú)介紹。

論文中提供的代碼鏈接

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

今天的推文我們復(fù)現(xiàn)論文中的figure1

image.png

論文中提供的作圖數(shù)據(jù)如下悦施,excel存儲(chǔ)

image.png

加載需要用到的R包

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

讀取數(shù)據(jù)

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

首先是第一個(gè)小圖a

論文中的代碼是用RR作為Y軸并扇,GCFs作為X軸,然后再通過(guò)coord_flip()函數(shù)整體旋轉(zhuǎn)抡诞;論文中關(guān)于字體上小標(biāo)是用expression函數(shù)實(shí)現(xiàn)的穷蛹,這里我們使用latex2exp這個(gè)R包

代碼我們參考論文中的代碼,但是不完全按照他的寫(xiě)

數(shù)據(jù)整理和作圖代碼

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())

輸出結(jié)果

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這個(gè)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)

我自己更習(xí)慣使用patchwork這個(gè)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))

最終結(jié)果

image.png

示例數(shù)據(jù)和代碼可以自己到論文中下載昼汗,如果需要我推文中的代碼和數(shù)據(jù)可以給公眾號(hào)推文點(diǎn)贊肴熏,點(diǎn)擊在看,最后留言獲取

查rma()函數(shù)找到了這個(gè)鏈接

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

歡迎大家關(guān)注我的公眾號(hào)

小明的數(shù)據(jù)分析筆記本

小明的數(shù)據(jù)分析筆記本 公眾號(hào) 主要分享:1顷窒、R語(yǔ)言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡(jiǎn)單小例子蛙吏;2、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)鞋吉、基因組學(xué)出刷、群體遺傳學(xué)文獻(xiàn)閱讀筆記;3坯辩、生物信息學(xué)入門(mén)學(xué)習(xí)資料及自己的學(xué)習(xí)筆記!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末崩侠,一起剝皮案震驚了整個(gè)濱河市漆魔,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖改抡,帶你破解...
    沈念sama閱讀 206,013評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件矢炼,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡阿纤,警方通過(guò)查閱死者的電腦和手機(jī)句灌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,205評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)欠拾,“玉大人胰锌,你說(shuō)我怎么就攤上這事∶暾” “怎么了资昧?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,370評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)荆忍。 經(jīng)常有香客問(wèn)我格带,道長(zhǎng),這世上最難降的妖魔是什么刹枉? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,168評(píng)論 1 278
  • 正文 為了忘掉前任叽唱,我火速辦了婚禮,結(jié)果婚禮上微宝,老公的妹妹穿的比我還像新娘棺亭。我一直安慰自己,他們只是感情好芥吟,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,153評(píng)論 5 371
  • 文/花漫 我一把揭開(kāi)白布侦铜。 她就那樣靜靜地躺著,像睡著了一般钟鸵。 火紅的嫁衣襯著肌膚如雪钉稍。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 48,954評(píng)論 1 283
  • 那天棺耍,我揣著相機(jī)與錄音贡未,去河邊找鬼。 笑死蒙袍,一個(gè)胖子當(dāng)著我的面吹牛俊卤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播害幅,決...
    沈念sama閱讀 38,271評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼消恍,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了以现?” 一聲冷哼從身側(cè)響起狠怨,我...
    開(kāi)封第一講書(shū)人閱讀 36,916評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤约啊,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后佣赖,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體恰矩,經(jīng)...
    沈念sama閱讀 43,382評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,877評(píng)論 2 323
  • 正文 我和宋清朗相戀三年憎蛤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了外傅。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 37,989評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡俩檬,死狀恐怖萎胰,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情豆胸,我是刑警寧澤奥洼,帶...
    沈念sama閱讀 33,624評(píng)論 4 322
  • 正文 年R本政府宣布,位于F島的核電站晚胡,受9級(jí)特大地震影響灵奖,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜估盘,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,209評(píng)論 3 307
  • 文/蒙蒙 一瓷患、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧遣妥,春花似錦擅编、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,199評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至境钟,卻和暖如春锦担,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背慨削。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,418評(píng)論 1 260
  • 我被黑心中介騙來(lái)泰國(guó)打工洞渔, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人缚态。 一個(gè)月前我還...
    沈念sama閱讀 45,401評(píng)論 2 352
  • 正文 我出身青樓磁椒,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親玫芦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子浆熔,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,700評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容