Extracting elements from ggplot figures

1,052 views
Skip to first unread message

Raivo Kolde

unread,
Jun 7, 2012, 8:32:08 AM6/7/12
to ggplot2 support
Hi

I'm writing a package where I combine together several plots using grid graphics. Some elements of the picture will be drawn by ggplot2. My problem is that I do not want to use the whole ggplot2 picture but only the actual plot area without axes, guides and all other things, plus I want to extract the legend and place it independently from other elements.

One approach that worked reasonably well with a bit older version of ggplot2 was to save a plot into a variable p = qplot(x, y, colour = z) and then extract the plot area

editGrob(getGrob(ggplotGrob(p), gPath("panel-3-3"), grep = TRUE), vp=viewport())

and the legend area as

editGrob(getGrob(ggplotGrob(p), gPath("legend.frame"), grep = TRUE), vp=viewport())

Although it worked, it does not seem to be a really good practice to extract the elements like this, especially when doing package development. This code also broke down when I updated the ggplot version. So now my question would be, how to extract these parts of the ggplot figure in an appropriate way that would be stable over present and future versions of ggplot2.

Best,
Raivo

Hadley Wickham

unread,
Jun 7, 2012, 10:44:16 AM6/7/12
to Raivo Kolde, ggplot2 support
Hi Raivo,

Have a look at the gtable package - https://github.com/hadley/gtable.
Some older code is currently include in ggplot2, but we're starting to
port ggplot2 to use this package - it should make it easy to extract
parts of the plot that you want.

Hadley
> --
> 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



--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Raivo Kolde

unread,
Jun 8, 2012, 5:29:01 AM6/8/12
to Hadley Wickham, ggplot2 support
Thanks for the suggestion. The gtable package looks very promising and I probably should port some of my code to this as well.

Using the gtable_filter I was able to extract the panel and legend. Now to create sufficient amount of room for the legend I would have to know its size. Ideally I would like to calculate it using grobWidth and grobHeight, however this gives me an error. Then I noticed the widths and heights slots in gtable. The widths seem to be adequate, but height is something like 0.5cm for a rather large legend. What would be the correct way to calculate the height of the legend? Have I missed some functions in gtable that would do it for me?

Also when trying to extract guide-box from a figure without one:

gtable_filter(ggplot_gtable(ggplot_build(qplot(1))), "guide-box")

I get an error, I think It would be nicer to get some type of NULL value as a result.

Best,
Raivo

Hadley Wickham

unread,
Jun 25, 2012, 6:39:37 PM6/25/12
to Raivo Kolde, Winston Chang, ggplot2 support
> Using the gtable_filter I was able to extract the panel and legend. Now to create sufficient amount of room for the legend I would have to know its size. Ideally I would like to calculate it using grobWidth and grobHeight, however this gives me an error. Then I noticed the widths and heights slots in gtable. The widths seem to be adequate, but height is something like 0.5cm for a rather large legend. What would be the correct way to calculate the height of the legend? Have I missed some functions in gtable that would do it for me?

Oh hmmm, that's probably because the height isn't calculated correctly
for the legend. Winston, can you please look into as part of the
gtable stuff?

> Also when trying to extract guide-box from a figure without one:
>
> gtable_filter(ggplot_gtable(ggplot_build(qplot(1))), "guide-box")
>
> I get an error, I think It would be nicer to get some type of NULL value as a result.

And similarly for this?

Hadley

Kohske Takahashi

unread,
Jun 25, 2012, 9:03:06 PM6/25/12
to Hadley Wickham, Raivo Kolde, Winston Chang, ggplot2 support
Hi,

>> Using the gtable_filter I was able to extract the panel and legend. Now to create sufficient amount of room for the legend I would have to know its size. Ideally I would like to calculate it using grobWidth and grobHeight, however this gives me an error. Then I noticed the widths and heights slots in gtable. The widths seem to be adequate, but height is something like 0.5cm for a rather large legend. What would be the correct way to calculate the height of the legend? Have I missed some functions in gtable that would do it for me?
>
> Oh hmmm, that's probably because the height isn't calculated correctly
> for the legend.  Winston, can you please look into as part of the
> gtable stuff?

The legend, place left (or right) side of the panel, has height of
1null. This means the height of the grob (this is gTree) is determined
by main panel.
In the case of top/bottom legend, the width is 1null.

You may get the height by accessing the height element of the gTree:

d> p <- qplot(1:3, 1:3, colour = 1:3, size = 1:3)
d> r <- gtable_filter(ggplot_gtable(ggplot_build(p)), "guide-box")
d> gg <- r$grobs[[1]]
d> gg$height
[1] sum(0.5lines, sum(1.5mm, 2.42424488799749mm, 2.032mm, 40.64mm,
1.5mm), 0.5lines, sum(1.5mm, 2.42424488799749mm, 2.032mm, 8.128mm,
8.128mm, 8.128mm, 8.128mm, 8.128mm, 1.5mm), 0.5lines)

kohske


2012/6/26 Hadley Wickham <had...@rice.edu>:
> --
> 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 <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
Reply all
Reply to author
Forward
0 new messages