R forestplot()函数固定截面宽度

jtw3ybtb  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(205)
    • bounty将在4天后过期**。回答此问题可获得+50声望奖励。user36196希望引起更多人关注此问题。

我想使用R中的forestplot()函数(来自R/forestplot包)生成多个森林图,并想确保我可以将每个森林图中的文本和图形部分对齐,这样它们就可以有效地显示为堆叠图,如下所示:

(取自R forestplot package blank lines with section headings
但是在每个子图中可能存在不同的尺度,在每个图中排列零效应线。
需要在forestplot()调用中更改哪些属性以确保发生这种情况?
EDIT:提供最小代码示例

library(forestplot)
library(tidyr)

cohort <- data.frame(Age = c(43, 39, 34, 55, 70, 59, 44, 83, 76, 44, 
                       75, 60, 62, 50, 44, 40, 41, 42, 37, 35, 55, 46), 
               Status = structure(c(2L, 
                                    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 
                                    1L, 1L, 1L, 2L, 2L), levels = c("-", "+"), class = "factor"), 
               Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                   1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("1","2"), class = "factor"))

age.lm <- lm(Age ~ Group, data = cohort)
status.lm <- glm(Status ~ Group, data = cohort, family=binomial(link=logit))

age.data <- summary(age.lm)$coefficients[2,]
status.data <- summary(status.lm)$coefficients[2,]

age.data <- rbind(c(0,0,0,1,"Group 1", "n=15"),
                  c(age.data[1], age.data[1]-age.data[2]*1.95, age.data[1]+age.data[2]*1.95, age.data[4], "Group 2", "n=7"))
status.data <- rbind(c(0,0,0,1,"Group 1", "[+13,-2]"),
                     c(status.data[1], status.data[1]-status.data[2]*1.95, status.data[1]+status.data[2]*1.95, status.data[4], "Group 2", "[+2,-5]"))

colnames(age.data) <- c("mean","lower","upper","p-val","labeltext","numbers")
colnames(status.data) <- c("mean","lower","upper","p-val","labeltext","numbers")

age.data <- data.frame(age.data)
status.data <- data.frame(status.data)

age.data$mean <- as.numeric(age.data$mean)
age.data$lower <- as.numeric(age.data$lower)
age.data$upper <- as.numeric(age.data$upper)

status.data$mean <- exp(as.numeric(status.data$mean))
status.data$lower <- exp(as.numeric(status.data$lower))
status.data$upper <- exp(as.numeric(status.data$upper))

age.plot <- forestplot(age.data,
           labeltext = c(labeltext,numbers),
           boxsize = 0.1,
           xlog = FALSE,
           clip=c(-20,20),
           xticks=c(-20,-10,0,10,20),
           txt_gp = fpTxtGp(ticks=gpar(cex=1)),
           align=c("l","c","l"))
           
status.plot <- forestplot(status.data,
                          labeltext = c(labeltext,numbers),
                          boxsize = 0.1,
                          xlog = TRUE,
                          clip=c(1/100,100),
                          xticks=c(log(1e-2),log(1e-1),0,log(1e1),log(1e2)),
                          txt_gp = fpTxtGp(ticks=gpar(cex=1)),
                          align=c("l","c","l"))

注意,年龄图是线性模型,状态图是逻辑模型:
第一节第一节第一节第二节第一节
我希望能够排列左侧文本和右侧图的相对大小,以便零效应线(分别位于0和1)对齐,从而使森林图整齐堆叠。

xmd2e60i

xmd2e60i1#

使用align参数,您可以左“l”对齐绘图的各个部分,因此文本可以左对齐。如果您想对齐零效应线,您可以使用mar并调整单位来调整其中一个图形。

library(forestplot)
library(tidyr)

age.plot <- forestplot(age.data,
                       labeltext = c(labeltext,numbers),
                       boxsize = 0.1,
                       xlog = FALSE,
                       clip=c(-20,20),
                       xticks=c(-20,-10,0,10,20),
                       txt_gp = fpTxtGp(ticks=gpar(cex=1)),
                       align=c("l","l","l")
                       )

status.plot <- forestplot(status.data,
                          labeltext = c(labeltext,numbers),
                          boxsize = 0.1,
                          xlog = TRUE,
                          clip=c(1/100,100),
                          xticks=c(log(1e-2),log(1e-1),0,log(1e1),log(1e2)),
                          txt_gp = fpTxtGp(ticks=gpar(cex=1)),
                          align=c("l","l","l"),
                          mar = unit(c(0,5,0,10.5), "mm")
                          )

library(grid)
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 1)))
pushViewport(viewport(layout.pos.row = 1))
plot(age.plot)
popViewport()
pushViewport(viewport(layout.pos.row = 2))
plot(status.plot)
popViewport(2)

创建于2022年12月28日,使用reprex v2.0.2

相关问题