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

From: hadley wickham <h.wickham_at_gmail.com>
Date: Tue 04 Oct 2005 - 04:20:24 EST


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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Oct 04 04:22:41 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:40:35 EST