Skip to content
Prev 16393 / 63424 Next

Problem going back to a viewport with gridBase

Hi
Gabor Grothendieck wrote:
I think this sort of thing can easily be built on top rather than into 
the existing system.  For example, here's a function that pushes all of 
the basic cells in a layout using a simple naming convention:

layoutVPname <- function(i, j) {
   paste("layoutViewport", i, ",", j, sep="")
}

layoutVPpath <- function(i, j, name="layout") {
   vpPath(name, layoutVPname(i, j))
}

pushLayout <- function(nr, nc, name="layout") {
   pushViewport(viewport(layout=grid.layout(nr, nc),
                         name=name))
   for (i in 1:nr) {
     for (j in 1:nc) {
       pushViewport(viewport(layout.pos.row=i,
                             layout.pos.col=j,
                             name=layoutVPname(i, j)))
       upViewport()
     }
   }
   upViewport()
}

And here's a use of the function to push lots of layout cells, then draw 
lattice plots in different cells using downViewport() to go to the cell 
with the appropriate name.  In this case, we use cells by column, but 
simply reverse the order of the loops to use cells by row.

pushLayout(2, 3)
for (i in 1:2) {
   for (j in 1:3){
     depth <- downViewport(layoutVPpath(i, j))
     print(xyplot(seq(i*j) ~ seq(i*j)), newpage=FALSE)
     upViewport(depth)
   }
}
The raw grid functions have a 'vp' argument for this purpose.  It would 
be nice if lattice functions had something similar (or maybe just 
print.trellis).  Here's your example using the 'vp' argument to 
grid.text() (and using the layout that was pushed above) ...

for (i in 1:2) {
   for (j in 1:3){
     grid.text(i*j, vp=layoutVPpath(i, j))
   }
}

Paul