0

我正在创建一个包含 3 个部分的图形,这些部分是使用metafor包创建的森林图。如何减少子图之间的空白,使它们更靠近?

这是示例数据和代码

library(metafor)
dat <- read.csv(("https://raw.githubusercontent.com/aelhak/data/main/dat2.csv"))

graphics.off()
grDevices::cairo_pdf("plot.pdf", width = 4.5, height = 6)
layout(mat = matrix(c(1, 2, 3), nrow = 3, ncol = 1))

# SUB-PLOT 1
forest(
  x = dat$estimate, 
  ci.lb = dat$conf.low, 
  ci.ub = dat$conf.high,
  slab = dat$age_group, 
  xlab = "mean difference", 
  cex = 1, cex.lab = 0.7, psize = 1, 
  xlim = c(-16.7, 11),
  alim = c(-3, 4.5), 
  #  ylim = c(0, 71), 
  ilab = cbind(
    dat$I2,
    dat$n_studies,
    dat$n_g1,
    dat$n_g2),
  ilab.xpos = c(-10.1, -8.2, -6.1, -3.85),
  header = "Age\nGroup"
)

text(-10.65, 6, "I\xB2", font = 2,  pos = 4)
text(-8.8, 6, "N\nSt.", font = 2,  pos = 4)
text(-7.05, 6, "N\ng1", font = 2,  pos = 4)
text(-4.9, 6, "N\ng2", font = 2,  pos = 4)
title("sub-plot 1", adj = 0)

# SUB-PLOT 2
forest(
  x = dat$estimate, 
  ci.lb = dat$conf.low, 
  ci.ub = dat$conf.high,
  slab = dat$age_group, 
  xlab = "mean difference", 
  cex = 1, cex.lab = 0.7, psize = 1, 
  xlim = c(-16.7, 11),
  alim = c(-3, 4.5), 
  #  ylim = c(0, 71), 
  ilab = cbind(
    dat$I2,
    dat$n_studies,
    dat$n_g1,
    dat$n_g2),
  ilab.xpos = c(-10.1, -8.2, -6.1, -3.85),
  header = "Age\nGroup"
)

text(-10.65, 6, "I\xB2", font = 2,  pos = 4)
text(-8.8, 6, "N\nSt.", font = 2,  pos = 4)
text(-7.05, 6, "N\ng1", font = 2,  pos = 4)
text(-4.9, 6, "N\ng2", font = 2,  pos = 4)
title("sub-plot 2", adj = 0)

# SUB-PLOT 3
forest(
  x = dat$estimate, 
  ci.lb = dat$conf.low, 
  ci.ub = dat$conf.high,
  slab = dat$age_group, 
  xlab = "mean difference", 
  cex = 1, cex.lab = 0.7, psize = 1, 
  xlim = c(-16.7, 11),
  alim = c(-3, 4.5), 
  #  ylim = c(0, 71), 
  ilab = cbind(
    dat$I2,
    dat$n_studies,
    dat$n_g1,
    dat$n_g2),
  ilab.xpos = c(-10.1, -8.2, -6.1, -3.85),
  header = "Age\nGroup"
)

text(-10.65, 6, "I\xB2", font = 2,  pos = 4)
text(-8.8, 6, "N\nSt.", font = 2,  pos = 4)
text(-7.05, 6, "N\ng1", font = 2,  pos = 4)
text(-4.9, 6, "N\ng2", font = 2,  pos = 4)
title("sub-plot 3", adj = 0)

dev.off()

输出图

4

1 回答 1

1

可以使用par 函数的参数omi来控制它们。mai我们设置par(mfrow=c(3,1), omi=c(2,.3,.1,.3), mai=c(.5,0,.2,0)). omi 是指整个页面的外边距,而 mai 是指每个单独绘图的内边距。它们总是按 c(lower,left,upper,right) 的顺序排列;详情咨询?par。这是我能看到的最好的...

library(metafor)
dat <- read.csv(("https://raw.githubusercontent.com/aelhak/data/main/dat2.csv"))

graphics.off()
grDevices::cairo_pdf("plot.pdf", width = 4.5, height = 6)
par(mfrow=c(3,1), omi=c(2,.3,.1,.3), mai=c(.5,0,.2,0))

# SUB-PLOT 1
forest(
  x = dat$estimate, 
  ci.lb = dat$conf.low, 
  ci.ub = dat$conf.high,
  slab = dat$age_group, 
  xlab = "mean difference", 
  cex = .7, cex.lab = 0.7, psize = 1, 
  xlim = c(-16.7, 11),
  alim = c(-3, 4.5), 
  #  ylim = c(0, 71), 
  ilab = cbind(
    dat$I2,
    dat$n_studies,
    dat$n_g1,
    dat$n_g2),
  ilab.xpos = c(-10.1, -8.2, -6.1, -3.85),
)

text(-10.65, 6, "I\xB2", font = 2,  pos = 4)
text(-8.8, 6, "N\nSt.", font = 2,  pos = 4)
text(-7.05, 6, "N\ng1", font = 2,  pos = 4)
text(-4.9, 6, "N\ng2", font = 2,  pos = 4)
text(-15, 6, "Age", cex=.6)
text(-15, 5.3, "Group", cex=.6)
title("sub-plot 1", adj = 0)

# SUB-PLOT 2
forest(
  x = dat$estimate, 
  ci.lb = dat$conf.low, 
  ci.ub = dat$conf.high,
  slab = dat$age_group, 
  xlab = "mean difference", 
  cex = .7, cex.lab = 0.7, psize = 1, 
  xlim = c(-16.7, 11),
  alim = c(-3, 4.5), 
  #  ylim = c(0, 71), 
  ilab = cbind(
    dat$I2,
    dat$n_studies,
    dat$n_g1,
    dat$n_g2),
  ilab.xpos = c(-10.1, -8.2, -6.1, -3.85),
)

text(-10.65, 6, "I\xB2", font = 2,  pos = 4)
text(-8.8, 6, "N\nSt.", font = 2,  pos = 4)
text(-7.05, 6, "N\ng1", font = 2,  pos = 4)
text(-4.9, 6, "N\ng2", font = 2,  pos = 4)
text(-15, 6, "Age", cex=.6)
text(-15, 5.3, "Group", cex=.6)
title("sub-plot 2", adj = 0)

# SUB-PLOT 3
forest(
  x = dat$estimate, 
  ci.lb = dat$conf.low, 
  ci.ub = dat$conf.high,
  slab = dat$age_group, 
  xlab = "mean difference", 
  cex = 1, cex.lab = 0.7, psize = 1, 
  xlim = c(-16.7, 11),
  alim = c(-3, 4.5), 
  #  ylim = c(0, 71), 
  ilab = cbind(
    dat$I2,
    dat$n_studies,
    dat$n_g1,
    dat$n_g2),
  ilab.xpos = c(-10.1, -8.2, -6.1, -3.85),
  header = "Age\nGroup"
)

text(-10.65, 6, "I\xB2", font = 2,  pos = 4)
text(-8.8, 6, "N\nSt.", font = 2,  pos = 4)
text(-7.05, 6, "N\ng1", font = 2,  pos = 4)
text(-4.9, 6, "N\ng2", font = 2,  pos = 4)
title("sub-plot 3", adj = 0)

dev.off()

在此处输入图像描述

于 2021-03-11T21:49:23.927 回答