使用gridExtra和grid.pattern()在面板上对齐模式

时间:2012-06-12 22:17:01

标签: r graphics lattice

gridExtra包添加了一个类“pattern”的grob,它允许用图案填充矩形。例如,

library(gridExtra)
grid.pattern(pattern = 1)

创建一个填充对角线的框。我想创建一堆面板,其中每个面板都填充了这些对角线。这很简单:

library(lattice); library(gridExtra)
examplePlot <- xyplot(
  1 ~ 1 | 1:2, 
  panel = function () grid.pattern(pattern = 1),
  layout = c(1, 2),

  # Remove distracting visual detail
  scales = list(x=list(draw=FALSE), y=list(draw=FALSE)),
  strip = FALSE, xlab = '', ylab = ''
)
print(examplePlot)

问题是对角线不是跨面板对齐的。也就是说,存在视觉“断裂”,其中第一面板的底部与第二面板的顶部相遇:在该点处,线不对齐。这是我想解决的问题。

我可以通过在pattern.offset = c(.2005, 0)调用中添加参数grid.pattern来消除大部分视觉中断,并确保它仅适用于底部面板。但是这个解决方案并没有概括。例如,如果我更改模式(例如,通过将granularity参数用于grid.pattern),则此解决方案将无效。有更普遍的解决方案吗?

1 个答案:

答案 0 :(得分:1)

要完成这项工作,您必须负责设置panel.height使用的print.trellis参数。 (要了解原因,请在运行示例代码后尝试调整绘图设备的大小:随着设备和面板的大小发生变化,线的匹配/不匹配也会发生变化):

## Calculate vertical distance (in mm) between 45 degree diagonal lines
## spaced 5mm apart (the default distance for grid.pattern).
vdist <- 5 * sqrt(2)

nLines <- 8L    ## can be any integer
panelHeight <- list(x = nLines*vdist, units = "mm", data = NULL)

## Plot it
print(examplePlot, panel.height=panelHeight)

enter image description here