【问题标题】:How do I get a forestplot function to work in r?如何让 Forestplot 函数在 r 中工作?
【发布时间】:2020-08-17 08:35:39
【问题描述】:

我正在使用 foresplot 包。我想创建一个包含两组(飞机、汽车)和每个组三个变量的森林图。我试图为箱线图和线条创建一个函数。但是,森林图表格文本有效,但函数无效。

示例数据

structure(list(X = c("Aeroplanes", "Sex (F)", "1", "2", "Cars", 
"Sex (F)", "1", "2"), OR = c(NA, 1.35, 7.81, 6.14, NA, 1.17, 
0.15, 0.4), Cl.low = c(NA, 1.13, 5.69, 4.36, NA, 0.74, 0.05, 
0.16), CI.high = 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")), class = "data.frame", row.names = c(NA, 
-8L))



CI <- table2OR_ed
CI <- within (CI, rm(X,OR, p.value))
CI$CI <-apply(CI,1,function(x){
  paste0("(",paste(x, collapse=", "),")")
})  
CI$CI[which(CI$CI=="(NA, NA)")] <- NA


tabletext <- cbind(c("Transport","\n",table2OR_ex$X), 
                   c("Odds ratio","\n",table2OR_ex$OR),
                   c("Confidence Interval","\n",CI$CI))

fn <- local({
  i = 0
  no_lines <- sum(!is.na(table2OR_ex$OR)) 
  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, graphwidth=unit (70, "mm"), graph.pos=3, 
           mean=c(NA,NA,table2OR_ex$OR), 
           lower=c(NA,NA,table2OR_ex$CI.low), upper=c(NA,NA,table2OR_ex$CI.high),
           fn.ci_norm = fn, #use function
           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)

我收到此错误

警告信息: 在 min(lower, na.rm = TRUE) 中: min 没有不可缺少的参数;返回Inf

我检查了类并且 CI.low 为空,即使在我导入 csv 时它是数字。那么也许这是错误的根源?

【问题讨论】:

  • CI.low 有两种不同的拼写形式
  • @rawr 抱歉,我错过了 CI 的一些代码

标签: r forestplot


【解决方案1】:

试试这个:

library(forestplot)
#> Loading required package: grid
#> Loading required package: magrittr
#> Loading required package: checkmate
library(grDevices)
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)

reprex package (v0.3.0) 于 2020 年 5 月 2 日创建

【讨论】:

  • 仅适用于切割左侧的钻石 (
  • 另外它也没有使用我在函数中放入的配色方案。有没有办法指定颜色?
  • 钻石看起来也是锯齿状的。不知道为什么会这样?
  • 谢谢。有几件事-您放置的表格文本的CI组合代码不起作用,因此我继续使用我的版本。该函数的配色方案为黑色线条和菱形形状不均匀的线条生成粗体标题。 x 轴以不均匀的方式跳过数字。不知道为什么会发生所有这些故障
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 2016-05-10
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 2022-06-18
  • 2014-02-16
相关资源
最近更新 更多