ggplot2 geom_area重叠而不是堆叠

发布于 2024-08-24 06:31:32 字数 1946 浏览 4 评论 0原文

我正在尝试生成堆积区域图,但 ggplot 会生成重叠区域。我尝试过其他与我相似的例子,但它们有效,而我的则无效。

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

这给出了一个图,其中每种类型都绘制为自己的区域,并且这些区域是重叠的而不是堆叠的。如果我对它们进行正确的排序,我就会在较大的区域内得到一系列较小的区域,但这不是我想要的。

我尝试过不同的立场论证,但无济于事。

我怎样才能获得堆叠区域?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖鼻大婶 2024-08-31 06:31:32

我找到了解决方案。问题在于,每个日期的每种类型都没有值,即存在某些类型级别没有条目的 x 值。

例如,type=V 在 2010-01-17 的访问者=20,在 2010-01-19 的访问者=22,所以我也会为 2010-01-18 添加访问者=20。

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

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

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)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文