Re: Plotting a barplot and legend separately

217 views
Skip to first unread message

Kohske Takahashi

unread,
Sep 26, 2012, 6:11:38 PM9/26/12
to jie zhao, ggp...@googlegroups.com
You can use the power of gtable like this:

```
p <- ggplot(mtcars, aes(cyl, fill = factor(gear))) + geom_bar() +
facet_wrap(~am+vs, nrow = 1)
gt <- ggplot_gtable(ggplot_build(p))

pushViewport(viewport(layout = grid.layout(nrow = 4, ncol = 4)))

pushViewport(viewport(layout.pos.row = 3))
grid.draw(gt[, -15])
popViewport()

pushViewport(viewport(layout.pos.row = 4, layout.pos.col = 4))
grid.draw(gt[, 15])
popViewport()
```

kohske

2012/9/22 jie zhao <jie...@gmail.com>:
> Layout <- grid.layout(nrow = 4, ncol = 4,
> widths = unit(1, "null"),
> heights = unit(c(0.4, 0.8, 1.2, 1.2), c("null", "null", "null")))



--
Kohske Takahashi <takahash...@gmail.com>

Assistant Professor,
Research Center for Advanced Science and Technology,
The University of Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

Winston Chang

unread,
Sep 26, 2012, 6:28:16 PM9/26/12
to Kohske Takahashi, jie zhao, ggp...@googlegroups.com
Building off of Kohske's example, you can pull out the pieces of the original gtable, and put them into another gtable, so you don't have to mess with grid viewports.


library(gtable)
library(ggplot2)

p <- ggplot(mtcars, aes(cyl, fill = factor(gear))) + geom_bar() + 
    facet_wrap(~am+vs, nrow = 1)
gt <- ggplot_gtable(ggplot_build(p))

# We can render gt
grid.newpage()
grid.draw(g)

# Display the layout of gt
gtable_show_layout(gt)



# Create the gtable with one col and two rows
g <- gtable(widths = unit(1, "null"), heights = unit(c(1, 1), "null"))

# Pull out all columns from gt, except the last column, which has the legend
# Put it on the first row of the new gtable
g <- gtable_add_grob(g, gt[, -15], t=1, l=1, b=1, r=1)

# Pull out the legend from gt, and put it in the second row of the new gtable
g <- gtable_add_grob(g, gt[4, 15], t=2, l=1, b=2, r=1)

# Draw the new gtable
grid.newpage()
grid.draw(g)

--Winston



--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Kohske Takahashi

unread,
Sep 26, 2012, 6:34:18 PM9/26/12
to Winston Chang, jie zhao, ggp...@googlegroups.com
Thanks Winston for the following up.

> g <- gtable(widths = unit(1, "null"), heights = unit(c(1, 1), "null"))

Just another topic, can we have gtable creation by setting nrow and ncol, like:

g <- gtable(nrow = 1, nrow = 2)
g <- gtable(nrow = 1, nrow = 2, default.units = "mm")

they are equivalent to:
g <- gtable(widths = unit(1, "null"), heights = unit(c(1, 1), "null"))
g <- gtable(widths = unit(1, "mm"), heights = unit(c(1, 1), "mm"))

kohske



2012/9/27 Winston Chang <winsto...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages