[R] How to create a list of trellis objects for grid.arrange()

Ben Tupper btupper at bigelow.org
Sun Oct 30 14:20:44 CET 2016


Hi,

You might try gridExtra::marrangeGrob()

require(raster)
require(sp)
require(gridExtra)

f <- system.file("external/test.grd", package="raster")
r <- raster(f)
p1 <- spplot(r)
p2 <- spplot(r/2)

ps <- list(p1,p2)
mm <- marrangeGrob(ps, ncol=1, nrow=2, top = 'foo')

mm


The above generates the two plots arranged vertically.  Note that if I use ps <- c(p1,p2) instead of ps <- list(p1,p2) the marrangeGrob() function throws an error.  Unlike list(), c() coerces the components to a common type, which, in this case, I guess isn't 'trellis'.  Check out the values of str(c(p1, p2)) vs str(list(p1,p2)).   


> ps <- c(p1,p2)
> mm <- marrangeGrob(ps, ncol=1, nrow=2, top = 'foo')
Error in (function (..., grobs = list(...), layout_matrix, vp = NULL,  : 
  could not find function "levelplot"


Cheers,
Ben

> On Oct 30, 2016, at 12:26 AM, Duncan Mackay <dulcalma at bigpond.com> wrote:
> 
> Hi
> 
> I did not have a look at this when it was sent as I thought it dealt with
> ggplot objects 
> 
> If you have trellis objects (check by str())or ?class) try printing them 
> 
> print(p2, position = (c(0,0,1,0.5), more =T)
> print(p1, position = (c(0,0.5,1,1), more = F)
> 
> The only other way that I can think of is using viewports
> ? grid::viewports
> 
> If it is base graphics then ? layout  may fix it
> 
> Regards
> 
> Duncan
> 
> Duncan Mackay
> Department of Agronomy and Soil Science
> University of New England
> Armidale NSW 2351
> Email: home: mackay at northnet.com.au
> 
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Agustin Lobo
> Sent: Thursday, 27 October 2016 23:58
> To: r-help at r-project.org
> Subject: [R] How to create a list of trellis objects for grid.arrange()
> 
> Given
> require(raster)
> require(sp)
> require(gridExtra)
> 
> f <- system.file("external/test.grd", package="raster")
> r <- raster(f)
> p1 <- spplot(r)
> p2 <- spplot(r)
> 
> I would like to plot the equivalent to
> 
> grid.arrange(p1,p2,ncol=1,nrow=2)
> 
> but keeping the trellis objects p1 and p2 within one single object (as
> in practice I have many objects generated within a for() loop).
> 
> The following used to work:
> ps <- c(p1,p2)
> grid.arrange(ps,ncol=1,nrow=2)
> 
> but does not work any more.
> 
> How should I combine p1 and p2 into one single object that would be
> accepted by grid.arrange?
> 
> Thanks
> -- 
> Agustin Lobo
> aloboaleu at gmail.com
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org



More information about the R-help mailing list