技术文章:用R语言中的forestplot包绘制森林图的用法
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)最后以菱形所在位置代表总体的评价结果。具体数据具体分析哈!

图片新闻
最新活动更多
-
6月13日立即参评>> 【评选】维科杯·OFweek2025中国工业自动化及数字化行业年度评选
-
7.30-8.1火热报名中>> 全数会2025(第六届)机器人及智能工厂展
-
7月30-31日报名参会>>> 全数会2025中国激光产业高质量发展峰会
-
即日-2025.8.1立即下载>> 《2024智能制造产业高端化、智能化、绿色化发展蓝皮书》
-
精彩回顾立即查看>> 宾采尔激光焊接领域一站式应用方案在线研讨会
-
精彩回顾立即查看>> 【在线会议】安森美Hyperlux™ ID系列引领iToF技术革新
推荐专题
- 1 智造赋能,生态共融:大联大携手产业伙伴,共绘新质工业宏伟蓝图
- 2 开目新一代MOM:AI赋能高端制造的破局之道
- 3 谁将成为行业榜样?维科杯·OFweek2025中国工业自动化及数字化行业年度评选等您来参与!
- 4 【重磅来袭】6月17日上海见!全球智造巨头聚首,60余场前沿论坛,制造业数字化盛会邀您共启数智破局之旅!
- 5 当数智工业邂逅大湾区,看PHIIDF2025如何破界,链动全球!
- 6 世界计量日盛会启幕,聚焦测量体系变革:质量、效率与动力
- 7 安森美正式参评“维科杯·OFweek2025中国自动化领域年度最具竞争力创新产品奖”
- 8 iEi威强电邀您共襄AIAE Expo 2025北京国际工业自动化盛会,探索智能工业新边界!
- 9 展会首日速递 | iEi威强电亮相北京AIAE Expo 2025,客户云集共话智能工业未来
- 10 欧姆龙正式参评“维科杯·OFweek 2025中国工业自动化与数字化行业优秀产品奖”
发表评论
请输入评论内容...
请输入评论/评论长度6~500个字
暂无评论
暂无评论