r - 更改森林图元数据包中的置信区间格式?

标签 r plot confidence-interval

假设以下代码(如 Viechtbauer,2010 中给出):

library(metafor)
data("dat.bcg", package = "metafor")
dat <- escalc(measure = "RR", ai = tpos, bi = tneg, ci = cpos, di = cneg, data = dat.bcg, append = TRUE)
res <- rma(ai = tpos, bi = tneg, ci = cpos, di = cneg, data = dat, measure = "RR")
forest(res, slab = paste(dat$author, dat$year, sep = ", "), xlim = c(-16, 6), at = log(c(0.05, 0.25, 1, 4)), atransf = exp, ilab = cbind(dat$tpos, dat$tneg, dat$cpos, dat$cneg), ilab.xpos = c(-9.5, -8, -6, -4.5), cex = 0.75)
op <- par(cex = 0.75, font = 2)
text(c(-9.5, -8, -6, -4.5), 15, c("TB+", "TB-", "TB+", "TB-"))
text(c(-8.75, -5.25), 16, c("Vaccinated", "Control"))
text(-16, 15, "Author(s) and Year", pos = 4)
text(6, 15, "Relative Risk [95% CI]", pos = 2)
par(op)

这给出了如下的森林图:

Forest plot

那么如何更改图表中置信区间的格式?是否可以将方括号替换为圆括号并使用“to”代替“,”?使用“-”或长连字符代替“,”怎么样?这应该改变,即 [0.13, 1.26] 到 (0.13 到 1.26) 或 (0.13 – 1.26)

最佳答案

您需要对forest.rma的代码进行一些修改。几个步骤:

通过键入函数名称显示当前版本的代码后:

 forest.rma   # Copy the name and the code and paste into the console 
              #  Add an assignment operator `<-`
              # leave off the bytecode and environment notations at the bottom

或者您可以在编辑器中执行此操作,这可能是首选方法,因为您可能希望将此代码保存到 .Rprofile 文件中。

1) 将参数添加到参数列表:

forest.rma <- 
function (x, annotate = TRUE, addfit = TRUE, addcred = FALSE, 
    showweights = FALSE, xlim, alim, clim, ylim, at, steps = 5, 
    level = x$level, digits = 2, refline = 0, xlab, slab, mlab, 
    ilab, ilab.xpos, ilab.pos, order, transf, atransf, targs, 
    rows, efac = 1, pch = 15, psize, col, border, lty, cex, cex.lab, 
    cex.axis, annosep = " , ", bkt=c("[", "]"), ...) 
{  #  ....not showing all the _long_ function body 
   # Scroll down to almost the bottom of the function body

2) 查找并更改 annotext cbind 赋值的参数。有几个地方可以构造 annotext,但只有其中一个与您的“格式目标”匹配。找到一个看起来像这样的:

# annotext <- cbind(annotext[, 1], " [ ", annotext[, 
#                2], " , ", annotext[, 3], " ]")

更改为:

annotext <- cbind(annotext[, 1], bkt[1], annotext[, 
                 2], annosep, annotext[, 3], bkt[2] )
# hit enter to get the modification to hold in your workspace

3) 现在为该函数分配正确的环境,以便它可以与其兄弟函数良好地配合:

environment(forest.rma) <- environment(forest.default)
# if you forget this step you see this error:

Error in forest.rma(res, slab = paste(dat$author, dat$year, sep = ", "), : could not find function ".setlab"

并用您选择的新参数调用它:

png(); forest(res, slab = paste(dat$author, dat$year, sep = ", "), xlim = c(-16, 6), at = log(c(0.05, 0.25, 1, 4)), atransf = exp, ilab = cbind(dat$tpos, dat$tneg, dat$cpos, dat$cneg), ilab.xpos = c(-9.5, -8, -6, -4.5), cex = 0.75, annosep=" to ", bkt = c( "(", ")" ) )
op <- par(cex = 0.75, font = 2)
text(c(-9.5, -8, -6, -4.5), 15, c("TB+", "TB-", "TB+", "TB-"))
text(c(-8.75, -5.25), 16, c("Vaccinated", "Control"))
text(-16, 15, "Author(s) and Year", pos = 4)
text(6, 15, "Relative Risk [95% CI]", pos = 2)
dev.off()

enter image description here

关于r - 更改森林图元数据包中的置信区间格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38864396/

相关文章:

matlab - 在 MATLAB 中绘制 3-D 矩阵*值*

matlab - 更改 Matlab 默认的 x 限制行为

java - java中的置信区间,测试对象列表中元素的随机选择

r - 如何获得 glm 系数的阴影置信区间带?

python - matplotlib:有轴保持率

r - 置信区间通过 BIFE 给出 NA 值

rpy2 importr 因 xts 和 quantmod 失败

r - 循环返回比预期更多的值和 NA

r - 在 R foreach() 下并行运行时无法识别动态库依赖项

python - 通过 subprocess.call 使用 Python 执行 R 脚本