0

我正在用forestplotR 中的包制作森林图

在这个博客的帮助下(https://www.r-bloggers.com/forest-plot-with-horizo​​ntal -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))

在此处输入图像描述

4

2 回答 2

0

在您提供的示例 URL 中,作者总是有 3 行被水平带覆盖。他通过使用中间的一条线并给它一条覆盖所有 3 条线的宽线来实现这一点。hrzl_lines是否可以在当前行创建一条线,因此您将始终在行之间看到它的中心。我能为您找到的唯一解决方法是在您的行下方添加一个空行Age,然后将灰色透明带添加到该行:

library("forestplot")

#Create empty df row
emptyRow <- data.frame(X1 = NA, X2 = NA, X3 = NA, X4 = NA, X5 = NA, X6 = NA, X7 = NA, X8 = NA)
names(emptyRow) <- names(data)

#Insert empty row below the 'Age' line
data <- rbind(data[c(1:2),], emptyRow, data[c(3:17),])

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))

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"),
                           "5" = gpar(lwd=50, 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()

在此处输入图像描述

于 2019-06-27T09:33:43.683 回答
0

我发现在一条线上获得灰色条纹的唯一解决方案forestplot是用grid.rect(). 一个相当耗时的选项,但是当您获得正确的坐标时才有效。

于 2019-09-08T14:21:29.487 回答