geom_area 不堆叠(ggplot)
geom_area not stacking (ggplot)
这是我的数据结构:
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 ...
我正在尝试使用以下代码将其放入堆积面积图中:
ggplot(evidence.summary.main, aes(x = year, y = N, fill=studyType)) +
geom_area(alpha=.80)
为什么我无法正确堆叠它?
我的数据来自 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"))
我同意@untitled 的观点,问题很可能是缺失值。我一直在想必须有一个更清洁的转变,但我能想到的就是这个
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()
基本上为所有缺失的 year/study 组合填充零。
我认为有一个稍微更优雅的解决方案,它依赖于 tidyr::complete
,这是一个仅用于填充缺失级别的函数。
evidence.summary.main %>%
tidyr::complete(year, studyType, fill = list(N = 0)) %>%
ggplot(aes(x = as.numeric(year), y = N, fill = studyType)) +
geom_area(alpha = .8)
这是我的数据结构:
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 ...
我正在尝试使用以下代码将其放入堆积面积图中:
ggplot(evidence.summary.main, aes(x = year, y = N, fill=studyType)) +
geom_area(alpha=.80)
为什么我无法正确堆叠它?
我的数据来自 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"))
我同意@untitled 的观点,问题很可能是缺失值。我一直在想必须有一个更清洁的转变,但我能想到的就是这个
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()
基本上为所有缺失的 year/study 组合填充零。
我认为有一个稍微更优雅的解决方案,它依赖于 tidyr::complete
,这是一个仅用于填充缺失级别的函数。
evidence.summary.main %>%
tidyr::complete(year, studyType, fill = list(N = 0)) %>%
ggplot(aes(x = as.numeric(year), y = N, fill = studyType)) +
geom_area(alpha = .8)