收藏贴-森林图绘图R包汇总

2022-03-29 08:15:25 浏览数 (1)

导语

GUIDE ╲

森林图是以统计指标和统计分析方法为基础,用数值运算结果绘制出的图型。用以综合展示每个被纳入研究的效应量以及汇总的合并效应量。

背景介绍

森林图是可视化meta分析结果最常用的图形,森林图展示了单个研究和Meta分析的效应估计值及可信区间。每个研究都由位于干预效果点估计值位置的方块来代表,同时一条横线分别向该方块的两边延伸出去。方块的面积代表在Meta分析中该研究被赋予的权重,而横线代表可信区间(通常为95%可信区间)。方块面积和可信区间传达的信息是相似的,但在森林图中两者的作用却不同。可信区间描述的是与研究结果相符的干预效果的范围,且能表示每个研究是否有统计学意义。今天小编给大家汇总了在R语言中绘制森林图常用到的多个工具包,接下来让我们一起看看吧!

可视化介绍

01

forestploter

代码语言:javascript复制
library(grid)
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))
# NA 为空白或 NA 将转换为字符
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
# 为森林图添加空白列以显示 CI 
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))
head(dt)

上面是森林图绘制的基本数据格式,接下来是绘图过程!

代码语言:javascript复制
# 设置主题
tm <- forest_theme(base_size = 10,
                   refline_col = "red",
                   footnote_col = "#636363",
                   footnote_fontface = "italic")
##参数设置
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 = "This is the demo data. Please feel free to changenanything you want.",
            theme = tm)
plot(p)

02

forestplot

代码语言:javascript复制
library(forestplot)
library(dplyr)
# 来自“rmeta”包的 Cochrane 数据
cochrane_from_rmeta <- structure(list(mean  = c(NA, NA, 0.578, 0.165, 0.246, 0.700, 0.348, 0.139, 1.017, NA, 0.531), 
                                      lower = c(NA, NA, 0.372, 0.018, 0.072, 0.333, 0.083, 0.016, 0.365, NA, 0.386),
                                      upper = c(NA, NA, 0.898, 1.517, 0.833, 1.474, 1.455, 1.209, 2.831, NA, 0.731)),
                                 .Names = c("mean", "lower", "upper"), 
                                 row.names = c(NA, -11L), 
                                 class = "data.frame")
#数据处理
tabletext <- cbind(c("", "Study", "Auckland", "Block", "Doran", "Gamsu", "Morrison", "Papageorgiou", "Tauesch", NA, "Summary"),
                   c("Deaths", "(steroid)", "36", "1", "4", "14", "3", "1", "8", NA, NA),
                   c("Deaths", "(placebo)", "60", "5", "11", "20", "7", "7", "10", NA, NA),
                   c("", "OR", "0.58", "0.16", "0.25", "0.70", "0.35", "0.14", "1.02", NA, "0.53"))

cochrane_from_rmeta %>% 
  forestplot(labeltext = tabletext, 
             is.summary = c(rep(TRUE, 2), rep(FALSE, 8), TRUE),
             clip = c(0.1, 2.5), 
             xlog = TRUE, 
             col = fpColors(box = "royalblue",
                            line = "darkblue",
                            summary = "royalblue"))

03

forplo

代码语言:javascript复制
exdf <- cbind(OR=c(1.21,0.90,1.02,
                   1.54,1.32,0.79,1.38,0.85,1.11,
                   1.58,1.80,2.27),
              LCI=c(0.82,0.61,0.66,
                    1.08,0.91,0.48,1.15,0.39,0.91,
                    0.99,1.48,0.92),
              UCI=c(1.79,1.34,1.57,
                    2.19,1.92,1.32,1.64,1.87,1.34,
                    2.54,2.19,5.59),
              groups=c(1,1,1,
                       2,2,2,2,2,2,
                       3,3,3))
exdf <- data.frame(exdf)
rownames(exdf) <- c('Barry, 2005', 'Frances, 2000', 'Rowley, 1995',
                    'Biro, 2000', 'Crowe, 2010', 'Harvey, 1996',
                    'Johns, 2004', 'Parr, 2002', 'Zhang, 2011',
                    'Flint, 1989', 'Mac Vicar, 1993', 'Turnbull, 1996')
knitr::kable(exdf)
代码语言:javascript复制
forplo(exdf[,1:3],
       groups=exdf$groups,
       grouplabs=c('Low risk of bias',
                   'Some concerns',
                   'High risk of bias'))

04

metaviz

代码语言:javascript复制
library(metaviz)
viz_forest(x = mozart[1:10, c("d", "se")], study_labels = mozart[1:10, c("study_name")],
           summary_label = "Summary effect", xlab = "Cohen d")
代码语言:javascript复制
viz_forest(x = mozart[1:10, c("d", "se")], study_labels = mozart[1:10, c("study_name")],
           summary_label = "Summary effect", xlab = "Cohen d", variant = "rain")

05

ggforestplot

代码语言:javascript复制
library(ggforestplot)
library(tidyverse)

# 数据集
df <-
  ggforestplot::df_linear_associations %>%
  filter(
    trait == "BMI",
    dplyr::row_number() <= 30
  )

# 绘制横截面线性关联的森林图
ggforestplot::forestplot(
  df = df,
  name = name,
  estimate = beta,
  se = se
)

06

survminer

代码语言:javascript复制
library(survminer)
library(survival)

计算 Cox 模型再绘图

代码语言:javascript复制
res.cox <- coxph(Surv(time, status) ~ ph.karno * age, data=lung)
summary(res.cox, conf.int = FALSE)
##画图ggforest()
ggforest(res.cox, data = lung)

小编总结

R语言有许多工具可以快速的绘制森林图,今天小编一次性总结来分享给大家!大家可以选择合适的包去画自己的森林图哦!

END

0 人点赞