[R] Combining ggplot2 objects and/or extracting layers

hadley wickham h.wickham at gmail.com
Wed Apr 28 19:33:15 CEST 2010


Hi Marshall,

It sounds like an interesting idea.  I would approach it in a slightly
different way.  I'd recommending writing a function that returns a
geom_bar object - that way you can easily add it on to any existing
plot, specifying the appropriate parameters.

If you just wanted to plot that layer, you could do ggplot() +
my_bar_function().

Hadley

PS.  Sorry for the delay in responding!

On Fri, Apr 9, 2010 at 9:21 AM, Marshall Feldman <marsh at uri.edu> wrote:
> Hi Hadley,
>
> Thanks for the terrific package!
>
> If you'd like I could give you my code, but conceptually what I'm trying to
> do is pretty simple.
>
> The chart on this page
> (http://www.businessinsider.com/20-reasons-why-the-us-economy-is-dying-and-is-simply-not-going-to-recover-2010-2#hard-to-find-jobs-3)
> is pretty typical. It shows a line chart of time series data against a
> backdrop of shaded bars that indicate periods of recession. This is what I'm
> doing.
>
> The tis package can do this and has a function that works with ggplot2.
> However, I see three problems with the approach in tis. (1) It only adds the
> bars to an existing plot being displayed. I would like to have it as a
> separate object that can be constructed once and added to any number of
> plots whether they are displayed or not.  (2) I'd like to see the bars by
> themselves on a plot. For consistency's sake, once I do this and am
> satisfied with the display, I don't want to have to and do a separate
> reconstruction. Instead, I want to take the bars from the satisfactory
> display. This way there's less room for accidentally breaking the
> consistency of the plots. (3) The tis plots are fixed in their format. They
> span the y dimension and have widths equal to the durations of the
> recessions. There are instances when one might like something different,
> such as stacked bars or multiple bars of varying heights (patterns, etc.)
> side-by-side that together have a width equal to the recession's duration.
>
> Obviously what I'm trying to do can be done with more work, but I'm trying
> to minimize unnecessary repetitions. I already coded a function that draws
> not only the recession bars but also that can draw bars whose height
> represents the value of some variable but with widths equal to the durations
> of the recessions. Once I create a free-standing plot, I'd like to be able
> to use it in various other contexts, including adding it to other existing
> plots. The alternative is to reconstruct the plot as a layer and add it to
> the other plots, but this is time-consuming and introduces more room for
> programming error.
>
> Thanks for your help.
>
>     Marsh
>
> On 4/9/2010 8:48 AM, hadley wickham wrote:
>
> Other then rebuilding the plots, is there any way either (1) to combine
> existing ggplot2 plots or (2) to extract a layer from an existing plot
> so that it can be added to another?
>
>
> Not really, although you can always pull apart the plot components.
> Can you give an example of what you are trying to achieve?
>
> Hadley
>
>
>
>



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



More information about the R-help mailing list