geom_area不能堆叠(ggplot) [英] geom_area not stacking (ggplot)

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

问题描述

以下是我的数据结构:

$ p $ 类'tbl_df','tbl'和'data.frame': 60 obs。 3个变量:
$ year:num 1990 1990 1991 1992 1993 ...
$ studyType:因子w / 4等级meta,observational,..:2 3 3 1 3 3 3 1 3 3 ...
$ N:int 1 4 4 1 2 5 3 1 2 6 ...

我试图用下面的代码把它放到一个堆积区域图表中:

  ggplot .summary.main,aes(x = year,y = N,fill = studyType))+ 
geom_area(alpha = .80)

有什么想法,为什么我无法正确堆叠它?





我的数据来自 dput

 <$ c (年份= c(1990年,1990年,1991年,1992年,1993年,1994年,1995年,
1996年,1996年,1997年,1997年,1998年,1998年,1999年,1999年,2000年,2000年,2000年,2000年,
2001,2001,2002,2002,2003,2003,2003,2004,2004,2004,2005,
2005,2006,2006,2006,2007,2007,2007,2007,2008,2008 ,2008 bb b 2009,2009,2009,2010,2010,2010,2011,2011,2011 b $ b 2011,2012,2012,2012,2012,2013,2013,2013。 ),studyType = structure(c(2L,
3L,3L,1L,3L,3L,3L,1L,3L,3L,4L,1L,3L,1L,3L,1L,2L,
3L,1L,3L,1L,3L,1L,3L,4L,1L,2L,3L,3L,4L,1L,3L,4L,
1L,2L,3L,4L, 1L,2L,3L,4L,1L,2L,3L,4L,1L,
2L,3L,4L,1L,2L,3L,4L,1L,2L,3L,4L) 元,
观察,rct,sr),class =factor),N = c(1L,4L,
4L,1L,2L,5L,3 L,1L,2L,6L,1L,2L,2L,1L,5L,3L,1L,1L,
4L,6L,1L,10L,2L,6L,3L,1L,2L,8L, 2L,3L,12L,4L,
3L,1L,9L,5L,4L,7L,4L,4L,1L,16L,8L,9L,2L,17L,7L, 15L,16L,3L,2L,19L,14L,8L,2L,28L,20L)),row.names = c(NA,
-60L),.Names = c(year,studyType ,N),class = c(tbl_df,
tbl,data.frame))

$ b

解决方案

我同意@ntitled,这个问题可能是缺少的值。我一直认为必须有一个更清洁的转型,但我所能想到的是

  ggplot(as.data.frame (xtabs(N〜year + studyType,evidence.summary.main)),
aes(x = as.numeric(year),y = Freq,fill = studyType))+
geom_area(alpha = .80)

xtabs()来基本填充所有缺失年份/研究组合的零。

Here is the structure of my data:

Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   60 obs. of  3 variables:
$ year     : num  1990 1990 1991 1992 1993 ...
$ studyType: Factor w/ 4 levels "meta","observational",..: 2 3 3 1 3 3 3 1 3 3 ...
$ N        : int  1 4 4 1 2 5 3 1 2 6 ...

I'm trying to get this into a stacked area chart using the following code:

ggplot(evidence.summary.main, aes(x = year, y = N, fill=studyType)) + 
geom_area(alpha=.80) 

Any ideas why I cannot get this to stack properly?

And my data from dput:

structure(list(year = c(1990, 1990, 1991, 1992, 1993, 1994, 1995, 
1996, 1996, 1997, 1997, 1998, 1998, 1999, 1999, 2000, 2000, 2000, 
2001, 2001, 2002, 2002, 2003, 2003, 2003, 2004, 2004, 2004, 2005, 
2005, 2006, 2006, 2006, 2007, 2007, 2007, 2007, 2008, 2008, 2008, 
2009, 2009, 2009, 2009, 2010, 2010, 2010, 2010, 2011, 2011, 2011, 
2011, 2012, 2012, 2012, 2012, 2013, 2013, 2013, 2013), studyType = structure(c(2L, 
3L, 3L, 1L, 3L, 3L, 3L, 1L, 3L, 3L, 4L, 1L, 3L, 1L, 3L, 1L, 2L, 
3L, 1L, 3L, 1L, 3L, 1L, 3L, 4L, 1L, 2L, 3L, 3L, 4L, 1L, 3L, 4L, 
1L, 2L, 3L, 4L, 1L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("meta", 
"observational", "rct", "sr"), class = "factor"), N = c(1L, 4L, 
4L, 1L, 2L, 5L, 3L, 1L, 2L, 6L, 1L, 2L, 2L, 1L, 5L, 3L, 1L, 1L, 
4L, 6L, 1L, 10L, 2L, 6L, 3L, 1L, 2L, 8L, 12L, 2L, 3L, 12L, 4L, 
3L, 1L, 9L, 5L, 4L, 7L, 4L, 4L, 1L, 16L, 8L, 9L, 2L, 17L, 7L, 
6L, 2L, 15L, 16L, 3L, 2L, 19L, 14L, 8L, 2L, 28L, 20L)), row.names = c(NA, 
-60L), .Names = c("year", "studyType", "N"), class = c("tbl_df", 
"tbl", "data.frame"))

解决方案

I agree with @untitled that the problem is likely the missing values. I keep thinking there must be a cleaner transformation, but all I could come up with is this

ggplot(as.data.frame(xtabs(N~year+studyType, evidence.summary.main)), 
    aes(x = as.numeric(year), y = Freq, fill=studyType)) + 
geom_area(alpha=.80)

So I used xtabs() to basically fill in zeros for all the missing year/study combinations.

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

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆