grid.layout Question

779 views
Skip to first unread message

Nick S

unread,
Jul 19, 2010, 1:07:32 PM7/19/10
to ggplot2
Is there a way to split a single row of a grid.layout into multiple
columns?

I am currently producing a graphic and also displaying a table of
summary data beneath it by using grid.layout to "stitch" them
together. The table under the graph is getting a bit crowded and I
would like to split it into two tables that are 1/2 the length of the
graph above.

I am setting the layout with the following code:
Layout = grid.layout(nrow=2,ncol=1,heights =
unit(c(2,.5),c("null","null")))
grid.show.layout(Layout)

I would like to split the bottom section (2,1) in half so that it
makes two columns (instead of one long one) without splitting the top
portion into two columns as well.

Is this possible? Any help is appreciated!

baptiste auguie

unread,
Jul 19, 2010, 3:50:40 PM7/19/10
to Nick S, ggplot2
Hi,

It is certainly possible. I can think of three ways of doing it, as
illustrated below.


## create a list of three grobs to illustrate the placement
foo <- function(.label) gTree(children=gList(rectGrob(), textGrob(.label)))
g <- lapply(c("this is", "split", "in 3"), foo)

## lazy approach: use grid.arrange twice (arrange two grobs in a row,
## then grid.arrange the result with the last grob)

library(gridExtra)
gg <- arrangeGrob(arrangeGrob(g[[1]], g[[2]], ncol=2), g[[3]],
heights=unit(c(2,.5),c("null","null")))

## result:
## grid.draw(gg)

## second technique, split the page in 4, and specify that you want to use two
## regions when drawing
Layout = grid.layout(nrow=2,ncol=2,heights = unit(c(2,.5),c("null","null")))

## last technique, a first layout split in rows
## then push a second split in columns
Layout2 = grid.layout(nrow=2,ncol=1,heights = unit(c(2,.5),c("null","null")))
Layout3 = grid.layout(nrow=1,ncol=2)


grid.newpage()

## push the 2x2 layout
pushViewport(viewport(layout=Layout))
## draw in the first quadrant
pushViewport(viewport(layout.pos.row=1, layout.pos.col=1))
grid.draw(gg)
upViewport()
## push the Layout2
pushViewport(viewport(layout=Layout2, layout.pos.row=1, layout.pos.col=2))
## then the Layout3
pushViewport(viewport(layout=Layout3, layout.pos.row=1, layout.pos.col=1))
## now draw in the first cell
pushViewport(viewport(layout.pos.row=1, layout.pos.col=1))
grid.draw(g[[1]])
upViewport()
## second cell
pushViewport(viewport(layout.pos.row=1, layout.pos.col=2))
grid.draw(g[[2]])
upViewport()
upViewport()
## last cell
pushViewport(viewport(layout.pos.row=2, layout.pos.col=1))
grid.draw(g[[3]])
upViewport()
upViewport()
## finally: drawing fills two cells
pushViewport(viewport(layout.pos.row=2, layout.pos.col=1:2))
grid.text("confused yet? ;)")
upViewport()

HTH,

baptiste

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

Nick S

unread,
Jul 19, 2010, 7:42:32 PM7/19/10
to ggplot2
Thanks for the reply.

I have gotten as far as the arrangeGrob function. What package is this
a part of? I installed gridExtra from the standard R package list and
it still throws the following error: Error: could not find function
"arrangeGrob"

Dennis Murphy

unread,
Jul 19, 2010, 9:50:49 PM7/19/10
to Nick S, ggplot2
Hi:

This sounds like the example on p. 154 of Hadley's book:

library(ggplot2)
a <- qplot(date, unemploy, data = economics, geom = "line")
b <- qplot(uempmed, unemploy, data = economics) +
  geom_smooth(se = F)
c <- qplot(uempmed, unemploy, data = economics, geom="path")

grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 2)))

vplayout <- function(x, y)
  viewport(layout.pos.row = x, layout.pos.col = y)
print(a, vp = vplayout(1, 1:2))
print(b, vp = vplayout(2, 1))
print(c, vp = vplayout(2, 2))

Some modification of this idea, perhaps?

BTW, I had the same problem as Nick re arrangeGrob(); here's my sessionInfo():

> sessionInfo()
R version 2.11.1 (2010-05-31)
i386-pc-mingw32

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252  
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                         
[5] LC_TIME=English_United States.1252   

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods 
[8] base    

other attached packages:
[1] gridExtra_0.5 ggplot2_0.8.7 digest_0.4.2  reshape_0.8.3 plyr_0.1.9  
[6] proto_0.3-8   sos_1.2-7     brew_1.0-3  

loaded via a namespace (and not attached):
[1] tools_2.11.1

Is arrangeGrob() in a development version of gridExtra?

HTH,
Dennis

baptiste auguie

unread,
Jul 20, 2010, 1:37:11 AM7/20/10
to Nick S, ggplot2
Hi,

I forgot to mention it, grid.arrange() and arrangeGrob() are in
version 0.6.5 of gridExtra, available at
http://r-forge.r-project.org/R/?group_id=506 . Unfortunately it seems
that the version on CRAN has not yet been updated.

The function used to be called arrange() but the name was clashing
with a new plyr function.

Note that the other two techniques are independent of this package
(just replace gg by whatever grob to run it).

baptiste

Nick S

unread,
Jul 20, 2010, 12:13:21 PM7/20/10
to ggplot2
Thanks again for the reply's and pointing out that this exact scenario
is presented in the book :)

Once I installed the gridExtra package from the R-forge
repository...The code Baptiste provided worked like a charm.

Is there a comprehensive resource that anyone can recommend so that I
can become more familiar with the options for grid?

On Jul 19, 10:37 pm, baptiste auguie <bapt4...@googlemail.com> wrote:
> Hi,
>
> I forgot to mention it, grid.arrange() and arrangeGrob() are in
> version 0.6.5 of gridExtra, available athttp://r-forge.r-project.org/R/?group_id=506. Unfortunately it seems
> that the version on CRAN has not yet been updated.
>
> The function used to be called arrange() but the name was clashing
> with a new plyr function.
>
> Note that the other two techniques are independent of this package
> (just replace gg by whatever grob to run it).
>
> baptiste
>

baptiste auguie

unread,
Jul 20, 2010, 12:21:34 PM7/20/10
to Nick S, ggplot2
Glad you got it to work.

The 'R graphics' book by Paul Murrell is the authoritative document, I think.

baptiste

Reply all
Reply to author
Forward
0 new messages