为什么森林图不显示置信区间条?

Why Forest plot is not showing the confidence interval bars?

您好,我正在通过以下代码生成森林图。但我的可视化图表没有显示框的置信区间。我怎样才能改进这个图形表示。

    mydf <- data.frame(
  Variables=c('Variables','Neuroticism_2','Neuroticism_3','Neuroticism_4'),
  HazardRatio=c(NA,1.109,1.296,1.363),
  HazardLower=c(NA,1.041,1.206,1.274),
  HazardUpper=c(NA,1.182,1.393,1.458),
  Pvalue=c(NA,"0.001","<0.001","<0.001"),
  stringsAsFactors=FALSE
)

#png('temp.png', width=8, height=4, units='in', res=400)
rowseq <- seq(nrow(mydf),1)
par(mai=c(1,0,0,0))
plot(mydf$HazardRatio, rowseq, pch=15,
     xlim=c(-10,12), ylim=c(0,7),
     xlab='', ylab='', yaxt='n', xaxt='n',
     bty='n')
axis(1, seq(0,5,by=.5), cex.axis=.5)

segments(1,-1,1,6.25, lty=3)
segments(mydf$HazardLower, rowseq, mydf$HazardUpper, rowseq)

text(-8,6.5, "Variables", cex=.75, font=2, pos=4)
t1h <- ifelse(!is.na(mydf$Variables), mydf$Variables, '')
text(-8,rowseq, t1h, cex=.75, pos=4, font=3)



text(-1,6.5, "Hazard Ratio (95%)", cex=.75, font=2, pos=4)
t3 <- ifelse(!is.na(mydf$HazardRatio), with(mydf, paste(HazardRatio,' (',HazardLower,'-',HazardUpper,')',sep='')), '')
text(3,rowseq, t3, cex=.75, pos=4)

text(7.5,6.5, "P Value", cex=.75, font=2, pos=4)
t4 <- ifelse(!is.na(mydf$Pvalue), mydf$Pvalue, '')
text(7.5,rowseq, t4, cex=.75, pos=4)

#dev.off()

编辑

我什至尝试通过 forestplot 包来做到这一点。但是我没有得到关于 grpah 的置信区间,也没有像上图那样展示。

test_data <- data.frame(coef=c(1.109,1.296,1.363),
                        low=c(1.041,1.206,1.274),
                        high=c(1.182,1.393,1.458),
                        boxsize=c(0.1, 0.1, 0.1))
row_names <- cbind(c("Variable", "N_Quartile 1", "N_Quartile 2", "N_Quartile 3"),
                   c("HR", test_data$coef), c("CI -95%",  test_data$low), c("CI +95%", test_data$high) )
test_data <- rbind(NA, test_data)
forestplot(labeltext = row_names,
           mean = test_data$coef, upper = test_data$high,
           lower = test_data$low,
             clip =c(0.1, 25),
           is.summary=c(TRUE, FALSE, FALSE, FALSE),
           boxsize = test_data$boxsize,

           zero = 1,colgap = unit(3, "mm"), txt_gp=fpTxtGp(label= gpar(cex = 0.7),
                                                             title = gpar(cex = 1) ),
           xlog = TRUE,
           xlab = "HR (95% CI)",
           col = fpColors(lines="black", box="black"),
                      ci.vertices = TRUE,
           xticks = c(0.1, 1, 2.5,5,7.5))

您的间隔非常小,因此如果您在 plot 上手动执行此操作,将需要一段时间来完善正确的设置,并且将文本与其放在一起并非易事。现在你的第一个代码甚至还不到 50%。

我的建议是使用 forestplot 慢慢构建绘图,并找出问题所在,例如,如果您只是绘制 data.frame,您会看到它有效,那就是 c.i 就在那里,只是它很窄,这就是你手头的问题,使用 lwd.ci 调整大小,使其可见:

forestplot(test_data[,1:3],lwd.ci=3)

现在如果我们在文本中添加:

forestplot(
    labeltext =row_names,
    mean = test_data$coef, upper = test_data$high,
           lower = test_data$low,
    txt_gp=fpTxtGp(cex=0.8),
    is.summary=c(TRUE, FALSE, FALSE, FALSE),
    boxsize = test_data$boxsize,lwd.ci=3)        

所以文字占用有点多space,我认为一种方法是使用传统的 est[ll - ul] 表示估计和置信区间的方式,你可以看例子 here。我可以在下面尝试的一种方法是将 CI 的值包装到 1 个字符串中,并且只有两列用于文本:

library(stringr)

test_data <- data.frame(coef=c(1.109,1.296,1.363),
                        low=c(1.041,1.206,1.274),
                        high=c(1.182,1.393,1.458),
                        boxsize=c(0.1, 0.1, 0.1))

column1 = c("Variable", "N_Quartile 1", "N_Quartile 2", "N_Quartile 3")
column2 = cbind(c("HR", test_data$coef), 
           c("CI -95%",  test_data$low),
           c("CI +95%", test_data$high))

L = max(nchar(column2))
padded_text =apply(column2,1,
function(i)paste(str_pad(i,L),collapse=" "))

test_data <- rbind(NA, test_data)

pdf("test.pdf",width=8,height=4)

forestplot(
    labeltext =cbind(column1,padded_text),
    mean = test_data$coef, upper = test_data$high,
           lower = test_data$low,
    txt_gp=fpTxtGp(cex=0.8),align="c",
    is.summary=c(TRUE, FALSE, FALSE, FALSE),
    boxsize = test_data$boxsize,lwd.ci=3,
    graphwidth=unit(100,'mm'))

dev.off()