如何在森林图上添加透明的灰色条纹?
How to add transparent grey stripes on forest plot?
我正在用 R
中的 forestplot
包制作森林图
在这个博客(https://www.r-bloggers.com/forest-plot-with-horizontal-bands/)的帮助下,我勉强做出了森林图。但是,在 forestplot
包中,hzrl_lines
语句没有如我所愿地工作。
我想在我的森林图上制作透明的灰色条纹,与文字重叠。但是在 hzrl_lines
语句中,它只覆盖了两行,而不是一行。例如,在下图中,我想覆盖 'age',即类别标题。
hrzl_lines=list("4" = gpar(lwd=60, lineend="butt", columns=c(1:5), col="#99999922"))
如您所见,横线覆盖了 'primary analysis' 和 'age' 类别标题。该线的中心位于主要分析和年龄标题之间。
如何覆盖唯一的'Age'标题?我使用了下面的代码。非常感谢您的帮助。
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()
此外,原始数据如下。 Whosebug上传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))
在您提供的示例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()
我发现在使用 forestplot
的一行上获得灰色条纹的唯一解决方案是使用 grid.rect()
绘制条纹。一个相当耗时的选项,但在您获得正确的坐标时有效。
我正在用 R
中的forestplot
包制作森林图
在这个博客(https://www.r-bloggers.com/forest-plot-with-horizontal-bands/)的帮助下,我勉强做出了森林图。但是,在 forestplot
包中,hzrl_lines
语句没有如我所愿地工作。
我想在我的森林图上制作透明的灰色条纹,与文字重叠。但是在 hzrl_lines
语句中,它只覆盖了两行,而不是一行。例如,在下图中,我想覆盖 'age',即类别标题。
hrzl_lines=list("4" = gpar(lwd=60, lineend="butt", columns=c(1:5), col="#99999922"))
如您所见,横线覆盖了 'primary analysis' 和 'age' 类别标题。该线的中心位于主要分析和年龄标题之间。
如何覆盖唯一的'Age'标题?我使用了下面的代码。非常感谢您的帮助。
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()
此外,原始数据如下。 Whosebug上传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))
在您提供的示例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()
我发现在使用 forestplot
的一行上获得灰色条纹的唯一解决方案是使用 grid.rect()
绘制条纹。一个相当耗时的选项,但在您获得正确的坐标时有效。