forestplot.default 中的错误:您在 mean 参数中提供了 2 行,而标签有 34 行

Error in forestplot.default: You have provided 2 rows in your mean argument while the labels have 34 rows

我正在尝试在 R 中制作森林图。

本站提供的样本:https://designdatadecisions.wordpress.com/2016/07/02/forest-plot-with-horizontal-bands/

从读取 R 中的数据开始。

格式化以便列标签和列匹配

subgps <- c(4,5,8,9,12,13,16,17,20,21,24,25,28,29,32,33)
data$Variable[subgps] <- paste("  ",data$Variable[subgps]) 
np <- ifelse(!is.na(data$Count), paste(data$Count," (",data$Percent,")",sep=""), NA)
tabletext <- cbind(c("Subgroup","\n",data$Variable), 
                    c("No. of Patients (%)","\n",np), 
                    c("4-Yr Cum. Event Rate\n PCI","\n",data$PCI.Group), 
                    c("4-Yr Cum. Event Rate\n Medical Therapy","\n",data$Medical.Therapy.Group), 
                    c("P Value","\n",data$P.Value))

到目前为止没有错误。

在我尝试执行第二步后,出现错误。 第二步如下:

library(forestplot)
png(file.path(workdir,"Figures\Forestplot.png"),width=960, height=640)
forestplot(labeltext=tabletext, graph.pos=3, 
           mean=c(NA,NA,data$Point.Estimate), 
           lower=c(NA,NA,data$Low), upper=c(NA,NA,data$High),
           title="Hazard Ratio",
           xlab="     <---PCI Better---    ---Medical Therapy Better--->",
           hrzl_lines=list("3" = gpar(lwd=1, col="#99999922"), 
                          "7" = gpar(lwd=60, lineend="butt", columns=c(2:6), col="#99999922"),
                          "15" = gpar(lwd=60, lineend="butt", columns=c(2:6), col="#99999922"),
                          "23" = gpar(lwd=60, lineend="butt", columns=c(2:6), col="#99999922"),
                          "31" = gpar(lwd=60, lineend="butt", columns=c(2:6), col="#99999922")),
           txt_gp=fpTxtGp(label=gpar(cex=1.25),
                              ticks=gpar(cex=1.1),
                              xlab=gpar(cex = 1.2),
                              title=gpar(cex = 1.2)),
           col=fpColors(box="black", lines="black", zero = "gray50"),
           zero=1, cex=0.9, lineheight = "auto", boxsize=0.5, colgap=unit(6,"mm"),
           lwd.ci=2, ci.vertices=TRUE, ci.vertices.height = 0.4)
dev.off()

错误:

Error in forestplot.default(labeltext = tabletext, graph.pos = 3, mean = c(NA, : You have provided 2 rows in your mean argument while the labels have 34 rows

这到底是什么意思?如何解决?

言出必行。来自 forestplot 的文档:

mean: A vector or a matrix with the averages. You can also provide a 2D/3D matrix that is automatically converted to the lower/upper parameters.

您是否检查过 data 以确保导入正确?如果您从博客上的 link 下载它,它是一个 .xlsx 文件。

例如,当您尝试 data$Point.Estimate 时,您看到了什么?

如果它是 NULL(我怀疑是),然后尝试使用 openxlsx 包下载它:

install.packages("openxlsx")
library(openxlsx)
data <- read.xlsx("ForestPlotData.csv")
# Presuming you downloaded the file correctly from the url, and its
# in your working directory, and titled "ForestPlotData.csv"