geom_area 产生层与层之间的空白区域
geom_area produces blank areas between layers
我在data.frametbl
中有以下数据:
library(tibble)
tbl <- structure(list(
year = c(2007, 2008, 2008, 2009, 2009, 2010, 2010, 2010, 2011, 2011, 2011, 2011, 2012, 2012, 2012, 2013, 2013, 2013, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2018, 2018, 2018, NA),
Type = c("RLR", "PLR", "RLR", "PLR", "RLR", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "Supplement", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "LR", "PLR", "RLR", "RR", "LR", "Other", "PLR", "RR", "LR", "Other", "RR", "LR", "Other", "RR", "RR"),
`n()` = c(4L, 2L, 35L, 14L, 41L, 34L, 37L, 61L, 40L, 21L, 149L, 1L, 38L, 17L, 134L, 41L, 4L, 115L, 23L, 3L, 76L, 1L, 27L, 2L, 78L, 28L, 2L, 4L, 36L, 33L, 5L, 32L, 22L, 3L, 36L, 5L)),
row.names = c(NA, -36L),
class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
groups = structure(list(
year = c(2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, NA),
.rows = list(1L, 2:3, 4:5, 6:8, 9:12, 13:15, 16:18, 19:21, 22:25, 26:29, 30:32, 33:35, 36L)),
row.names = c(NA, -13L),
class = c("tbl_df", "tbl", "data.frame"),
.drop = TRUE))
我想创建一个分层(堆叠)面积图,使用以下代码:
ggplot(tbl,aes(x=year,y=`n()`,fill=Type)) +
geom_area(position="stack") +
theme_light() +
scale_colour_brewer(type="qual",palette = 1) +
ylab("Count")
但是我得到的结果在图层之间的某些地方有奇怪的空白:
我发现 this question 但这似乎不是我遇到的问题 - 我的数据中没有负数。
有什么解决办法吗?
这似乎是这里发生的事情:在不同的时间添加新的 Type
和从完整列表中删除旧的。当发生这种情况时,这些区域以垂直直线开始,但是这个新区域上方的 Type
区域从 0 到该层的新起点呈对角线。对我来说,这听起来不像是一种合理的行为。我如何让 ggplot 将所有颜色绘制在另一种之上而不是尝试创建色带?我认为这就是 geom_area
...
的重点
缺少一些(年份、类型)组合(隐式零)。如果添加它们(显式零),则没有空白区域。
library(dplyr) # for %>% operator
library(tidyr) # for drop_na function
# Your data here....
tbl <- tbl %>%
# Ungroup or `complete` won't work as expected
ungroup() %>%
# There is one NA year
drop_na() %>%
# Add all (Type, year) combinations, filling in with 0s where `n()` is not observed
complete(Type, year, fill = list(`n()` = 0))
ggplot(tbl, aes(x=year,y=`n()`, group=Type, fill=Type)) +
geom_area(alpha = 0.5) +
theme_light() +
scale_colour_brewer(type="qual",palette = 1) +
ylab("Count")
由 reprex package (v0.2.1)
于 2019-03-28 创建
我在data.frametbl
中有以下数据:
library(tibble)
tbl <- structure(list(
year = c(2007, 2008, 2008, 2009, 2009, 2010, 2010, 2010, 2011, 2011, 2011, 2011, 2012, 2012, 2012, 2013, 2013, 2013, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2018, 2018, 2018, NA),
Type = c("RLR", "PLR", "RLR", "PLR", "RLR", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "Supplement", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "LR", "PLR", "RLR", "RR", "LR", "Other", "PLR", "RR", "LR", "Other", "RR", "LR", "Other", "RR", "RR"),
`n()` = c(4L, 2L, 35L, 14L, 41L, 34L, 37L, 61L, 40L, 21L, 149L, 1L, 38L, 17L, 134L, 41L, 4L, 115L, 23L, 3L, 76L, 1L, 27L, 2L, 78L, 28L, 2L, 4L, 36L, 33L, 5L, 32L, 22L, 3L, 36L, 5L)),
row.names = c(NA, -36L),
class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
groups = structure(list(
year = c(2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, NA),
.rows = list(1L, 2:3, 4:5, 6:8, 9:12, 13:15, 16:18, 19:21, 22:25, 26:29, 30:32, 33:35, 36L)),
row.names = c(NA, -13L),
class = c("tbl_df", "tbl", "data.frame"),
.drop = TRUE))
我想创建一个分层(堆叠)面积图,使用以下代码:
ggplot(tbl,aes(x=year,y=`n()`,fill=Type)) +
geom_area(position="stack") +
theme_light() +
scale_colour_brewer(type="qual",palette = 1) +
ylab("Count")
但是我得到的结果在图层之间的某些地方有奇怪的空白:
我发现 this question 但这似乎不是我遇到的问题 - 我的数据中没有负数。
有什么解决办法吗?
这似乎是这里发生的事情:在不同的时间添加新的 Type
和从完整列表中删除旧的。当发生这种情况时,这些区域以垂直直线开始,但是这个新区域上方的 Type
区域从 0 到该层的新起点呈对角线。对我来说,这听起来不像是一种合理的行为。我如何让 ggplot 将所有颜色绘制在另一种之上而不是尝试创建色带?我认为这就是 geom_area
...
缺少一些(年份、类型)组合(隐式零)。如果添加它们(显式零),则没有空白区域。
library(dplyr) # for %>% operator
library(tidyr) # for drop_na function
# Your data here....
tbl <- tbl %>%
# Ungroup or `complete` won't work as expected
ungroup() %>%
# There is one NA year
drop_na() %>%
# Add all (Type, year) combinations, filling in with 0s where `n()` is not observed
complete(Type, year, fill = list(`n()` = 0))
ggplot(tbl, aes(x=year,y=`n()`, group=Type, fill=Type)) +
geom_area(alpha = 0.5) +
theme_light() +
scale_colour_brewer(type="qual",palette = 1) +
ylab("Count")
由 reprex package (v0.2.1)
于 2019-03-28 创建