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

## 加载需要用到的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

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()) 输出结果 ## 小图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())

## 小图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())

## 最后是拼图

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)

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

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

|
4月前
|

533 0
|
4月前
|

75 0
|
7月前
|

74 0
|
7月前
R语言ggplot2做表格，很好看
R语言ggplot2做表格，很好看
59 0
|
7月前
|

52 0
|
7月前
|

72 0
|
7月前
|

72 0
|
7月前
|

63 0
|
7月前
|

45 0
|
7月前
|

167 0