[R] Creating grid graphics with grid.layout

Jeff Horner jeff.horner at vanderbilt.edu
Fri Sep 25 00:48:45 CEST 2009


Hi,

I recently created a function which uses grid with a viewport 
constructed with grid.layout() to position four plots on a plot device. 
My question is what's the best way to programatically traverse each 
element of the viewport?

The grid is 5x5 and the four plots occupy positions (2,2), (4,2), (2,4), 
and (4,4).

Here's the viewport code:

  pushViewport(
         viewport(
             layout=grid.layout(
                 nrow=5,
                 ncol=5,
                 widths=unit(
                     c(margin[2],1,margin[6],1,margin[4]),
                     c('npc','null','npc','null','npc')),
                 heights=unit(
                     c(margin[3],1,margin[5],1,margin[1]),
                     c('npc','null','npc','null','npc'))
             )
         )
     )

margin is a numeric vector of length 6 which resp defines the bottom, 
left, top, right, horizontal, and veritical margins (the cross hairs 
between the 4 plots). This is to allow the user to define how much 
margin should exist around all plots and between the plots.

I then use lapply to visit only those viewports to place the plots by 
specifying an x and y vector like so:

x <- c(2,4,2,4)
y <- c(2,2,4,4)
i <- 1
lapply(list(plot1,plot2,plot3,plot4),function(j){
    print(j,vp=viewport(layout.pos.col=x[i],layout.pos.row=y[i])
    i <<- i + 1
})

Looks kludgey. Is there a better way?

Also, I created debug code to visit those viewports where *nothing* is 
supposed to be printed except for a grey box with black edges:

z <- outer(logical(5),logical(5),"==")
for (i in 1:length(x)) z[x[i],y[i]] <- FALSE
for (i in 1:5){
    for (j in 1:5){
       if (z[i,j])
          grid.rect(gp=gpar(fill="grey"),
            vp=viewport(layout.pos.col=i,layout.pos.row=j))
    }
}

This looks kludgey as well. Any other ways outer there that are more 
elegant?

Thanks,

Jeff




More information about the R-help mailing list