hadley wickham
2005-Oct-03 18:20 UTC
[R] Grid: constructing a gTree with grobs that use named viewports from a vpTree
I'm trying to create a layout with named viewports that I can use for other functions. I create the viewport tree that I want, and a list of grobs with the viewports describing where they should go. library(grid) vp <- vpTree( viewport(layout=grid.layout(2,2), name="layout"), children=vpList( viewport(layout.pos.col = 1, layout.pos.row=1, name="tl"), viewport(layout.pos.col = 2, layout.pos.row=2, name="br") ) ) grobs <- gList( rectGrob(vp="tl"), textGrob("Top left", vp="tl"), textGrob("Bottom right", vp="br") ) I can draw the grobs using the following code: grid.newpage() pushViewport(vp) upViewport(1) grid.draw(grobs) But I want a grob that represents those grobs drawn in the appropriate viewports. I had hoped I could do something like: grid.newpage() grid.draw(gTree(vp=vp, children = grobs)) But I get: Error in downViewport.vpPath(vp, strict = TRUE, recording = FALSE) : Viewport 'tl' was not found presumably because no equivalent of the upViewport(1) command is used. What should I be doing here? Thanks, Hadley
hadley wickham
2005-Oct-03 18:50 UTC
[R] Grid: constructing a gTree with grobs that use named viewports from a vpTree
> But I get: > > Error in downViewport.vpPath(vp, strict = TRUE, recording = FALSE) : > Viewport 'tl' was not found > > presumably because no equivalent of the upViewport(1) command is used. > > What should I be doing here?I've solved my own problem - I need to use childrenvp instead and construct a fuller viewport path: grobs <- gList( rectGrob(vp=vpPath("layout","tl")), textGrob("Top left", vp=vpPath("layout","tl")), textGrob("Bottom right", vp=vpPath("layout","br")) ) grid.draw(gTree(childrenvp=vp, children = grobs)) Hadley