【问题标题】:Adjust plot margin with coplot使用 coplot 调整绘图边距
【发布时间】:2019-12-07 13:33:54
【问题描述】:

使用coplot添加标题时如何改变绘图区域的上边距?更改 par 值似乎没有效果。我想增加上边距以将标题放在那里,但更改 par(mar) 并没有像我预期的那样工作。这是一个例子,

opar <- par(mar=c(5.1, 4.1, 20, 1))
coplot(Sepal.Length ~ Sepal.Width | Species, data=iris, columns=3,
  bar.bg=c(fac="light green"), panel=panel.smooth)   # mar=c(5.1, 4.1, 10, 1)
title("test", outer=TRUE)
par(opar)

【问题讨论】:

  • 使用latticelatticeExtra,您拥有更多的灵活性。首先:xyplot(Sepal.Length ~ Sepal.Width | Species, data=iris,type=c("p","smooth"),layout=c(3,1),main="Test")

标签: r plot


【解决方案1】:

您可以将其稍微向下移动,使其不会“偏离边缘”:

opar <- par(mar=c(5.1, 4.1, 20, 1))
coplot(Sepal.Length ~ Sepal.Width | Species, data=iris, columns=3,
  bar.bg=c(fac="light green"), panel=panel.smooth)   # mar=c(5.1, 4.1, 10, 1)
title("test", line =-1, outer=TRUE)
par(opar)

par() 影响顶部边距空间的努力失败了,我怀疑这是硬编码边距的绘图方法之一。实际上,这里是 coplot 中的代码:

 mar <- if (have.b) 
        rep.int(0, 4)
    else c(0.5, 0, 0.5, 0)
    oma <- c(5, 6, 5, 4)
    if (have.b) {
        oma[2L] <- 5
        if (!b.is.fac) 
            oma[4L] <- 5
    }
    if (a.is.fac && show.given[1L]) 
        oma[3L] <- oma[3L] - 1

 opar <- par(mfrow = c(total.rows, total.columns), oma = oma, 
        mar = mar, xaxs = "r", yaxs = "r")

【讨论】:

    【解决方案2】:

    正如@42 提到的,coplot 的代码似乎包含一个硬编码值marhttps://github.com/wch/r-source/blob/bfe73ecd848198cb9b68427cec7e70c40f96bd72/src/library/graphics/R/coplot.R#L214

    另一种解决方案是将 coplot 代码复制/粘贴到您自己的脚本中,并在函数中更改 mar 的值,但如果 @42 的解决方案为您完成这项工作,这似乎有点矫枉过正。

    【讨论】:

      猜你喜欢
      • 2021-10-18
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      相关资源
      最近更新 更多