首页
/ Forestploter项目入门指南:绘制专业森林图的技术详解

Forestploter项目入门指南:绘制专业森林图的技术详解

2025-06-08 12:50:15作者:凤尚柏Louis

概述

森林图(Forest Plot)是医学研究和元分析中常用的数据可视化工具,用于展示效应量及其置信区间。forestploter是一个专门用于绘制森林图的R包,相比其他类似工具(如forestplot、meta等),它具有以下独特优势:

  1. 将森林图视为表格处理,用户可以完全控制内容和显示方式
  2. 通过主题系统统一控制图形参数
  3. 支持绘制后编辑修改
  4. 支持多列置信区间和分组显示

基础森林图绘制

数据准备

绘制森林图的第一步是准备数据框,其中需要包含以下关键元素:

library(forestploter)

# 示例数据准备
dt <- read.csv(system.file("extdata", "example_data.csv", package = "forestploter"))
dt <- dt[, 1:6]

# 处理子组缩进
dt$Subgroup <- ifelse(is.na(dt$Placebo), 
                     dt$Subgroup,
                     paste0("   ", dt$Subgroup))

# 处理缺失值
dt$Treatment <- ifelse(is.na(dt$Treatment), "", dt$Treatment)
dt$Placebo <- ifelse(is.na(dt$Placebo), "", dt$Placebo)

# 计算标准误
dt$se <- (log(dt$hi) - log(dt$est)) / 1.96

# 添加空白列用于绘制置信区间
dt$` ` <- paste(rep(" ", 20), collapse = " ")

# 创建置信区间显示列
dt$`HR (95% CI)` <- ifelse(is.na(dt$se), "",
                          sprintf("%.2f (%.2f to %.2f)",
                                  dt$est, dt$low, dt$hi))

基本绘图函数

使用forest()函数绘制基础森林图:

p <- forest(dt[, c(1:3, 8:9)],
           est = dt$est,
           lower = dt$low, 
           upper = dt$hi,
           sizes = dt$se,
           ci_column = 4,
           ref_line = 1,
           arrow_lab = c("Placebo Better", "Treatment Better"),
           xlim = c(0, 4),
           ticks_at = c(0.5, 1, 2, 3),
           footnote = "示例数据说明")

plot(p)

关键参数说明:

  • est: 点估计值
  • lower/upper: 置信区间上下限
  • ci_column: 指定绘制置信区间的列
  • ref_line: 参考线位置
  • arrow_lab: 箭头标签
  • xlim: x轴范围
  • ticks_at: x轴刻度位置

主题定制

forestploter提供了灵活的主题系统,可以自定义各种图形元素:

# 定义主题
tm <- forest_theme(base_size = 10,
                  ci_pch = 15,          # 置信区间点形状
                  ci_col = "#762a83",   # 置信区间颜色
                  ci_fill = "black",    # 置信区间填充色
                  ci_lty = 1,           # 置信区间线型
                  ci_lwd = 1.5,         # 置信区间线宽
                  refline_gp = gpar(lwd = 1, lty = "dashed", col = "grey20"),
                  vertline_col = "grey20",
                  summary_fill = "#4575b4",  # 汇总统计填充色
                  footnote_gp = gpar(cex = 0.6, fontface = "italic"))

# 应用主题绘图
pt <- forest(dt_tmp[, c(1:3, 8:9)],
            theme = tm)

文本对齐与背景设置

通过主题系统可以控制文本对齐方式和单元格背景:

# 右对齐文本
tm <- forest_theme(core = list(fg_params = list(hjust = 1, x = 0.9),
                              bg_params = list(fill = c("#edf8e9", "#c7e9c0"))),
                  colhead = list(fg_params = list(hjust = 0.5, x = 0.5)))

# 混合对齐方式
tm <- forest_theme(core = list(fg_params = list(hjust = c(1, 0, 0.5),
                               x = c(0.9, 0.1, 0.5))))

多列置信区间

forestploter支持在同一图中绘制多列置信区间:

# 准备多组数据
dt$`CVD outcome` <- paste(rep(" ", 20), collapse = " ")
dt$`COPD outcome` <- paste(rep(" ", 20), collapse = " ")

# 绘制多列置信区间
p <- forest(dt[, c(1, 19, 23, 21, 20, 24, 22)],
           est = list(dt$est_gp1, dt$est_gp2, dt$est_gp3, dt$est_gp4),
           lower = list(dt$low_gp1, dt$low_gp2, dt$low_gp3, dt$low_gp4), 
           upper = list(dt$hi_gp1, dt$hi_gp2, dt$hi_gp3, dt$hi_gp4),
           ci_column = c(4, 7))

高级技巧:自定义置信区间

forestploter允许用户自定义置信区间的绘制方式:

# 使用内置箱线图函数
p <- forest(dat[, c(1, 7, 2:6)],
           est = list(dat$med),
           lower = list(dat$min), 
           upper = list(dat$max),
           fn_ci = make_boxplot,
           ci_column = 2,
           index_args = c("q1", "q3"))

总结

forestploter提供了高度灵活的森林图绘制功能,主要特点包括:

  1. 表格化的布局控制
  2. 丰富的主题定制选项
  3. 多列置信区间支持
  4. 绘制后编辑能力
  5. 自定义图形元素能力

通过合理使用这些功能,用户可以创建出满足各种专业需求的森林图,适用于医学研究、元分析等多种场景。

登录后查看全文
热门项目推荐

项目优选

收起
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
179
263
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
869
514
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
130
183
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
295
331
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
333
1.09 K
harmony-utilsharmony-utils
harmony-utils 一款功能丰富且极易上手的HarmonyOS工具库,借助众多实用工具类,致力于助力开发者迅速构建鸿蒙应用。其封装的工具涵盖了APP、设备、屏幕、授权、通知、线程间通信、弹框、吐司、生物认证、用户首选项、拍照、相册、扫码、文件、日志,异常捕获、字符、字符串、数字、集合、日期、随机、base64、加密、解密、JSON等一系列的功能和操作,能够满足各种不同的开发需求。
ArkTS
18
0
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.08 K
0
kernelkernel
deepin linux kernel
C
22
5
WxJavaWxJava
微信开发 Java SDK,支持微信支付、开放平台、公众号、视频号、企业微信、小程序等的后端开发,记得关注公众号及时接受版本更新信息,以及加入微信群进行深入讨论
Java
829
22
cherry-studiocherry-studio
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
601
58