R中的森林图。如何添加箭头,X轴断裂并延伸X轴?

问题描述 投票:0回答:1

我已经使用下面的forestplot软件包和代码创建了一个forestplot。

我想补充三件事:

  1. [在x轴下添加箭头和文本(即,任一方向的箭头都说明了关联)。

我已经使用其他软件包查看了这些帖子。

How to add arrows to forest plot in survminer (ggforest)

How to add arrows to a forest plot?

  1. 我也想中断x轴,因为我有一个变量且CI很大。

  2. 我也想将x轴的负侧扩展到-5,以平衡图形(即使大多数数据点在右侧)。

我正在使用森林积木包。有任何想法如何使用此软件包吗?

table2OR_ex <- structure(list(X = c("Aeroplanes", "Sex (F)", "1", "2", "Cars", "Sex (F)", "1", "2"), 
                 mean = c(NA, 1.35, 7.81, 6.14, NA, 1.17, 0.15, 0.4), 
                 lower = c(NA, 1.13, 5.69, 4.36, NA, 0.74, 0.05, 0.16), 
                 upper = c(NA, 1.61, 11.01, 8.83, NA, 1.88, 0.35, 0.89),
                 p.value = c("", "< 0.001", "< 0.001", "< 0.001", "", "0.509", "< 0.001", "0.034")), 

                 .Names = c("Transport", "mean", "lower", "upper", "p value"), 
                 class = "data.frame", row.names = c(NA, -8L))

tabletext <- cbind(c("Transport","\n",table2OR_ex$Transport), 
               c("Odds ratio","\n",table2OR_ex$mean),
               c("Confidence Interval","\n", 
                 ifelse(is.na(table2OR_ex$lower), "", paste(table2OR_ex$lower, table2OR_ex$upper, 
sep= " - "))))
mat <- rbind(rbind(rep(NA, 3), rbind(rep(NA, 3), as.matrix(table2OR_ex[, 2:4]))))


fn <- local({
i = 0
no_lines <- sum(!is.na(mat[,"mean"])) 
b_clrs = colorRampPalette(colors=c("pink", "blue"))(no_lines)
l_clrs = colorRampPalette(colors=c("blue", "pink"))(no_lines) 
function(..., clr.line, clr.marker){
    i <<- i + 1
    fpDrawDiamondCI(..., clr.line = l_clrs[i], clr.marker = b_clrs[i])
}
})

forestplot(labeltext=tabletext, 
       mat,
       graphwidth=unit (70, "mm"), 
       graph.pos=3, 
       fn.ci_norm = fn,
       clip =c(-.125, max(table2OR_ex$upper, na.rm = TRUE)),
       is.summary=c(TRUE, TRUE, rep(FALSE, 8)),
       txt_gp=fpTxtGp(label=gpar(fontsize=12, cex=1), 
                      ticks=gpar(fontsize=12, cex=1.4),
                      xlab=gpar(fontsize=12,cex = 1),
                      title=gpar(fontsize=12,cex = 1.2)),

       zero=1,
       boxsize=0.4)
r graph figure forestplot
1个回答
0
投票

这里没有您想要的一切,但这是一个开始。也许其他人可以改善这个答案。

  1. 我已经使用grid.text在null值的左侧和右侧添加了标签,但是它没有箭头。您可能需要调整grid.text y的值,以使标签与x轴刻度线不重叠。
  2. 我不知道如何在x轴上放置一个中断,但是优势比应该在对数刻度上绘制(请参阅https://doi.org/10.1002/sim.4780070807,我认为这可以解决问题。
  3. 绘制OR时,在x轴上没有负值是没有意义的,但是使用对数刻度时,可以通过将下限设置为上限的倒数来使x轴对称,这就是我所做的。
library(forestplot)

table2OR_ex <- structure(list(X = c("Aeroplanes", "Sex (F)", "1", "2", "Cars", "Sex (F)", "1", "2"), 
                 mean = c(NA, 1.35, 7.81, 6.14, NA, 1.17, 0.15, 0.4), 
                 lower = c(NA, 1.13, 5.69, 4.36, NA, 0.74, 0.05, 0.16), 
                 upper = c(NA, 1.61, 11.01, 8.83, NA, 1.88, 0.35, 0.89),
                 p.value = c("", "< 0.001", "< 0.001", "< 0.001", "", "0.509", "< 0.001", "0.034")), 

                 .Names = c("Transport", "mean", "lower", "upper", "p value"), 
                 class = "data.frame", row.names = c(NA, -8L))

tabletext <- cbind(c("Transport","\n",table2OR_ex$Transport), 
               c("Odds ratio","\n",table2OR_ex$mean),
               c("Confidence Interval","\n", 
                 ifelse(is.na(table2OR_ex$lower), "", paste(table2OR_ex$lower, table2OR_ex$upper, 
sep= " - "))))
mat <- rbind(rbind(rep(NA, 3), rbind(rep(NA, 3), as.matrix(table2OR_ex[, 2:4]))))


fn <- local({
i = 0
no_lines <- sum(!is.na(mat[,"mean"])) 
b_clrs = colorRampPalette(colors=c("pink", "blue"))(no_lines)
l_clrs = colorRampPalette(colors=c("blue", "pink"))(no_lines) 
function(..., clr.line, clr.marker){
    i <<- i + 1
    fpDrawDiamondCI(..., clr.line = l_clrs[i], clr.marker = b_clrs[i])
}
})

ticks <- c(0.04, 0.2, 1, 5, 25) # ADDITION
attr(ticks, "labels") <- as.character(ticks) # ADDITION
#attr(ticks, "labels") <- c("1/25", "1/5", "1", "5", "25") # ADDITION

forestplot(labeltext=tabletext, 
       mat,
       graphwidth=unit (70, "mm"), 
       graph.pos=3, 
       fn.ci_norm = fn,
       mar = unit(rep(10, times = 4), "mm"), # ADDITION
       xlog=TRUE, # ADDITION
       xticks=ticks, # ADDITION
       clip =c(0.04, 25), # CHANGE
       is.summary=c(TRUE, TRUE, rep(FALSE, 8)),
       txt_gp=fpTxtGp(label=gpar(fontsize=12, cex=1), 
                      ticks=gpar(fontsize=12, cex=1.4),
                      xlab=gpar(fontsize=12,cex = 1),
                      title=gpar(fontsize=12,cex = 1.2)),

       zero=1,
       boxsize=0.4)

# ADDITION
downViewport("forestplot_margins")
downViewport("axis_margin")
downViewport("axis")
grid.text("Favors X", x=0.46, y=-0.25, just=c("right", "center"))
grid.text("Favors Y", x=0.54, y=-0.25, just=c("left", "center"))
© www.soinside.com 2019 - 2024. All rights reserved.