开发者

Stacked Area Histogram in R

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?


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)

Stacked Area Histogram in R


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/r-help@r-project.org/msg84857.html


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);


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')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜