模仿柳叶刀期刊文献森林图的绘制-基于R语言forestplot包
参考文献:The association between education and premature mortality in the Chinese population: a 10-year cohort study
介绍:上述文献研究了文化程度与死亡的关系,文中的图2给出了总人群、分性别、分城乡的文化程度月全死因、心血管死亡、癌症死亡、慢性呼吸系统疾病、其他死亡的森林图,如下所示:
本文目的:参考上述图片,复现上述类似的森林图,我们将生成以下森林图:
自变量:受教育程度:分为低、中、高,以高为参照
因变量:全死因、心血管死亡、癌症、糖尿病、慢性呼吸系统疾病、其他死亡
分别有全人群、男性、女性与上述死亡的森林图
森林图的绘制有三大步骤:
1是整理数据,了解上述森林图数据的整理形式,这里很关键
2是分别绘制全人群、男性、女性的森林图
3是进行拼接,输出图片
实践操作:
第一步:掌握数据的整理形式,分别整理三个csv表,分别代表全人群、男性、女性森林图的原始数据。整理的数据情况如下:
第二步:绘制全人群、男性、女性的森林图
.libPaths()
.libPaths("D:/Program File/R/R-4.3.2/library")
/03R语言图形绘制/14森林图")#设置工作空间getwd()#加载工作空间 ##画森林图的包library(forestplot)library(stringr)
###############画森林图:全人群##################
result<-read.csv("fiture1_all.csv",
as.is = TRUE,header = T,sep = ",", fileEncoding='utf-8')
result<- rbind(c("Outcomes", NA, NA, NA, "HR(95%CI)","P","Death(n)"),result)
p1<-forestplot(result[,c(1,7,5)],
mean=result[,2],
lower=result[,3],
upper=result[,4],
zero=1,
boxsize=0.2,
graph.pos=3,
hrzl_lines=list("1" = gpar(lty=1,lwd=2),
"2" = gpar(lty=2),
"26"= gpar(lwd=2,lty=1,columns=c(1:4)) ),
graphwidth = unit(.25,"npc"),
xticks=c(0.5,1,2,3,4,5),
is.summary=c(T,
T,F,F,F,
T,F,F,F,
T,F,F,F,
T,F,F,F,
T,F,F,F,
T,F,F,F),
txt_gp=fpTxtGp(
label=gpar(cex=1),
ticks=gpar(cex=0.8),
xlab=gpar(cex=1),
title=gpar(cex=1.5)),
lwd.zero=1,
xlab="Hazard ratio",
lwd.xaxis = 1,
title="Total",
col=fpColors(box ='black',
lines ='black',
zero = "black"),
fontsize = 10,
lwd.ci=1,
lty.ci=1,
ci.vertices =T,
ci.vertices.height=0.1,
clip=c(0.1,8),
ineheight=unit(8, 'mm'),
line.margin=unit(8, 'mm'),
colgap=unit(6, 'mm')
)
p1
######森林图-男############
result<-read.csv("fiture1_m.csv",
as.is = TRUE,header = T,sep = ",", fileEncoding='utf-8')
result<- rbind(c("Outcomes", NA, NA, NA, "HR(95%CI)","P","Death(n)"),result)
p2<-forestplot(result[,c(1,7,5)],
mean=result[,2],
lower=result[,3],
upper=result[,4],
zero=1,
boxsize=0.2,
graph.pos=3,
hrzl_lines=list("1" = gpar(lty=1,lwd=2),
"2" = gpar(lty=2),
"26"= gpar(lwd=2,lty=1,columns=c(1:4)) ),
graphwidth = unit(.25,"npc"),
xticks=c(0.5,1,2,3,4,5),
is.summary=c(T,
T,F,F,F,
T,F,F,F,
T,F,F,F,
T,F,F,F,
T,F,F,F,
T,F,F,F),
txt_gp=fpTxtGp(
label=gpar(cex=1),
ticks=gpar(cex=0.8),
xlab=gpar(cex=1),
title=gpar(cex=1.5)),
lwd.zero=1,
xlab="Hazard ratio",
lwd.xaxis = 1,
title="Male",
col=fpColors(box ='black',
lines ='black',
zero = "black"),
fontsize = 10,
lwd.ci=1,
lty.ci=1,
ci.vertices =T,
ci.vertices.height=0.1,
clip=c(0.1,8),
ineheight=unit(8, 'mm'),
line.margin=unit(8, 'mm'),
colgap=unit(6, 'mm')
)
p2
##########森林图-女##################
result<-read.csv("fiture1_f.csv",
as.is = TRUE,header = T,sep = ",", fileEncoding='utf-8')
result<- rbind(c("Outcomes", NA, NA, NA, "HR(95%CI)","P","Death(n)"),result)
p3<-forestplot(result[,c(1,7,5)],
mean=result[,2],
lower=result[,3],
upper=result[,4],
zero=1,
boxsize=0.2,
graph.pos=3,
hrzl_lines=list("1" = gpar(lty=1,lwd=2),
"2" = gpar(lty=2),
"26"= gpar(lwd=2,lty=1,columns=c(1:4)) ),
graphwidth = unit(.25,"npc"),
xticks=c(0.5,1,2,3,4,5),
is.summary=c(T,
T,F,F,F,
T,F,F,F,
T,F,F,F,
T,F,F,F,
T,F,F,F,
T,F,F,F),
txt_gp=fpTxtGp(
label=gpar(cex=1),
ticks=gpar(cex=0.8),
xlab=gpar(cex=1),
title=gpar(cex=1.5)),
lwd.zero=1,
xlab="Hazard ratio",
lwd.xaxis = 1,
title="Female",
col=fpColors(box ='black',
lines ='black',
zero = "black"),
fontsize = 10,
lwd.ci=1,
lty.ci=1,
ci.vertices =T,
ci.vertices.height=0.1,
clip=c(0.1,8),
ineheight=unit(8, 'mm'),
line.margin=unit(8, 'mm'),
colgap=unit(6, 'mm')
)
p3
第三步:进行拼接,输出图片
############进行拼接,输出图片:p1 p2 p3合并##########
####forestplot森林图的拼接
##先转化为ggplot能拼的
library(ggplotify)
library(patchwork)
p11 <- grid2grob(print(p1))
p21 <- grid2grob(print(p2))
p31 <- grid2grob(print(p3))
###然后再拼接
library(cowplot)
# nrow:几行 ncol:几列
fiture1 <- plot_grid(p11,p21,p31, nrow=1,ncol = 3)
fiture1
##################保存fiture1
#保存其他包图片
library(Cairo)
# Cairo.capabilities() # 检查当前电脑所支持的格式
# 1. 创建画布
Cairo::CairoPNG(
filename = "fiture1.png", # 文件名称
width = 20, # 宽
height = 12, # 高
units = "in", # 单位
dpi = 300) # 分辨率
# 2. 绘图
fiture1
# 3. 关闭画布
dev.off()
结果图如下:
本文的数据如下:求关注