我正在用forestplot
R 中的包制作森林图
在这个博客的帮助下(https://www.r-bloggers.com/forest-plot-with-horizontal -bands/ ),我几乎没有制作森林图。但是,在forestplot
包中,该hzrl_lines
语句没有按我的意愿工作。
我想在我的森林图上制作透明的灰色条纹,与文本重叠。但在hzrl_lines
声明中,它只涵盖两行,而不是一行。例如,在下图中,我想覆盖“年龄”,即类别标题。
hrzl_lines=list("4" = gpar(lwd=60, lineend="butt", columns=c(1:5), col="#99999922"))
如您所见,水平线涵盖“主要分析”和“年龄”类别标题。该线的中心位于主要分析和年龄标题之间。
如何覆盖唯一的“年龄”标题?我使用了下面的代码。我真的很感谢你的帮助。
library("forestplot")
datafile <- file.path("example.csv")
data <- read.csv(datafile, stringsAsFactors=FALSE)
np <- ifelse(!is.na(data$total), data$total, NA)
nc <- ifelse(!is.na(data$intervention), data$intervention, NA)
nu <- ifelse(!is.na(data$control), data$control, NA)
tabletext <- cbind(c("Subgroup","\n",data$category),
c("No. of\nPatients","\n",np),
c("intervention","\n",np),
c("control","\n",nu),
c("Interaction\n P Value","\n",data$prob))
tabletext
names(data)
png(file.path("example.png"),width=750, height=750)
forestplot(labeltext=tabletext,
mean=c(NA,NA,data$estimate),
lower=c(NA,NA,data$lcl), upper=c(NA,NA,data$ucl),
hrzl_lines=list("3" = gpar(lwd=1, col="#000000"),
"4" = gpar(lwd=60, lineend="butt", columns=c(1:5), col="#99999922")),
xticks=c(-40,-30,-20,-10,0,10),
clip=c(-40,10),
col=fpColors(box="black", lines="black", zero = "gray50"))
dev.off()
此外,原始数据如下所示。在 stackoverflow 中很难上传 csv 文件。
> dput(data)
structure(list(category = c("Primary analysis", "Age", " 60-",
" 20-40", " -20", "Sex", " Female", " Male", "", "",
"", "", "", "", "", "", ""), total = c(100L, NA, NA, 40L, 60L,
NA, 40L, 60L, NA, NA, NA, NA, NA, NA, NA, NA, NA), intervention = c(50L,
NA, NA, 20L, 30L, NA, 20L, 30L, NA, NA, NA, NA, NA, NA, NA, NA,
NA), control = c(50L, NA, NA, 20L, 30L, NA, 20L, 30L, NA, NA,
NA, NA, NA, NA, NA, NA, NA), estimate = c(-17.5, NA, NA, -12,
-20, NA, -15, -20, NA, NA, NA, NA, NA, NA, NA, NA, NA), lcl = c(-25L,
NA, NA, -25L, -30L, NA, -22L, -35L, NA, NA, NA, NA, NA, NA, NA,
NA, NA), ucl = c(-10L, NA, NA, 1L, -10L, NA, -8L, -5L, NA, NA,
NA, NA, NA, NA, NA, NA, NA), prob = c(NA, 0.5, NA, NA, NA, 0.5,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), class = "data.frame", row.names = c(NA,
-17L))