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

Hi, On Sun, Feb 7, 2010 at 11: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.Are you trying to show a graph that looks like Figure 4.5 from this page? http://learnr.wordpress.com/2009/07/02/ggplot2-version-of-figures-in-lattice-multivariate-data-visualization-with-r-part-4/ sans the coord_flip(), perhaps? That website is a good resource for ggplot graphics. He ran a whole series recreating the graphs in the lattice graphics book with ggplot2. His final post on that subject included a link to a pdf with the code and graphics for all the posts in that series for easy scanning, too. If this isn''t the graph you wanted, perhaps you can skim that document to see if there''s a graphic that resembles what you''re after. -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact

On 02/08/2010 03:40 PM, Liam Blanckenberg 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). >Hi Liam, Are you looking for something like stackpoly (plotrix package)? It''s not ggplot, but it might do what you want. Jim

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/

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

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/

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

Hi: This is closer, but it is still not what you want; it does, however, show what geom_area is doing when it renders the plot, as can be seen by the colors. 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) ) ggplot(data.set, aes(x = Time, y = Value, group = Type)) + geom_area(aes(color = Type), fill = ''transparent'', position = ''stack'') You can see that a polygon is drawn (and normally filled) for each group, but the line colors are not what you want and the polygon edges are not what you want, either. I''d suggest using cumsum() to get cumulative totals and using geom_line() instead; it would probably be less work...as in: library(ggplot2) data2 <- ddply(data.set, .(Time), transform, Csum = cumsum(Value)) ggplot(data2, aes(x = Time, y = Csum, color = Type)) + geom_line() HTH, Dennis On Tue, Feb 9, 2010 at 8:43 PM, Liam Blanckenberg < liam.blanckenberg@gmail.com> wrote:> 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@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@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@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@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@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/ > > > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]

Many thanks for you help on this Dennis. Liam On Thu, Feb 11, 2010 at 6:02 AM, Dennis Murphy <djmuser at gmail.com> wrote:> Hi: > > This is closer, but it is still not what you want; it does, however, show > what > geom_area is doing when it renders the plot, as can be seen by the colors. > > 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) > ?) > ggplot(data.set, aes(x = Time, y = Value, group = Type)) + > ? geom_area(aes(color = Type), fill = ''transparent'', position = ''stack'') > > You can see that a polygon is drawn (and normally filled) for each group, > but the line colors are not what you want and the polygon edges are not > what you want, either. I''d suggest using cumsum() to get cumulative totals > and using geom_line() instead; it would probably be less work...as in: > > library(ggplot2) > data2 <- ddply(data.set, .(Time), transform, Csum = cumsum(Value)) > ggplot(data2, aes(x = Time, y = Csum, color = Type)) + geom_line() > > HTH, > Dennis > > On Tue, Feb 9, 2010 at 8:43 PM, Liam Blanckenberg > <liam.blanckenberg at gmail.com> wrote: >> >> 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/ >> > >> >> ______________________________________________ >> 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. > >