[R] Construct plot combination using grid without plotting and retrieving an object?

Paul Murrell paul at stat.auckland.ac.nz
Mon May 27 01:42:18 CEST 2013


On 05/24/13 20:24, Johannes Graumann wrote:
> Hi,
> I'm currently combining multiple plots using something along the lines
> of the following pseudo-code:
> library(grid)
> grid.newpage()
> tmpLayout <- grid.layout(
>      nrow=4,
>      ncol=2)
> pushViewport(viewport(layout = tmpLayout))
> and than proceeding with filling the viewports ... works fine, but for
> packaging of functions I would really prefer if I could assemble all of
> this in an object which in the end would be callable with "print".
> I'm envisioning something along the lines of what I can do with
> ggplot2: return a plot as a ggpplot object and plot it later rather
> than as I assemble it. Is that possible with a complex grid figure?
> Thanks for any pointers.

You can work off-screen with grobs and gTrees and vpTrees, for example ...

vplay <- viewport(layout=grid.layout(2, 2),
vp.1.1 <- viewport(layout.pos.col=1, layout.pos.row=1,
vp.2.2 <- viewport(layout.pos.col=2, layout.pos.row=2,
x <- gTree(childrenvp=vpTree(vplay, vpList(vp.1.1, vp.2.2)),
                rectGrob(vp="vplay::vp.1.1", gp=gpar(fill="grey")),
                textGrob("1", vp="vplay::vp.1.1"),
                rectGrob(vp="vplay::vp.2.2", gp=gpar(fill="grey")),
                textGrob("2", vp="vplay::vp.2.2")))

... is that the sort of thing you mean?


> Joh
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz

More information about the R-help mailing list