使用ggplot2制作堆积区域图 [英] Making a stacked area plot using ggplot2

查看:199
本文介绍了使用ggplot2制作堆积区域图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下数据结构来尝试制作堆积区域图:

  df<  -  data。框架(PopDen = c(0.002279892,0.002885407,0.004291351,0.002457731,0.006631572,0.007578882,0.004465446,0.007436628,0.009001456,0.006951703,0.003602076,0.005695585,0.005819783,0.007412274,0.004931548,0.006257411,0.008635908,0.005438558,0.002251421,0.006438558),DomArea = c (253500,135270,197180,131590,142210,166920,125640,184600,139940,126280,127760,190940,133440,143510,117260,69340,143620,127480,181970,164180),PR_Cat = c(高,高 高低低低低低低高高中中中低 ),低,中,中,低,低,低))

p < - ggplot(df,aes(PopDen,order(DomArea) ,color = PR_Cat))
p + geom_area(aes(color = PR_Cat,fill = PR_Cat),position ='stack')

但是,我不明白如何堆叠e上的区域其他;此刻它们重叠。我假设在这里需要一个 position ='stack'参数,但是不管它是否被包含,剧情看起来都是相同的。

另外,是否可以通过 PR_Cat 中的某个类别订购 DomArea ,或者我是否需要重组我的数据?

解决方案

我不确定你在这里绘制什么,但是你不想绘图沿着 y 轴而不是 x 轴放置 PopDen 您可以使用 ddply 来为每个 PR_Cat 类别订购 DomArea plyr 包,然后堆叠工作如下:
编辑
I意识到你可能希望剧情按照 Low,Medium High 的顺序堆放,所以我们需要先对 PR_Cat b
$ b

  df $ PR_Cat < - 有序(df $ PR_Cat,levels = c('Low ','Medium','High'))

现在创建 DomAreaByCat 列使用 ddply

  df <  -  ddply(df,。(PR_Cat),transform,DomAreaByCat = order(DomArea))

您的 df 将如下所示:

 > df 
PopDen DomArea PR_Cat DomAreaByCat
1 0.004291351 197180低8
2 0.002457731 131590低5
0.006631572 142210低9
4 0.007578882 166920低2
5 0.004465446 125640最低3
6 0.007436628 184600最低7
7 0.007412274 143510最低11 $ b $ 8 0.004931548 117260最低4
9 0.005438558 127480最低10
10 0.002251421 181970最低6
11 0.006438558 164180低1
12 0.003602076 127760中4 4
13 0.005695585 190940中1
14 0.005819783 133440中3
0.006257411 69340中5
16 0.008635908 143620中2
17 0.002279892 253500高4 $ b $ 18 0.002885407 135270高2
19 0.009001456 139940高3
0.006951703 126280高1

然后您可以像这样做堆积区图:

  p < -  ggplot(df,aes(DomAreaByCat,PopDen))

p + geom_area(aes(color = PR_Cat,fill = PR_CAT),position ='stack')


I'm using the following data structure to try and make a stacked area chart:

df <- data.frame(PopDen = c( 0.002279892, 0.002885407, 0.004291351, 0.002457731, 0.006631572, 0.007578882, 0.004465446, 0.007436628, 0.009001456, 0.006951703, 0.003602076, 0.005695585, 0.005819783, 0.007412274, 0.004931548, 0.006257411, 0.008635908, 0.005438558, 0.002251421,0.006438558), DomArea = c( 253500, 135270, 197180, 131590, 142210, 166920, 125640, 184600, 139940, 126280, 127760, 190940, 133440, 143510, 117260, 69340, 143620, 127480, 181970,164180), PR_Cat = c( "High", "High", "Low", "Low", "Low", "Low", "Low", "Low", "High", "High", "Medium", "Medium", "Medium", "Low", "Low", "Medium", "Medium", "Low", "Low","Low") )

p <- ggplot(df, aes(PopDen, order(DomArea), colour = PR_Cat))  
p + geom_area(aes(colour = PR_Cat, fill= PR_Cat), position = 'stack')

However, I don't understand how to stack the areas on top of each other; at the moment they are overlapping. I assume that I need a position = 'stack' argument here, but the plot looks the same whether it is included or not.

Also, is it possible to order DomArea by one of the categories in PR_Cator would I need to reorganize my data?

解决方案

I'm not sure what you are plotting here, but don't you want to be plotting PopDen along the y axis rather than the x axis? You can order the DomArea by each PR_Cat category using ddply from the plyr package, and then the stacking works as follows: EDIT I realized you probably want the plot to be stacked in the order Low, Medium High, so we need to first force this ordering on the PR_Cat factor by doing:

df$PR_Cat <- ordered( df$PR_Cat, levels = c('Low', 'Medium', 'High'))

And now create the DomAreaByCat column using ddply:

df <- ddply(df, .(PR_Cat), transform, DomAreaByCat = order(DomArea))

Your df will look like this:

> df
        PopDen DomArea PR_Cat DomAreaByCat
1  0.004291351  197180    Low            8
2  0.002457731  131590    Low            5
3  0.006631572  142210    Low            9
4  0.007578882  166920    Low            2
5  0.004465446  125640    Low            3
6  0.007436628  184600    Low            7
7  0.007412274  143510    Low           11
8  0.004931548  117260    Low            4
9  0.005438558  127480    Low           10
10 0.002251421  181970    Low            6
11 0.006438558  164180    Low            1
12 0.003602076  127760 Medium            4
13 0.005695585  190940 Medium            1
14 0.005819783  133440 Medium            3
15 0.006257411   69340 Medium            5
16 0.008635908  143620 Medium            2
17 0.002279892  253500   High            4
18 0.002885407  135270   High            2
19 0.009001456  139940   High            3
20 0.006951703  126280   High            1

And then you can do the stacked area plot like this:

p <- ggplot(df, aes( DomAreaByCat, PopDen))

p + geom_area(aes(colour = PR_Cat, fill= PR_Cat), position = 'stack')   

这篇关于使用ggplot2制作堆积区域图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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