Search code examples
rstacked-area-chart

Plot CSV as stacked area


I have a CSV file which looks like this:

Time ColA ColB ColC
0    1    10   5
1    3    7    15
2    0    8    9
3    3    4    5
4    4    5    6
5    10   23   4

I'd like to plot this as a stacked area chart, as follows (ignoring the X-axis labels):

Stacked area chart

But most packages seem to require several transformations of the data. Is there a way to simply specify an X column and the various Y columns to be stacked?


Solution

  • The following ended up working well enough.

    The reshape library pulls in the melt command which recognises the data into a format usable by ggplot.

    library(ggplot2)
    library(reshape)
    data=read.csv("out20",comment.char = "#",sep = "")
    mdata=melt(data,id=c("Time"))
    ggplot(mdata, aes(x=Time,y=value,group=variable,fill=variable)) + geom_area(position="fill")