【问题标题】:using grid to annotate lattice plots outside of the plotting region使用网格来注释绘图区域之外的格子图
【发布时间】:2020-08-20 19:42:46
【问题描述】:

我经常使用 lattice 包来创建图形。然后我使用grid::grid.text() 来注释绘图区域之外的数字。通常,我制作PDF文件,没有问题。

我现在需要将我的图形保存为其他格式,例如 PNG。而且我现在发现,当我尝试在绘图区域之外进行注释时,注释被剪裁了。这是一个小例子:

library(grid)
library(gridExtra)
library(lattice)

myPanel <- function (...) { 
  panel.xyplot(...)
  grid.text("This is a very, very long line", x = .99, y = .5)
}
xyplot(1:10 ~ 1:10, panel = myPanel, par.settings = list(clip = list(panel="off")))

我可以通过使用gridExtra::grid.arrange() 来解决这个问题,但这似乎是解决一个简单问题的一种过于精细的方法:

myPlot_grob     <- grid.grab(wrap = TRUE)
rectTransparent <- rectGrob(gp = gpar(col = 'transparent', fill = 'transparent'))
grid.arrange(
  grobs = list(
    rectTransparent,
    myPlot_grob,
    rectTransparent),
  ncol = 3,
  widths = unit(c(2, 4, 2), 'inches'))

有没有更简单的方法?我有 Deepayan Sarkar 的格子书和 Paul Murrell 的 R 图形,但我没有在其中找到明确的解决方案。当问题出现在base graphicsggplot 时,有相关的 SO 帖子,但我还没有找到与格子图形相关的帖子。

【问题讨论】:

    标签: r lattice clipping r-grid


    【解决方案1】:

    您可以手动调整边距。

    lattice.options(layout.widths=list(left.padding=list(x=0), right.padding=list(x=5)))
    
    xyplot(1:10 ~ 1:10, panel = myPanel, par.settings = list(clip = list(panel="off")))
    

    但由于必须手动完成,因此可能不是理想的解决方案。

    【讨论】:

    • 谢谢 - 这确实有帮助。我曾尝试用trellis.par.set() 做类似的事情,但没有成功。但我没想到lattice.options()。 // left.paddingright.padding 之间有一个我不明白的权衡:增加一个似乎会减少另一个,即使在 lattice.options 中指定的另一个值没有改变。但我可以解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    相关资源
    最近更新 更多