ggplot2 geom_area重叠而不是堆叠 [英] ggplot2 geom_area overlapping instead of stacking

查看:162
本文介绍了ggplot2 geom_area重叠而不是堆叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成堆积区域图,但相反,ggplot会生成重叠区域。我已经尝试了其他类似于我的例子,但它们的工作原理并不适用于我。

 > cx 
日期类型访客
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 = visitor,fill = type),position =stack)

这给出了一个绘图,其中每种类型都绘制为它自己的区域,并且这些图层被叠加而不是叠加。如果我对它们进行正确的分类,那么我会在较大的范围内获得一系列较小的区域,但这不是我所追求的。



我尝试了不同的位置参数,无效。



如何获得堆积区域?

解决方案

<我找到了解决方案。问题是每个日期的每种类型都没有值,即有某些类型的类型没有入口的x值。



例如,类型= V在2010-01-17有游客= 20,游客在2010-01-19有= 22,所以我会为2010-01-18添加游客= 20。



我的数据是通过使用重塑包中的强制转换生成的,因此只需将add.missing = T设置为标志即可解决我的问题:

<$ (参观者融合,类型+日期〜,长度,添加失败= T)
名称(cx)[3]< - 访问者
cx < - ddply(cx,。(type),function(x)data.frame(date = x $ date,visitors = cumsum(x $ visitors)))


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?

解决方案

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

这篇关于ggplot2 geom_area重叠而不是堆叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆