【问题标题】:Forestplot in R. How to add arrows, x axis breaks and extend the x axis?R中的Forestplot。如何添加箭头、x轴中断和扩展x轴?
【发布时间】:2020-08-29 02:53:46
【问题描述】:

我使用下面的 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】:

    这并没有你想要的一切,但它是一个开始。也许其他人可以改进这个答案。

    1. 我已经使用 grid.text 在空值的左侧和右侧添加了标签,但它没有箭头。您可能需要调整 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"))
    

    【讨论】:

    • 谢谢。出于某种原因,这会产生不均匀的框尺寸或边缘(钻石看起来参差不齐,就像是两个图像重叠)
    • 很奇怪。导出为 PDF 时是这样,还是只是在预览窗格中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2014-06-29
    • 1970-01-01
    相关资源
    最近更新 更多