[R] grid.grab(): strange behavior of "wrap" argument when working with lattice output

John G. Bullock john@bullock @ending from northwe@tern@edu
Tue Oct 30 19:34:25 CET 2018

grid.grab() captures all of the current viewports and stores them as a 
grob. In theory, and often in practice, you can use grid.grab() to store 
an image now for drawing later on. But I have noticed some odd behavior 
of grid.grab() when working with lattice output. It is all related to 
the wrap argument:

1. When wrap = TRUE, the saved grob should produce a drawing faithful to 
the original viewports. But sometimes it doesn't.
     2. wrap = FALSE is supposed to produce less faithful output if it 
differs at all, but sometimes its output is more faithful.
     3. Even when using wrap = TRUE, grid.grab() warns me to use wrap = 

Here is a minimal example to illustrate all of these behaviors:

     xyplot(1:5 ~ 1:5)
     panel.abline(h = 3)
     panel.abline(h = 5)
myGrob <- grid.grab(wrap = TRUE)

At this point, I receive a warning --

       In grabDL(warn, wrap, ...) :
         one of more grobs overwritten (grab WILL not be faithful; try 
'wrap = TRUE')

-- which is strange, given that I specified wrap = TRUE.

I proceed by running grid.draw(myGrob). According to Paul Murrell (R 
Graphics, 2nd ed., page 239), using grid.grab(wrap = TRUE) is 
"guaranteed to replicate the original output." But it doesn't. 
Specifically, the top horizontal line (from panel.abline(h = 5)) isn't 

Matters get stranger when I consider the following code:

     xyplot(1:5 ~ 1:5)
     panel.abline(h = 3)
panel.abline(h = 5)
     myGrob <- grid.grab(wrap = FALSE)

These commands are the same as in the previous example, except that this 
time, wrap = FALSE. But this time, grid.draw() does faithfully reproduce 
the captured viewports. So the behavior is the opposite of what I 
expect: when wrap = FALSE, grid.draw() faithfully reproduces the 
captured viewports, but when wrap = TRUE, it doesn't.

I've run these examples in R 3.3.2 and 3.5.1. (Session info from 3.3.2 
is appended.) The result is the same in both cases. Are these bugs, or 
am I overlooking something?

Thank you,
John Bullock

 > sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United 
States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets methods base

other attached packages:
[1] lattice_0.20-34 RevoUtilsMath_10.0.0

loaded via a namespace (and not attached):
[1] RevoUtils_10.0.2

	[[alternative HTML version deleted]]

More information about the R-help mailing list