[R] Changing layout in grid.arrange

Roy Mendelssohn - NOAA Federal roy.mendelssohn at noaa.gov
Sat May 9 03:09:15 CEST 2015


Thanks, it took some digging in the internet, but I figured out how to do it.

The first thing is how to pass options to grid.arrange when used in do.call.  It takes a list, which I tried in several different ways,  the list has to be of a certain form, so something like:

> args.list <- c(plots,list(nrow=5,ncol=2))
> do.call(grid.arrange,args.list)


The second thing is to interleave sequences before doing the lapply, as in:

x<- 1:5
y<-6:10
iOrder<-c(rbind(x,y))

I should add that Dennis Murphy sent this solution off-line, I will add it for completeness:

> require(ggplot2)
> plots = lapply(1:10, function(.x) qplot(1:10,rnorm(10), main=paste("plot",.x)))
> require(gridExtra)
> do.call(grid.arrange, c(plots[as.vector(gdata::interleave(1:5, 6:10))],
>                         list(nrow = 5)))


Either of these produces a grid of plots with 5 rows and two columns, which is what I was after.


-Roy





> On May 8, 2015, at 5:38 PM, Jim Lemon <drjimlemon at gmail.com> wrote:
> 
> Hi Roy,
> If this helps, you can get the layout like this:
> 
> split.screen(figs=matrix(c(rep(0,5),rep(0.5,10),rep(1,5),
> rep(seq(0.8,0,by=-0.2),2),rep(seq(1,0.2,by=-0.2),2)),ncol=4))
> for(scr in 1:10) {
> screen(scr)
> par(mar=c(0,0,0,0))
> plot(0.5,0.5,xlim=c(0,1),ylim=c(0,1),
>  axes=FALSE,xlab="",ylab="",type="n")
> box()
> text(0.5,0.5,scr)
> }
> close.screen(all=TRUE)
> 
> Jim
> 
> 
> On Sat, May 9, 2015 at 12:53 AM, Roy Mendelssohn - NOAA Federal
> <roy.mendelssohn at noaa.gov> wrote:
>> Hi All:
>> 
>> I am doing something very similar to the the example in the grid.arrange package:
>> 
>> require(ggplot2)
>> plots = lapply(1:10, function(.x) qplot(1:10,rnorm(10), main=paste("plot",.x)))
>> require(gridExtra)
>> do.call(grid.arrange,  plots)
>> 
>> 
>> If you run this, the layout is 4 rows and 3 columns with graphs 1-3 going across. What I would like instead is for the layout to have 2 columns and 5 rows, with graphs 1-5 going down the first column, graphs 6-10 going down the second column but almost everything I have tried has failed.  Any help appreciated.
>> 
>> -Roy M.
>> 
>> 
>> **********************
>> "The contents of this message do not reflect any position of the U.S. Government or NOAA."
>> **********************
>> Roy Mendelssohn
>> Supervisory Operations Research Analyst
>> NOAA/NMFS
>> Environmental Research Division
>> Southwest Fisheries Science Center
>> ***Note new address and phone***
>> 110 Shaffer Road
>> Santa Cruz, CA 95060
>> Phone: (831)-420-3666
>> Fax: (831) 420-3980
>> e-mail: Roy.Mendelssohn at noaa.gov www: http://www.pfeg.noaa.gov/
>> 
>> "Old age and treachery will overcome youth and skill."
>> "From those who have been given much, much will be expected"
>> "the arc of the moral universe is long, but it bends toward justice" -MLK Jr.
>> 
>> ______________________________________________
>> 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.

**********************
"The contents of this message do not reflect any position of the U.S. Government or NOAA."
**********************
Roy Mendelssohn
Supervisory Operations Research Analyst
NOAA/NMFS
Environmental Research Division
Southwest Fisheries Science Center
***Note new address and phone***
110 Shaffer Road
Santa Cruz, CA 95060
Phone: (831)-420-3666
Fax: (831) 420-3980
e-mail: Roy.Mendelssohn at noaa.gov www: http://www.pfeg.noaa.gov/

"Old age and treachery will overcome youth and skill."
"From those who have been given much, much will be expected" 
"the arc of the moral universe is long, but it bends toward justice" -MLK Jr.



More information about the R-help mailing list