我有以下数据框 df:
df <- data.frame(
beta = c(0.45, -0.12, 0.33, -0.07, 0.21, 0.65, -0.18, 0.09),
se = c(0.05, 0.03, 0.04, 0.02, 0.06, 0.07, 0.03, 0.05),
prs_trait1 = c(
"Rose Growth Index",
"Tulip Petal Width",
"Sunflower Height",
"Daisy Bloom Count",
"Orchid Stem Thickness",
"Lily Leaf Area",
"Carnation Bud Count",
"Violet Flower Density"
),
OR_CI_text = c(
"1.57(1.46-1.70)",
"0.89(0.84-0.94)",
"1.39(1.28-1.52)",
"0.93(0.90-0.96)",
"1.23(1.15-1.32)",
"1.92(1.72-2.15)",
"0.83(0.79-0.87)",
"1.10(1.02-1.18)"
),
N = c(1200, 950, 1350, 890, 1020, 1600, 800, 1150) # Number of observations
)
我为这个森林图编写了代码:
# Create the forest plot
ggforestplot::forestplot(
df = df,
name = trait,
estimate = beta,
se = SE,
xlab = "Odds ratio (95% CI)",
title = NULL,
grid = FALSE
)
现在,我想在这个森林图的右侧添加 OR_CI_text,而特征和 N 则位于左侧,如附图所示:
如何调整我的 R 代码?谢谢!
我绘制了很多 OR 和文本,并且我总是发现基本 R 绘图可以让您更精细地控制所有内容的确切位置。
首先,虽然您可以将 beta 和 SE 值转换为 OR 和置信区间,但我只是从
df$OR_CI_text
中的文本值中提取它们,并将它们保存在名为 plotvals
: 的参考矩阵中
plotvals <- do.call(rbind,
lapply(regmatches(df$OR_CI_text, gregexpr("([0-9.]+)", df$OR_CI_text)),
as.numeric))
# [,1] [,2] [,3]
# [1,] 1.57 1.46 1.70
# [2,] 0.89 0.84 0.94
# [3,] 1.39 1.28 1.52
# [4,] 0.93 0.90 0.96
# [5,] 1.23 1.15 1.32
# [6,] 1.92 1.72 2.15
# [7,] 0.83 0.79 0.87
# [8,] 1.10 1.02 1.18
现在我们可以手动构建绘图了。如果您不熟悉基本 R 绘图,我建议您逐行运行此命令以查看在我们构建绘图时每个命令添加到绘图中的内容:
# Set some global paramters
seqx <- 2^c(-3:3) # OR axis
yy <- seq_len(nrow(df)) # placement of graphics and text on y axis
# Initiate blank plot
plot(x = log(range(0.005, 1e2)),
y = c(0.9, nrow(df)+0.1),
type = "n", axes = FALSE,
xlab = NA, ylab = NA)
# Add in axes and labels
axis(1, at = log(seqx), labels = seqx)
axis(3, at = log(seqx), labels = seqx)
mtext(side = 1, "Odds Ratio", at = 0, padj = 4)
mtext(side = 3, "Odds Ratio", at = 0, padj = -4)
abline(v = log(1), lty = 3, lwd = 0.75) # reference line
# Add in OR and CI points and lines
segments(x0 = log(plotvals[,2]),
x1 = log(plotvals[,3]),
y0 = yy)
points(x = log(plotvals[,1]),
y = yy, pch = 22, bg = "maroon")
# Add in text
text(df$prs_trait1, x = log(2^-5), y = yy, pos = 2, xpd = TRUE) # covariate
text(df$N, x = log(2^-4), y = yy, pos = 2, xpd = TRUE) # n
text(df$OR_CI_text, x = log(2^6), y = yy, pos = 2) # OR (CI)
text(c("Covariate", "n", "OR (95% CI)"), # top labels
x = log(2 ^ c(-5, -4, 6)),
y = max(yy) + 1,
xpd = TRUE, pos = 2)
最终情节如下:
这是一个相对基本的绘图 - 当然,您可以使用不同的参数来将其微调到您想要的样子。祝你好运!