用ggplot2创建不重叠的堆积区域图 [英] Create non-overlapping stacked area plot with ggplot2

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

问题描述

 > head(dat)$ b $我从网上抓取并处理了一些数据b计数名字情节百分比
1 309 don 01-a-little-kiss 0.27081507
2 220 megan 01-a-little-kiss 0.19281332
3 158 joan 01-a-little-kiss 0.13847502
4 113 peggy 01-a-little-kiss 0.09903593
5 107 roger 01-a-little-kiss 0.09377739
6 81 pete 01-a-little-kiss 0.07099036

我试图创建一个堆叠面积图表,类似于这里的:使用ggplot2制作堆积区域图



当我做了一个

  require(RCurl)
require(ggplot2)
link< - getURL( http://dl.dropbox.com/u/25609375/so_data/final.txt)
dat < - read.csv(textConnection(link),sep ='',header = FALSE,
col.names = c('count','name','episode'))

dat < - ddply(dat,。(episode),transform,percent = count / sum(count))

ggplot(dat,aes(episode,percent, group = name))+
geom_area(aes(fill = name,color = name),position ='stack')



我得到这张奇怪的图表。



我希望区域不要彼此交叉,并填充整个画布作为每个的总百分比。因子等于100%。

解决方案

这很有趣。你错过了一行(Lane没有出现在Tea Leaves ...中?),所以

  dat2< -  rbind(dat,data.frame(count = 0,name ='lane',
episode = '02 -tea-leaves',percent = 0))

ggplot(arrange( (aes(fill = name,group = name),position ='stack')
geom_area $ c>



似乎有效。但它也必须按照正确的顺序,我不完全确定为什么。


I have some data scraped and processed from the web in this form:

>head(dat)
  count  name          episode    percent
1   309   don 01-a-little-kiss 0.27081507
2   220 megan 01-a-little-kiss 0.19281332
3   158  joan 01-a-little-kiss 0.13847502
4   113 peggy 01-a-little-kiss 0.09903593
5   107 roger 01-a-little-kiss 0.09377739
6    81  pete 01-a-little-kiss 0.07099036

I'm trying to created a stacked area chart, similar to the one here: Making a stacked area plot using ggplot2

When I do a

require(RCurl)
require(ggplot2)
link <- getURL("http://dl.dropbox.com/u/25609375/so_data/final.txt")
dat <- read.csv(textConnection(link), sep=' ', header=FALSE, 
             col.names=c('count', 'name', 'episode'))

dat <- ddply(dat, .(episode), transform, percent = count / sum(count))

ggplot(dat, aes(episode, percent, group=name)) + 
     geom_area(aes(fill=name, colour=name), position='stack')

I get this bizarre chart.

I want the areas not to cross eachother, and to fill the entire canvas as the total percent for each episode factor equals 100%.

解决方案

That was interesting. You're missing a single row (Lane didn't appear in Tea Leaves...?), so

dat2 <- rbind(dat,data.frame(count = 0,name = 'lane',
                    episode = '02-tea-leaves',percent = 0))

ggplot(arrange(dat2,name,episode), aes(x = episode,y = percent)) + 
  geom_area(aes(fill=name,group = name), position='stack')

appears to work. But it had to be in the right order as well, and I'm not entirely sure why.

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

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