corybrunson / ggalluvial

ggplot2 extension for alluvial plots
http://corybrunson.github.io/ggalluvial/
GNU General Public License v3.0
499 stars 34 forks source link

handling missing lodes in 'geom_flow()' #12

Closed corybrunson closed 6 years ago

corybrunson commented 6 years ago

stat_alluvium() makes do when some lodes are NA or missing, but stat_flow() gives up when the lodes at two adjacent axes fail to match up exactly:

library(ggalluvial)
#> Loading required package: ggplot2
d1 <- data.frame(
  key = rep(letters[1:2], each = 2),
  value = c(1, NA, 1, 1),
  id = rep(LETTERS[1:2], times = 2)
)
ggplot(d1, aes(x = key, stratum = value, alluvium = id)) +
  geom_alluvium(aes(fill = id), na.rm = TRUE) + geom_stratum(na.rm = TRUE)

ggplot(d1, aes(x = key, stratum = value, alluvium = id)) +
  geom_flow(aes(fill = id), na.rm = TRUE) + geom_stratum(na.rm = TRUE)
#> Warning: Computation failed in `stat_flow()`:
#> all(table(data$alluvium) == 2) is not TRUE

d2 <- data.frame(
  key = letters[c(1, 2, 2)],
  value = c(1, 1, 1),
  id = LETTERS[c(1, 2, 1)]
)
ggplot(d2, aes(x = key, stratum = value, alluvium = id)) +
  geom_alluvium(aes(fill = id)) + geom_stratum()
#> Warning in is_alluvial_lodes(data, key = "x", value = "stratum", id =
#> "alluvium", : Missing id-axis pairings.
#> Warning in is_alluvial_lodes(data, key = "x", value = "stratum", id =
#> "alluvium", : Missing id-axis pairings.

ggplot(d2, aes(x = key, stratum = value, alluvium = id)) +
  geom_flow(aes(fill = id)) + geom_stratum()
#> Warning in is_alluvial_lodes(data, key = "x", value = "stratum", id =
#> "alluvium", : Missing id-axis pairings.
#> Warning: Computation failed in `stat_flow()`:
#> all(table(data$alluvium) == 2) is not TRUE
#> Warning in is_alluvial_lodes(data, key = "x", value = "stratum", id =
#> "alluvium", : Missing id-axis pairings.

(They work in a consistent way when NAs are included in the data and na.rm is set to FALSE.) Instead, stat_flow() should remove the ids contributing to each flow that are NA at either end.