[R] ggplot2 stacked line plot

Steve Lianoglou mailinglist.honeypot at gmail.com
Mon Feb 8 06:47:02 CET 2010


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



More information about the R-help mailing list