tags:

views:

60

answers:

1

I'm trying to generate a stacked area plot, but instead, ggplot makes overlapping areas. I've tried other examples that seems analogous to me, but they work and mine doesn't.

> cx
         date type visitors
1  2009-11-23    A        2
2  2010-01-07    A        4
3  2010-01-09    A        6
4  2010-02-07    A        8
5  2009-12-02    B        2
6  2009-12-03    B        4
7  2009-12-11    B        6
8  2010-01-20    B        8
9  2010-01-26    B       10
10 2010-01-30    B       11
11 2010-02-01    B       12
12 2009-12-07   LU        2
13 2009-12-28   LU        4
14 2010-01-27   LU        7
15 2010-02-04    L        1
16 2010-02-22    L        2
17 2009-11-14    O        2
18 2009-11-27    O        4
19 2010-01-11    O        6
20 2010-01-13    O        8
21 2010-02-10    O        9
22 2009-11-24    R        2
23 2009-12-01    R        4
24 2009-12-13    R        6
25 2009-12-14    R        8
26 2010-01-03    R       10
27 2010-01-16    R       12
28 2010-02-06    R       13
29 2010-02-08    R       15
30 2009-11-15    T        2
31 2009-11-19    T        4
32 2009-11-25    T        6
33 2009-11-26    T        8
34 2009-12-09    T       10
35 2009-12-10    T       12
36 2009-12-15    T       14
37 2009-12-19    T       16
38 2009-12-22    T       18
39 2010-02-23    T       19
40 2010-02-24    T       20
41 2010-01-21   Tr        2
42 2010-01-23   Tr        4
43 2010-01-24   Tr        6
44 2010-01-06    U        2
45 2009-11-09    V        2
46 2009-11-18    V        4
47 2009-12-16    V        6
48 2009-12-23    V        8
49 2009-12-25    V       10
50 2010-01-02    V       12
51 2010-01-12    V       14
52 2010-01-14    V       16
53 2010-01-15    V       18
54 2010-01-17    V       20
55 2010-01-19    V       22
56 2010-01-25    V       25
57 2010-02-05    V       26
> ggplot(cx) + geom_area(aes(x=date, y=visitors, fill=type), position="stack")

This gives a plot where each type is plotted as its own area, and these are overlaid instead of stacked. If I sort them right, I then get a series of smaller areas inside larger, but that's not what I'm after.

I've tried different arguments of position, to no avail.

How can I get the stacked areas?

+5  A: 

I found the solution. The problem is that there aren't values for each type for each date, i.e. there are x-values for which certain levels of type don't have an entry.

For instance, type=V has visitors=20 at 2010-01-17, and visitors=22 at 2010-01-19, so I would add visitors=20 for 2010-01-18 as well.

My data was generated by using cast from the reshape package, so just setting add.missing=T as a flag fixed my issues:

cx <- cast(visitors.melt, type+date~., length, add.missing=T)
names(cx)[3] <- "visitors"
cx <- ddply(cx, .(type), function(x) data.frame(date=x$date, visitors=cumsum(x$visitors)))
Daniel Eliasson