Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

r - geom_area produces blank areas between layers

I have the following data in data.frame tbl:

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))

I wanted to create a layered (stacked) area chart, using the following code:

ggplot(tbl,aes(x=year,y=`n()`,fill=Type)) +
geom_area(position="stack") +
theme_light() +
scale_colour_brewer(type="qual",palette = 1) +
ylab("Count")

But the result I'm getting has strange blank gaps in some places between the layers:

area chart

I found this question but that doesn't seem to be the issue I'm having - there's no negative numbers in my data.

Any ideas how to fix this?

Here's what seems to be going here: New Types are added and old ones removed from the full list at different times. When that happens, the area for these starts with a straight vertical line, but the area for the Type above this new one does diagonally from 0 to the new starting point for this layer. That doesn't sound like a reasonable behaviour to me. How do I get ggplot to plot all the colours one on top of the other rather than trying to create ribbons? I thought that was the whole point of geom_area...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are some (year, Type) combinations that are missing (implicit zeros). If you add them (explicit zeros), there are no empty areas.

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")

Created on 2019-03-28 by the reprex package (v0.2.1)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...