[R] ggplot2 stacked line plot

Liam Blanckenberg liam.blanckenberg at gmail.com
Wed Feb 10 05:43:04 CET 2010


Hi Hadley,

Thank you for taking the time to help me with this - I've constructed
the following example to illustrate my problem:

require(ggplot2)

data.set <- data.frame(
 Time = c(rep(1, 4),rep(2, 4), rep(3, 4), rep(4, 4)),
 Type = rep(c('a', 'b', 'c', 'd'), 4),
 Value = c(10, 12, 14, 16, 14, 14, 20, 18, 18, 16, 22, 20, 26, 20, 24, 26)
)

When I use the following code:

   ggplot(data.set, aes(x = Time, y = Value, colour = Type)) +
geom_area(aes(fill = Type), position = 'stack')

I get a very nice stacked area chart - each layer on the chart
represents a Type, with the total height of all the layers
representing the total Value across all Types for a given Time period
- this is what you would get numerically with the code:

   with(data.set, aggregate(Value, by = list(Time), sum))

I want to achieve exactly the same thing as this, but instead of
having a coloured area to represent each layer I just want a coloured
line for each layer. When I use the following code to try and achieve
this:

   ggplot(data.set, aes(x = Time, y = Value, colour = Type)) +
geom_line(aes(colour = Type), position = 'stack')

I get a warning/error message: "Missing ymax in position = 'stack'.
Maybe you want position = 'identity'?". The plot still renders, but
the lines are not stacked - the plot looks exactly as it would if the
following code was used instead:

   ggplot(data.set, aes(x = Time, y = Value, colour = Type)) +
geom_line(aes(colour = Type), position = 'identity')

I suspect that the "position = 'stack'" argument is being ignored and
'identity' is being used by default...

Hope this helps,

Liam




On Wed, Feb 10, 2010 at 10:12 AM, hadley wickham <h.wickham at gmail.com> wrote:
> Hi Liam,
>
> Yes, that's what that code should do.  Could you please send a small
> reproducible example?
>
> Hadley
>
> On Tue, Feb 9, 2010 at 4:21 PM, Liam Blanckenberg
> <liam.blanckenberg at gmail.com> wrote:
>> Hadley,
>>
>> Thanks for the pointing that error out to me. Unfortunately, revising
>> my code to:
>>
>> p <- ggplot2(~, aes(x = ~, y = ~, colour = Type)) +
>> geom_line(aes(colour = Type), position = 'stack')
>>
>> still does not generate what I'm after. I'm essentially after a line
>> plot where each 'Type' ('series' in excel lingo) is stacked in an
>> analogous fashion to a stacked area chart (i.e. geom_area(aes(Fill =
>> Type), position = 'stack')) - again using lines rather than areas
>> though.
>>
>> This chart should look just like a stacked area chart, however rather
>> than each 'layer' (representing a given 'Type') being filled with a
>> colour, each layer should have just a coloured line representing its
>> values. Summing each line vertically for a given value on the x-axis
>> across all the layers (i.e. 'Types') should then give the total y
>> value for that x value (just like vertically summing an area chart).
>> Really all I'm after is a stacked area chart where the fill for each
>> 'Type' has been removed, leaving only a line for each (stacked) Type's
>> value...
>>
>> I hope this is somewhat clear!
>>
>> Many thanks,
>>
>> Liam
>>
>>
>>
>>
>>
>> On Wed, Feb 10, 2010 at 1:53 AM, hadley wickham <h.wickham at gmail.com> wrote:
>>> Hi Liam,
>>>
>>> Your syntax is a little off.  You want:
>>>
>>> p <- ggplot2(~, aes(x = ~, y = ~, colour = Type)) +
>>>  geom_area(aes(fill = Type), position = 'stack')
>>>
>>> Position isn't an aesthetic.
>>>
>>> Hadley
>>>
>>> On Sun, Feb 7, 2010 at 10:40 PM, Liam Blanckenberg
>>> <liam.blanckenberg at gmail.com> wrote:
>>>> Hi all,
>>>>
>>>> I have been hunting around for hours trying to figure out how to
>>>> generate a stacked line chart using ggplot2. This type of chart can be
>>>> generated in excel 2007 by selecting: Chart type > Line > Stacked
>>>> line. I can generate a stacked area chart using the following code:
>>>>
>>>>   p <- ggplot2(~, aes(x = ~, y = ~, colour = Type)) +
>>>> geom_area(aes(position = 'stack', fill = Type))
>>>>
>>>> However, when I try and replicate this using the following code for geom_line:
>>>>
>>>>   p <- ggplot(~, aes(x = ~, y = ~, colour = Type)) +
>>>> geom_line(aes(position = 'stack'))
>>>>
>>>> the resulting plot is not stacked - i.e. each 'Type' is plotted at its
>>>> actual value rather than cumulatively to form a stacked chart... I
>>>> have poured through Hadley's ggplot2 book (ggplot2: elegant graphics
>>>> for data analysis), the R help list and also done general google
>>>> searching but cannot find a way to generate this type of plot.
>>>>
>>>> R version: 2.9.2
>>>> ggplot2 version: 0.8.5
>>>> OS: windows 7 (64-bit).
>>>>
>>>> Any suggestions or assistance would be greatly appreciated.
>>>>
>>>> Regards,
>>>>
>>>> Liam
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>
>>>
>>>
>>> --
>>> http://had.co.nz/
>>>
>>
>
>
>
> --
> http://had.co.nz/
>



More information about the R-help mailing list