corybrunson / ggalluvial

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

Custom order of NA strata #98

Closed GiuliaPais closed 2 years ago

GiuliaPais commented 2 years ago

Description of the issue

I would like to arrange the strata according to their "abundance" but I need to shift the strata associated with NA values either on top or on the bottom of the bar (independently from the size). Can this be achieved? I tried different approaches (see reprex below) but no luck so far. Thanks!

Reproducible example (preferably using reprex::reprex())

library(ggplot2)
library(ggalluvial)

toy <- tibble::tribble(
  ~ TimePoint, ~ alluvia_id, ~ abundance,
  "0030", NA_character_, 26.4,
  "0030", "1_40689188_+", 30.6,
  "0030", "1_8464757_-", 25.6,
  "0030", "1_16186297_-", 17.4
)

# What I want:
# Either NA, 1_40689188_+, 1_8464757_-, 1_16186297_-
# OR 1_40689188_+, 1_8464757_-, 1_16186297_-, NA

# This orders:
#  NA, 1_16186297_-, 1_40689188_+, 1_8464757_-
ggplot(toy, aes(x = TimePoint, y = abundance, alluvium = alluvia_id,
                stratum = alluvia_id)) +
  stat_stratum(na.rm = FALSE, fill = "grey90") +
  stat_stratum(geom = "text", aes(label = alluvia_id))
#> Warning: Removed 1 rows containing missing values (geom_text).

# This orders:
#  1_8464757_-, 1_40689188_+, 1_16186297_-, NA
ggplot(toy, aes(x = TimePoint, y = abundance, alluvium = alluvia_id,
                stratum = alluvia_id)) +
  stat_stratum(na.rm = FALSE, fill = "grey90", reverse = FALSE) +
  stat_stratum(geom = "text", aes(label = alluvia_id), reverse = FALSE)
#> Warning: Removed 1 rows containing missing values (geom_text).

# This orders:
#  1_40689188_+, NA, 1_8464757_-,  1_16186297_-
ggplot(toy, aes(x = TimePoint, y = abundance, alluvium = alluvia_id,
                stratum = alluvia_id)) +
  stat_stratum(na.rm = FALSE, fill = "grey90", decreasing = FALSE) +
  stat_stratum(geom = "text", aes(label = alluvia_id), decreasing = FALSE)
#> Warning: Removed 1 rows containing missing values (geom_text).


# This orders:
#  1_16186297_-, 1_8464757_-, NA, 1_40689188_+,  
ggplot(toy, aes(x = TimePoint, y = abundance, alluvium = alluvia_id,
                stratum = alluvia_id)) +
  stat_stratum(na.rm = FALSE, fill = "grey90", decreasing = TRUE) +
  stat_stratum(geom = "text", aes(label = alluvia_id), decreasing = TRUE)
#> Warning: Removed 1 rows containing missing values (geom_text).

Created on 2022-04-11 by the reprex package (v2.0.1)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.1.2 (2021-11-01) #> os macOS Big Sur 10.16 #> system x86_64, darwin17.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Rome #> date 2022-04-11 #> pandoc 2.14.0.3 @ /Applications/RStudio.app/Contents/MacOS/pandoc/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.1.0) #> cli 3.2.0 2022-02-14 [1] CRAN (R 4.1.2) #> colorspace 2.0-3 2022-02-21 [1] CRAN (R 4.1.2) #> crayon 1.5.1 2022-03-26 [1] CRAN (R 4.1.2) #> curl 4.3.2 2021-06-23 [1] CRAN (R 4.1.0) #> DBI 1.1.2 2021-12-20 [1] CRAN (R 4.1.0) #> digest 0.6.29 2021-12-01 [1] CRAN (R 4.1.0) #> dplyr 1.0.8 2022-02-08 [1] CRAN (R 4.1.2) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.0) #> evaluate 0.15 2022-02-18 [1] CRAN (R 4.1.2) #> fansi 1.0.3 2022-03-24 [1] CRAN (R 4.1.2) #> farver 2.1.0 2021-02-28 [1] CRAN (R 4.1.0) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.0) #> fs 1.5.2 2021-12-08 [1] CRAN (R 4.1.0) #> generics 0.1.2 2022-01-31 [1] CRAN (R 4.1.2) #> ggalluvial * 0.12.3 2020-12-05 [1] CRAN (R 4.1.0) #> ggplot2 * 3.3.5 2021-06-25 [1] CRAN (R 4.1.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.1.2) #> gtable 0.3.0 2019-03-25 [1] CRAN (R 4.1.0) #> highr 0.9 2021-04-16 [1] CRAN (R 4.1.0) #> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.0) #> httr 1.4.2 2020-07-20 [1] CRAN (R 4.1.0) #> knitr 1.38 2022-03-25 [1] CRAN (R 4.1.2) #> labeling 0.4.2 2020-10-20 [1] CRAN (R 4.1.0) #> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.1.2) #> mime 0.12 2021-09-28 [1] CRAN (R 4.1.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.1.0) #> pillar 1.7.0 2022-02-01 [1] CRAN (R 4.1.2) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.0) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.1.0) #> R.cache 0.15.0 2021-04-30 [1] CRAN (R 4.1.0) #> R.methodsS3 1.8.1 2020-08-26 [1] CRAN (R 4.1.0) #> R.oo 1.24.0 2020-08-26 [1] CRAN (R 4.1.0) #> R.utils 2.11.0 2021-09-26 [1] CRAN (R 4.1.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.0) #> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.1.0) #> rlang 1.0.2 2022-03-04 [1] CRAN (R 4.1.2) #> rmarkdown 2.13 2022-03-10 [1] CRAN (R 4.1.2) #> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.0) #> scales 1.1.1 2020-05-11 [1] CRAN (R 4.1.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.1.0) #> stringi 1.7.6 2021-11-29 [1] CRAN (R 4.1.0) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.1.0) #> styler 1.7.0 2022-03-13 [1] CRAN (R 4.1.2) #> tibble 3.1.6 2021-11-07 [1] CRAN (R 4.1.0) #> tidyselect 1.1.2 2022-02-21 [1] CRAN (R 4.1.2) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.0) #> vctrs 0.4.0 2022-03-30 [1] CRAN (R 4.1.2) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.1.2) #> xfun 0.30 2022-03-02 [1] CRAN (R 4.1.2) #> xml2 1.3.3 2021-11-30 [1] CRAN (R 4.1.0) #> yaml 2.3.5 2022-02-21 [1] CRAN (R 4.1.2) #> #> [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
corybrunson commented 2 years ago

Hi @GiuliaPais and thank you for raising the issue.

First, i think you can achieve this with the existing version of ggalluvial by converting alluvia_id in your tibble from a character to a factor:

toy$alluvia_id <- forcats::fct_reorder(toy$alluvia_id, toy$abundance)

By default, all ggalluvial layers will then arrange the strata according to these factor levels.

I've tried to keep options like reverse and decreasing to a minimum in ggalluvial, and i'm leaning against implementing this additional option. Though it might be worth changing one of the examples to illustrate the use of factors to achieve it. Do you have strong thoughts either way?

GiuliaPais commented 2 years ago

Thanks for the reply! Yes, it seems to be a good solution, below a reprex that shows the result, thanks for the help!

library(ggplot2)
library(ggalluvial)
library(magrittr)

toy <- tibble::tribble(
  ~ TimePoint, ~ alluvia_id, ~ abundance,
  "0030", NA_character_, 26.4,
  "0030", "1_40689188_+", 30.6,
  "0030", "1_8464757_-", 25.6,
  "0030", "1_16186297_-", 17.4
) %>%
  dplyr::mutate(alluvia_id = as.factor(alluvia_id)) %>%
  dplyr::mutate(alluvia_id = forcats::fct_reorder(alluvia_id, abundance, min))

ggplot(toy, aes(x = TimePoint, y = abundance, alluvium = alluvia_id,
                stratum = alluvia_id)) +
  stat_stratum(na.rm = FALSE, fill = "grey90", reverse = FALSE) +
  stat_stratum(geom = "text", aes(label = alluvia_id), reverse = FALSE)
#> Warning: Removed 1 rows containing missing values (geom_text).

Created on 2022-04-11 by the reprex package (v2.0.1)

corybrunson commented 2 years ago

Great, thank you for confirming!