R中的Forestplot.如何添加箭头,X轴折断和扩展X轴? [英] Forestplot in R. How to add arrows, x axis breaks and extend the x axis?

查看:364
本文介绍了R中的Forestplot.如何添加箭头,X轴折断和扩展X轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a forestplot using the forestplot package and code below.

我想补充三件事:

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

我使用其他程序包审查了这些帖子.

I have reviewed these posts using a different package.

如何在survminer(ggforest)的森林图中添加箭头

如何向中添加箭头森林地吗?

  1. 我也想中断x轴,因为我有一个具有非常宽CI的变量.

  1. I would also like to put a break the x axis as I have one variable with a very wide CI.

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

I would also like to extend the negative side of the x-axis to -5, to balance the figure (even though most datapoints are on the right).

我正在使用森林图包.有任何想法如何使用此程序包吗?

I am using the forest plot package. Any ideas how to do it with this package?

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)

推荐答案

这并没有您想要的所有内容,但这只是一个开始.也许其他人可以改善这个答案.

This doesn't have everything you wanted, but it's a start. Maybe someone else can improve on this answer.

  1. 我已经使用grid.text在null值的左侧和右侧添加了标签,但是它没有箭头.您可能需要调整grid.text y的值,以使标签与x轴刻度线不重叠.
  2. 我不知道如何在x轴上插入一个中断点,但是优势比应该以对数刻度绘制(请参见 https://doi.org/10.1002/sim.4780070807 ),我认为这可以解决问题.
  3. 在绘制OR时,在x轴上具有负值是没有意义的,但是通过对数标度,您可以通过将下限设置为上限的倒数来使x轴对称约束,这就是我所做的.
  1. I've added labels to the left and the right of the null value using grid.text, but it doesn't have arrows. You may need to adjust the grid.text y values so that the labels don't overlap with the x-axis ticks.
  2. I don't know how to put a break in the x-axis, but odds ratios should be plotted on a log scale (see https://doi.org/10.1002/sim.4780070807), and I think that solves the problem.
  3. It doesn't make sense to have a negative value on the x-axis when you're plotting ORs, but with a log scale you can make the x-axis symmetric by setting the lower bound to the reciprocal of the upper bound, which is what I've done.

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"))

这篇关于R中的Forestplot.如何添加箭头,X轴折断和扩展X轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆