【问题标题】:Reduce space between meta-analysis sub-plots in a PDF减少 PDF 中元分析子图之间的空间
【发布时间】:2021-06-09 15:44:57
【问题描述】:

我正在创建一个包含 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()

【问题讨论】:

    标签: r pdf spacing metafor


    【解决方案1】:

    可以使用 par 函数的 omimai 参数来控制它们。我们设置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()
    

    【讨论】:

      猜你喜欢
      • 2022-12-23
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      相关资源
      最近更新 更多