views:

597

answers:

4

I ran a Pig job on a Hadoop cluster that crunched a bunch of data down into something R can handle to do a cohort analysis. I have the following script, and as of the second to last line I have the data in the format:

> names(data)
[1] "VisitWeek" "ThingAge"    "MyMetric"

VisitWeek is a Date. ThingAge and MyMetric are integers.

The data looks like:

2010-02-07     49  12345

The script I have so far is:

# Load ggplot2 for charting 
library(ggplot2);

# Our file has headers - column names
data = read.table('weekly_cohorts.tsv',header=TRUE,sep="\t");

# Print the names
names(data)

# Convert to dates
data$VisitWeek = as.Date(data$VisitWeek)
data$ThingCreation = as.Date(data$ThingCreation)

# Fill in the age column
data$ThingAge = as.integer(data$VisitWeek - data$ThingCreation)

# Filter data to thing ages lt 10 weeks (70 days) + a sanity check for gt 0, and drop the creation week column
data = subset(data, data$ThingAge <= 70, c("VisitWeek","ThingAge","MyMetric"))
data = subset(data, data$ThingAge >= 0)

print(ggplot(data, aes(x=VisitWeek, y=MyMetric, fill=ThingAge)) + geom_area())

This last line does not work. I've tried lots of variations, bars, histograms, but as usual R docs defeat me.

I want it to show a standard Excel style stacked area chart - one time series for each ThingAge stacked across the weeks in the x axis, with the date on the y axis. An example of this kind of chart is here: http://upload.wikimedia.org/wikipedia/commons/a/a1/Mk_Zuwanderer.png

I've read the docs here: http://had.co.nz/ggplot2/geom_area.html and http://had.co.nz/ggplot2/geom_histogram.html and this blog http://chartsgraphs.wordpress.com/2008/10/05/r-lattice-plot-beats-excel-stacked-area-trend-chart/ but I can't quite make it work for me.

How can I achieve this?

+2  A: 

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

you need to give the geom_area a fill element and also stack it (though that might be a default)

found here http://www.mail-archive.com/[email protected]/msg84857.html

Dan
Thanks, that sounds reasonable, however - I still get a ribbon, not a stacked bar. Its a zig-zag ribbon - with only the largest color, 70 (red) filled in. This is what i was getting before, so I'm still stumped.
rjurney
+2  A: 

I was able to get my result with this:

I loaded the stackedPlot() function from https://stat.ethz.ch/pipermail/r-help/2005-August/077475.html

The function (not mine, see link) was:


stackedPlot = function(data, time=NULL, col=1:length(data), ...) {

  if (is.null(time))
    time = 1:length(data[[1]]);

  plot(0,0
       , xlim = range(time)
       , ylim = c(0,max(rowSums(data)))
       , t="n" 
       , ...
       );

  for (i in length(data):1) {

    # Die Summe bis zu aktuellen Spalte
    prep.data = rowSums(data[1:i]);

    # Das Polygon muss seinen ersten und letzten Punkt auf der Nulllinie haben
    prep.y = c(0
                , prep.data
                , 0
                )

    prep.x = c(time[1]
                , time
                , time[length(time)]
                )

    polygon(prep.x, prep.y
            , col=col[i]
            , border = NA
            );
  }
}

Then I reshaped my data to wide format. Then it worked!


wide = reshape(data, idvar="ThingAge", timevar="VisitWeek", direction="wide");
stackedPlot(wide);
rjurney
+2  A: 

Turning integers into factors and using geom_bar rather than geom_area worked for me:

df<-expand.grid(x=1:10,y=1:6)
df<-cbind(df,val=runif(60))
df$fx<-factor(df$x)
df$fy<-factor(df$y)
qplot(fy,val,fill=fx,data=df,geom='bar')
Jyotirmoy Bhattacharya
qplot(y,val,fill=fx,data=df,geom='area')gives you an area plot.
Jyotirmoy Bhattacharya
+3  A: 
library(ggplot2)
set.seed(134)
df <- data.frame(
    VisitWeek = rep(as.Date(seq(Sys.time(),length.out=5, by="1 day")),3),
    ThingAge = rep(1:3, each=5),
    MyMetric = sample(100, 15))

ggplot(df, aes(x=VisitWeek, y=MyMetric)) + 
    geom_area(aes(fill=factor(ThingAge)))

gives me the image below. I suspect your problem lies in correctly specifying the fill mapping for the area plot: fill=factor(ThingAge)

alt text

learnr
Thanks - this is much shorter than my solution. I have it working - but my bands are out of order. Working on sorting them now. This saved me about 80 lines of code. Bravo! :)
rjurney