[R] Placing a grob in multiple viewports

hadley wickham h.wickham at gmail.com
Wed Nov 2 21:09:40 CET 2005


Hi all,

What's the best way of placing a grob into muliple viewports?  I've
been using code like:

plot_grob_matrix <- function(gm, type=deparse(substitute(gm))) {	
	grid <- expand.grid(x=1:nrow(gm), y=1:ncol(gm))
	do.call(gList,mapply(function(x,y) editGrob(gm[[x,y]], vp=vp_path(x,
y, type)), grid$x, grid$y, SIMPLIFY=FALSE))
}

vp_path <- function(row, col, type) {
	vpPath("layout", paste(type, row, col, sep="_"))
}

to place a matrix of grobs into a similar looking matrix of viewports
(constructed by another function).  This works fine if the matrix
contains different grobs, but only draws one if they are all the same 
(obviously due to the copy by reference nature of grobs). What's a
better way of doing this? Most of the time the grobs are different
(ie. components of a plot), but sometimes are shared (eg. common
axes).

(any hints on a nicer way to construct the gList would also be appreciated)

Thanks,

Hadley




More information about the R-help mailing list