[R] Grid: constructing a gTree with grobs that use named viewports from a vpTree

hadley wickham h.wickham at gmail.com
Mon Oct 3 20:20:24 CEST 2005


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




More information about the R-help mailing list