森林圖常見于元分析,但其使用絕不僅如此,比如我現(xiàn)在想要研究的對象有諸多HR結(jié)果,我想要匯總為一張圖,森林圖就是個非常好的選擇。ggpubr包提供的森林圖是針對變量分析繪圖,我也嘗試使用了metafor包的forest畫圖函數(shù),但太靈活了,我除了感覺文檔畫的不錯,但實際使用卻很難得到想要的結(jié)果。
谷歌了一下,找到了forestplot這個包,下面根據(jù)文檔學(xué)習(xí)一波。
安裝:
install.packages("forestplot")
文本
森林圖可以與文本連接起來并自定義。
文本表
下面是一個使用文本表的例子:
library(forestplot)
#> 載入需要的程輯包:grid
#> 載入需要的程輯包:magrittr
#> 載入需要的程輯包:checkmate
# Cochrane data from the 'rmeta'-package
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"))
forestplot(tabletext,
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"))

匯總線
在上面基礎(chǔ)進(jìn)行增改:
forestplot(tabletext,
hrzl_lines = gpar(col="#444444"),
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"))

我們可以修改線條類型和它所影響的范圍:
forestplot(tabletext,
hrzl_lines = list("3" = gpar(lty=2),
"11" = gpar(lwd=1, columns=1:4, 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"))

為端點增加垂線:
forestplot(tabletext,
hrzl_lines = list("3" = gpar(lty=2),
"11" = gpar(lwd=1, columns=1:4, 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"),
vertices = TRUE)

調(diào)整圖元素的位置
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 = "#444444"))

使用表達(dá)式
data(HRQoL)
clrs <- fpColors(box="royalblue",line="darkblue", summary="royalblue")
tabletext <-
list(c(NA, rownames(HRQoL$Sweden)),
append(list(expression(beta)), sprintf("%.2f", HRQoL$Sweden[,"coef"])))
forestplot(tabletext,
rbind(rep(NA, 3),
HRQoL$Sweden),
col=clrs,
xlab="EQ-5D index")

更改字體
tabletext <- cbind(rownames(HRQoL$Sweden),
sprintf("%.2f", HRQoL$Sweden[,"coef"]))
forestplot(tabletext,
txt_gp = fpTxtGp(label = gpar(fontfamily = "HersheyScript")),
rbind(HRQoL$Sweden),
col=clrs,
xlab="EQ-5D index")

還可以更改風(fēng)格:
forestplot(tabletext,
txt_gp = fpTxtGp(label = list(gpar(fontfamily = "HersheyScript"),
gpar(fontfamily = "",
col = "#660000")),
ticks = gpar(fontfamily = "", cex=1),
xlab = gpar(fontfamily = "HersheySerif", cex = 1.5)),
rbind(HRQoL$Sweden),
col=clrs,
xlab="EQ-5D index")

置信區(qū)間
簡單的,給超出范圍的區(qū)間加箭頭(clip):
forestplot(tabletext,
rbind(HRQoL$Sweden),
clip =c(-.1, Inf),
col=clrs,
xlab="EQ-5D index")

多個置信區(qū)間范圍
這在對比時非常有用:
tabletext <- tabletext[,1]
forestplot(tabletext,
mean = cbind(HRQoL$Sweden[, "coef"], HRQoL$Denmark[, "coef"]),
lower = cbind(HRQoL$Sweden[, "lower"], HRQoL$Denmark[, "lower"]),
upper = cbind(HRQoL$Sweden[, "upper"], HRQoL$Denmark[, "upper"]),
clip =c(-.1, 0.075),
col=fpColors(box=c("blue", "darkred")),
xlab="EQ-5D index")

評估顯示器
可以用方塊、圓圈等:
forestplot(tabletext,
fn.ci_norm = c(fpDrawNormalCI, fpDrawCircleCI),
boxsize = .25, # We set the box size to better visualize the type
line.margin = .1, # We need to add this to avoid crowding
mean = cbind(HRQoL$Sweden[, "coef"], HRQoL$Denmark[, "coef"]),
lower = cbind(HRQoL$Sweden[, "lower"], HRQoL$Denmark[, "lower"]),
upper = cbind(HRQoL$Sweden[, "upper"], HRQoL$Denmark[, "upper"]),
clip =c(-.125, 0.075),
col=fpColors(box=c("blue", "darkred")),
xlab="EQ-5D index")

選擇線型
forestplot(tabletext,
fn.ci_norm = c(fpDrawNormalCI, fpDrawCircleCI),
boxsize = .25, # We set the box size to better visualize the type
line.margin = .1, # We need to add this to avoid crowding
mean = cbind(HRQoL$Sweden[, "coef"], HRQoL$Denmark[, "coef"]),
lower = cbind(HRQoL$Sweden[, "lower"], HRQoL$Denmark[, "lower"]),
upper = cbind(HRQoL$Sweden[, "upper"], HRQoL$Denmark[, "upper"]),
clip =c(-.125, 0.075),
lty.ci = c(1, 2),
col=fpColors(box=c("blue", "darkred")),
xlab="EQ-5D index")

圖例
添加一個基本圖例:
forestplot(tabletext,
legend = c("Sweden", "Denmark"),
fn.ci_norm = c(fpDrawNormalCI, fpDrawCircleCI),
boxsize = .25, # We set the box size to better visualize the type
line.margin = .1, # We need to add this to avoid crowding
mean = cbind(HRQoL$Sweden[, "coef"], HRQoL$Denmark[, "coef"]),
lower = cbind(HRQoL$Sweden[, "lower"], HRQoL$Denmark[, "lower"]),
upper = cbind(HRQoL$Sweden[, "upper"], HRQoL$Denmark[, "upper"]),
clip =c(-.125, 0.075),
col=fpColors(box=c("blue", "darkred")),
xlab="EQ-5D index")

通過設(shè)定參數(shù)可以進(jìn)一步自定義:
forestplot(tabletext,
legend_args = fpLegend(pos = list(x=.85, y=0.25),
gp=gpar(col="#CCCCCC", fill="#F9F9F9")),
legend = c("Sweden", "Denmark"),
fn.ci_norm = c(fpDrawNormalCI, fpDrawCircleCI),
boxsize = .25, # We set the box size to better visualize the type
line.margin = .1, # We need to add this to avoid crowding
mean = cbind(HRQoL$Sweden[, "coef"], HRQoL$Denmark[, "coef"]),
lower = cbind(HRQoL$Sweden[, "lower"], HRQoL$Denmark[, "lower"]),
upper = cbind(HRQoL$Sweden[, "upper"], HRQoL$Denmark[, "upper"]),
clip =c(-.125, 0.075),
col=fpColors(box=c("blue", "darkred")),
xlab="EQ-5D index")

刻度和網(wǎng)格
我們可以手動設(shè)定想要的刻度
forestplot(tabletext,
legend = c("Sweden", "Denmark"),
fn.ci_norm = c(fpDrawNormalCI, fpDrawCircleCI),
boxsize = .25, # We set the box size to better visualize the type
line.margin = .1, # We need to add this to avoid crowding
mean = cbind(HRQoL$Sweden[, "coef"], HRQoL$Denmark[, "coef"]),
lower = cbind(HRQoL$Sweden[, "lower"], HRQoL$Denmark[, "lower"]),
upper = cbind(HRQoL$Sweden[, "upper"], HRQoL$Denmark[, "upper"]),
clip =c(-.125, 0.075),
col=fpColors(box=c("blue", "darkred")),
xticks = c(-.1, -0.05, 0, .05),
xlab="EQ-5D index")

我們可以給想要的刻度加標(biāo)簽:
xticks <- seq(from = -.1, to = .05, by = 0.025)
xtlab <- rep(c(TRUE, FALSE), length.out = length(xticks))
attr(xticks, "labels") <- xtlab
forestplot(tabletext,
legend = c("Sweden", "Denmark"),
fn.ci_norm = c(fpDrawNormalCI, fpDrawCircleCI),
boxsize = .25, # We set the box size to better visualize the type
line.margin = .1, # We need to add this to avoid crowding
mean = cbind(HRQoL$Sweden[, "coef"], HRQoL$Denmark[, "coef"]),
lower = cbind(HRQoL$Sweden[, "lower"], HRQoL$Denmark[, "lower"]),
upper = cbind(HRQoL$Sweden[, "upper"], HRQoL$Denmark[, "upper"]),
clip =c(-.125, 0.075),
col=fpColors(box=c("blue", "darkred")),
xticks = xticks,
xlab="EQ-5D index")

如果圖形太高我們可能還需要增加輔助線以顯示對應(yīng)的刻度:
forestplot(tabletext,
legend = c("Sweden", "Denmark"),
fn.ci_norm = c(fpDrawNormalCI, fpDrawCircleCI),
boxsize = .25, # We set the box size to better visualize the type
line.margin = .1, # We need to add this to avoid crowding
mean = cbind(HRQoL$Sweden[, "coef"], HRQoL$Denmark[, "coef"]),
lower = cbind(HRQoL$Sweden[, "lower"], HRQoL$Denmark[, "lower"]),
upper = cbind(HRQoL$Sweden[, "upper"], HRQoL$Denmark[, "upper"]),
clip =c(-.125, 0.075),
col=fpColors(box=c("blue", "darkred")),
grid = TRUE,
xticks = c(-.1, -0.05, 0, .05),
xlab="EQ-5D index")

最后我們可以自定義想要的網(wǎng)格:
forestplot(tabletext,
legend = c("Sweden", "Denmark"),
fn.ci_norm = c(fpDrawNormalCI, fpDrawCircleCI),
boxsize = .25, # We set the box size to better visualize the type
line.margin = .1, # We need to add this to avoid crowding
mean = cbind(HRQoL$Sweden[, "coef"], HRQoL$Denmark[, "coef"]),
lower = cbind(HRQoL$Sweden[, "lower"], HRQoL$Denmark[, "lower"]),
upper = cbind(HRQoL$Sweden[, "upper"], HRQoL$Denmark[, "upper"]),
clip =c(-.125, 0.075),
col=fpColors(box=c("blue", "darkred")),
grid = structure(c(-.1, -.05, .05),
gp = gpar(lty = 2, col = "#CCCCFF")),
xlab="EQ-5D index")

下面兩種structure的書寫方式是一致的:
grid_arg <- c(-.1, -.05, .05)
attr(grid_arg, "gp") <- gpar(lty = 2, col = "#CCCCFF")
identical(grid_arg,
structure(c(-.1, -.05, .05),
gp = gpar(lty = 2, col = "#CCCCFF")))
#> [1] TRUE
文章作者 王詩翔
上次更新 2018-09-10
許可協(xié)議 CC BY-NC-ND 4.0
