【R语言】--- 森林图
2021-10-05 本文已影响0人
生态数据
基本简介
森林图(forest plot),一般是指在平面直角坐标系中,以一条垂直于X轴的无效线(通常坐标X=1或0)为中心,用若干条平行于X轴的线段,来表示每个研究的效应量大小及其95%可信区间,并用一个棱形来表示多个研究合并的效应量及可信区间,它是Meta分析中最常用的结果综合表达形式。实际上,除了Meta分析,森林图还有很多用处。森林图可以直观的反映出效应量(例如RR、OR、HR或者WMD)大小及其95% CI等。
基本用法
本篇文章利用ggplot2包进行绘制森林图(严格意义上应该叫meta分析效应量图),关于ggplot2的基本用法这里不在叙述。
示例
这里想用发表在SBB上的一篇meta分析文章(https://doi.org/10.1016/j.soilbio.2020.108118)中的图作为模板,进行绘制。
#清空工作台
rm(list=ls())
## 加载程序包
library(ggplot2)#作图
library(ggthemes)#主题
##建立数据集
dataset <- data.frame(
Varnames = c("Tropical forest","Temperate forest","Boreal forest","All forest","Cropland","Grassland","Overall"),
lnRR = c(0.9, 2, -0.3, 0.4, 0.5, -1.3, 2.3),
Lower=c(0.55, 1.79, -0.08, 0.2, 0.28, -1.05, 2.01),
Upper=c(1.05, 2.21, -0.42, 0.6, 0.62, -1.45, 2.5))
#转换为因子,设置排列顺序使用
dataset$Varnames<-factor(dataset$Varnames,levels= c("Tropical forest","Temperate forest","Boreal forest","All forest","Cropland","Grassland","Overall"))
#查看数据集
dataset
##作图
a<-ggplot(dataset, aes(lnRR, Varnames))+
geom_point(size=5,color = "orange")+
geom_errorbarh(aes(xmax =Upper, xmin = Lower),size= 1,height = 0.1, colour = "orange") +
scale_x_continuous(limits= c(-2.5, 2.5))+
geom_vline(aes(xintercept = 0),color="gray",linetype="dashed", size = 1.5) +
xlab('Effect size (lnRR) ')+
ylab(' ')+
theme_few()+
theme(axis.text.x = element_text(size = 14, color = "black"))+
theme(axis.text.y = element_text(size = 14, color = "black"))+
theme(title=element_text(size=14))+
geom_hline(aes(yintercept = 6.5), colour = "gray",
linetype = "dashed", size = 0.5)+#画横线
geom_hline(aes(yintercept = 4.5), colour = "gray",
linetype = "dashed", size = 0.5)+#画横线
geom_hline(aes(yintercept = 3.5), colour = "gray",
linetype = "dashed", size = 0.5)+#画横线
annotate("text", label = "QM=5.85, P=0.018",###添加注释文字1
x = 1.69, y = 3.3, size =4.5, colour = "black")+
annotate("text", label = "QM=0.85, P<0.01",###添加注释文字2
x = 1.69, y = 4.3, size =4.5, colour = "black")+
annotate("text", label = "QM=5.85, P=0.000",###添加注释文字3
x = 1.6, y = 6.3, size =4.5, colour = "black")+
annotate("text", label = "QM=10.88, P<0.0001",###添加注释文字4
x = 1.49, y = 7.4, size =4.5, colour = "black")+
annotate("text", label = "***",###添加注释文字1
x = 1.3, y = 0.98, size =6, colour = "black")+
annotate("text", label = "***",###添加注释文字2
x = 1.55, y = 1.95, size =6, colour = "black")+
annotate("text", label = "***",###添加注释文字3
x = -0.7, y = 2.95, size =6, colour = "black")+
annotate("text", label = "***",###添加注释文字4
x = 0.9, y = 3.95, size =6, colour = "black")+
annotate("text", label = "***",###添加注释文字5
x = 0.90, y = 4.95, size =6, colour = "black")+
annotate("text", label = "***",###添加注释文字6
x = -0.7, y = 5.95, size =6, colour = "black")+
annotate("text", label = "***",###添加注释文字7
x = 1.75, y = 6.95, size =6, colour = "black")
#查看图a
a
再把图a复制一张
b<-ggplot(dataset, aes(lnRR, Varnames))+
geom_point(size=5,color = "orange")+
geom_errorbarh(aes(xmax =Upper, xmin = Lower),size= 1,height = 0.1, colour = "orange") +
scale_x_continuous(limits= c(-2.5, 2.5))+
geom_vline(aes(xintercept = 0),color="gray",linetype="dashed", size = 1.5) +
xlab('Effect size (lnRR) ')+
ylab(' ')+
theme_few()+
theme(axis.text.x = element_text(size = 14, color = "black"))+
theme(axis.text.y = element_text(size = 14, color = "black"))+
theme(title=element_text(size=14))+
geom_hline(aes(yintercept = 6.5), colour = "gray",
linetype = "dashed", size = 0.5)+#画横线
geom_hline(aes(yintercept = 4.5), colour = "gray",
linetype = "dashed", size = 0.5)+#画横线
geom_hline(aes(yintercept = 3.5), colour = "gray",
linetype = "dashed", size = 0.5)+#画横线
annotate("text", label = "QM=5.85, P=0.018",###添加注释文字1
x = 1.65, y = 3.3, size =4.5, colour = "black")+
annotate("text", label = "QM=0.85, P<0.01",###添加注释文字2
x = 1.69, y = 4.3, size =4.5, colour = "black")+
annotate("text", label = "QM=5.85, P=0.000",###添加注释文字3
x = 1.6, y = 6.3, size =4.5, colour = "black")+
annotate("text", label = "QM=10.88, P<0.0001",###添加注释文字4
x = 1.49, y = 7.4, size =4.5, colour = "black")+
annotate("text", label = "***",###添加注释文字1
x = 1.3, y = 0.98, size =6, colour = "black")+
annotate("text", label = "***",###添加注释文字2
x = 1.55, y = 1.95, size =6, colour = "black")+
annotate("text", label = "***",###添加注释文字3
x = -0.7, y = 2.95, size =6, colour = "black")+
annotate("text", label = "***",###添加注释文字4
x = 0.9, y = 3.95, size =6, colour = "black")+
annotate("text", label = "***",###添加注释文字5
x = 0.90, y = 4.95, size =6, colour = "black")+
annotate("text", label = "***",###添加注释文字6
x = -0.7, y = 5.95, size =6, colour = "black")+
annotate("text", label = "***",###添加注释文字7
x = 1.75, y = 6.95, size =6, colour = "black")
#拼图
#加载组合多图所需包
library(patchwork)
#组合a和b两张图并分别把小图给命名a和b
c<-(a+b)+ plot_annotation(tag_levels = 'a')
#查看结果
c
参考文献
[1] https://zhuanlan.zhihu.com/p/340207465
[2] Feng, J. G., Zhu, B., 2021. Global patterns and associated drivers of priming effect in response to nutrient addition. Soil Biology and Biochemistry 153, 108118. https://doi.org/10.1016/j.soilbio.2020.108118.