forestplot(tabletext, graph.pos =4, hrzl_lines =list("3"=gpar(lty=2), "11" =gpar(lwd=1, columns=c(1:3,5), col ="#000044"), "12" =gpar(lwd=1, lty=2, columns=c(1:3,5), col ="#000044")), cochrane_from_rmeta,new_page =TRUE, is.summary=c(TRUE,TRUE,rep(FALSE,8),TRUE), clip=c(0.1,2.5), xlog=TRUE, col=fpColors(box="royalblue",line="darkblue",summary="royalblue",hrz_lines ="red"))
# 添加标题forestplot(tabletext, graph.pos =4, title="Hazard Ratio", hrzl_lines =list("3"=gpar(lty=2), "11" =gpar(lwd=1, columns=c(1:3,5), col ="#000044"), "12" =gpar(lwd=1, lty=2, columns=c(1:3,5), col ="#000044")), cochrane_from_rmeta,new_page =TRUE, is.summary=c(TRUE,TRUE,rep(FALSE,8),TRUE), clip=c(0.1,2.5), xlog=TRUE, col=fpColors(box="royalblue",line="darkblue",summary="royalblue",hrz_lines ="#444444"))
# 定义x轴forestplot(tabletext, graph.pos =4, title="Hazard Ratio", hrzl_lines =list("3"=gpar(lty=2), "11" =gpar(lwd=1, columns=c(1:3,5), col ="#000044"), "12" =gpar(lwd=1, lty=2, columns=c(1:3,5), col ="#000044")), cochrane_from_rmeta,new_page =TRUE, is.summary=c(TRUE,TRUE,rep(FALSE,8),TRUE), xlab=" <---PCI Better--- ---Medical Therapy Better--->", clip=c(0.1,2.5), xlog=TRUE, col=fpColors(box="royalblue",line="darkblue",summary="royalblue",hrz_lines ="#444444"))
# 取消对头2行和最后1行字体的特殊设置forestplot(tabletext, graph.pos =4, title="Hazard Ratio", hrzl_lines =list("3"=gpar(lty=2), "11" =gpar(lwd=1, columns=c(1:3,5), col ="#000044"), "12" =gpar(lwd=1, lty=2, columns=c(1:3,5), col ="#000044")), cochrane_from_rmeta,new_page =TRUE, is.summary=c(rep(FALSE,11)), xlab=" <---PCI Better--- ---Medical Therapy Better--->", clip=c(0.1,2.5), xlog=TRUE, zero=1, col=fpColors(box="royalblue",line="darkblue",summary="royalblue",hrz_lines ="#444444"))
示例代码②:需要定义亚组的数据
### 准备数据
library(forestplot)#数据来源:https://www.r-bloggers.com/forest-plot-with-horizontal-bands/data <- read.csv("forestplotdata.csv", stringsAsFactors=FALSE)
head(data)# Variable Count Percent Point.Estimate Low High PCI.Group Medical.Therapy.Group P.Value# 1 Overall 2166 100 1.3 0.90 1.50 17.2 15.6 NA# 2 NA NA NA NA NA NA NA NA# 3 Age NA NA NA NA NA NA NA 0.05# 4 <= 65 1534 71 1.5 1.05 1.90 17.0 13.2 NA# 5 > 65 632 29 0.8 0.60 1.25 17.8 21.3 NA# 6 NA NA NA NA NA NA NA NA
### 绘制森林图
## 简单森林图
# 构建tabletext,更改列名称,将 count 和 percent 合并np <- ifelse(!is.na(data$Count), paste(data$Count," (",data$Percent,")",sep=""), NA)View(np)
# 写出将要在图中展现出来的文本tabletext <- cbind(c("Subgroup","",data$Variable), c("No. of Patients (%)","",np), c("4-Yr Cum. Event Rate PCI","",data$PCI.Group), c("4-Yr Cum. Event Rate Medical Therapy","",data$Medical.Therapy.Group), c("P Value","",data$P.Value)View(tabletext)
##绘制森林图forestplot(labeltext=tabletext, graph.pos=3, mean=c(NA,NA,data$Point.Estimate), lower=c(NA,NA,data$Low), upper=c(NA,NA,data$High), boxsize=0.5)
接下来要对森林图进行优化:
## 定义亚组subgps <- c(4,5,8,9,12,13,16,17,20,21,24,25,28,29,32,33)data$Variable[subgps] <- paste(" ",data$Variable[subgps])View(data)
png(filename = "Forestplot.png",width=960, height=640)forestplot(labeltext=tabletext, graph.pos=3, #为Pvalue箱线图所在的位置 mean=c(NA,NA,data$Point.Estimate), lower=c(NA,NA,data$Low), upper=c(NA,NA,data$High), title="Hazard Ratio Plot", #定义标题 xlab=" <---PCI Better--- ---Medical Therapy Better--->", #定义x轴 ##根据亚组的位置,设置线型,宽度造成“区块感” hrzl_lines=list("3" = gpar(lwd=1, col="black"), "7" = gpar(lwd=60, lineend="butt", columns=c(2:6), col="#99999922"), "15" = gpar(lwd=60, lineend="butt", columns=c(2:6), col="#99999922"), "23" = gpar(lwd=60, lineend="butt", columns=c(2:6), col="#99999922"), "31" = gpar(lwd=60, lineend="butt", columns=c(2:6), col="#99999922")), #fpTxtGp函数中的cex参数设置各部分字体大小 txt_gp=fpTxtGp(label=gpar(cex=1.25), ticks=gpar(cex=1.1), xlab=gpar(cex = 1.2), title=gpar(cex = 1.2)), col=fpColors(box="#1c61b6", lines="#1c61b6", zero = "gray50"), ##fpColors函数设置颜色 zero=1, #箱线图中基准线的位置 cex=0.9, lineheight = "auto", colgap=unit(8,"mm"), lwd.ci=2, boxsize=0.5, #箱子大小,线的宽度 ci.vertices=TRUE, ci.vertices.height = 0.4) #森林图可信区间两端添加小竖线,设置高度dev.off()
森林图怎么看:
(1)森林图中横短线与中线相交表示无统计学意义;
(2)95% CI上下限均>1,即在森林图中,其95% CI横线不与无效竖线相交,且该横线落在无效线右侧时,说明该指标大于竖线代表的结局;
(3)95% CI上下限均<1,即在森林图中,其95% CI横线不与无效竖线相交,且该横线落在无效线左侧时,说明该指标小于于竖线代表的结局。
(4)最后以菱形所在位置代表总体的评价结果。具体数据具体分析哈!