r - geom_area not stacking (ggplot) -


here structure of data:

classes ‘tbl_df’, ‘tbl’ , '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 stacked area chart using following code:

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

any ideas why cannot stack properly?

enter image description here

and data 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 @untitled problem missing values. keep thinking there must cleaner transformation, come 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) 

enter image description here

so used xtabs() fill in zeros missing year/study combinations.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -