TidyTuesday 2020 Week31

Palmer Penguins

Preparations

  • ggaluvial(Brunson 2020a)
  • palmerpenguins(Horst, Hill, and Gorman 2020)
library(tidyverse)
library(palmerpenguins)
library(ggalluvial)
library(ggpubr)
library(magick)
library(patchwork)
library(munsell)
library(colorspace)
library(flair) #Highlight, Annotate, and Format your R Source Code

Tidy & Transform

  • pivot_longer1
    • Lodes format2
(
penguins_lodes <-
  penguins %>%
    count(species, island, year, sex, name = "freq") %>%
    rowid_to_column("alluvium") %>%
    mutate(year = factor(year)) %>%
    pivot_longer(
      -c(alluvium, freq, species),
      names_to = "variable", values_to = "stratum"
    )
)

## # A tibble: 105 x 5
##    alluvium species  freq variable stratum
##       <int> <fct>   <int> <chr>    <fct>  
##  1        1 Adelie      5 island   Biscoe 
##  2        1 Adelie      5 year     2007   
##  3        1 Adelie      5 sex      female 
##  4        2 Adelie      5 island   Biscoe 
##  5        2 Adelie      5 year     2007   
##  6        2 Adelie      5 sex      male   
##  7        3 Adelie      9 island   Biscoe 
##  8        3 Adelie      9 year     2008   
##  9        3 Adelie      9 sex      female 
## 10        4 Adelie      9 island   Biscoe 
## # … with 95 more rows

Visualise

alluvial_plot <- 
  penguins_lodes %>%
    ggplot(
      aes(
        x = variable, y = freq,
        stratum = stratum, alluvium = alluvium, label = stratum
      )
    ) +
      geom_alluvium(
        aes(fill = species),
        aes.bind = "alluvia", colour = "darkgray", reverse = FALSE
      ) +
      geom_stratum(aes(fill = stratum), reverse = FALSE) +
      geom_text(stat = "stratum", reverse = FALSE) +
      scale_x_discrete(expand = expansion(mult = .1)) +
      scale_fill_manual(
        values = c(
          Adelie = "darkorange", Chinstrap = "purple", Gentoo = "cyan4",
          `2007` = munsell::mnsl("5Y 9/2"), `2008` = munsell::mnsl("5Y 9/4"), `2009` = munsell::mnsl("5Y 9/6"),
          female = 'aliceblue', male = 'honeydew',
          Biscoe = '#FCDAE0', Dream = '#D4E8CE', Torgersen = '#D3E4F7'
        ),
        guide = FALSE,
        na.value = "red"
      ) +
      coord_flip()

penguins_png <- image_read("penguins.png")
img <- ggplot() + background_image(penguins_png)

(alluvial_plot / img) +
  plot_annotation(
    title = 'Palmer Penguins species by year, sex and island',
    caption = 'Artwork by @allison_horst | #TidyTuesday | @mstkolf',
    theme = theme(
      plot.title = element_text(face = "bold", size = 20, family = "serif"),
      plot.caption = element_text(size = 10)
    )
  )

References

Brunson, Jason Cory. 2020a. Ggalluvial: Alluvial Plots in ’Ggplot2’. https://CRAN.R-project.org/package=ggalluvial.

———. 2020b. “ggalluvial: Layered Grammar for Alluvial Plots.” Journal of Open Source Software 5 (49): 2017. https://doi.org/10.21105/joss.02017.

Horst, Allison, Alison Hill, and Kristen Gorman. 2020. Palmerpenguins: Palmer Archipelago (Antarctica) Penguin Data. https://CRAN.R-project.org/package=palmerpenguins.

Ihaka, Ross, Paul Murrell, Kurt Hornik, Jason C. Fisher, Reto Stauffer, Claus O. Wilke, Claire D. McWhite, and Achim Zeileis. 2019. Colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes. https://CRAN.R-project.org/package=colorspace.

Wickham, Charlotte. 2018. Munsell: Utilities for Using Munsell Colours. https://CRAN.R-project.org/package=munsell.

Reproducibility

## ─ Session info ───────────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 4.0.0 (2020-04-24)
##  os       macOS Catalina 10.15.5      
##  system   x86_64, darwin17.0          
##  ui       X11                         
##  language (EN)                        
##  collate  ja_JP.UTF-8                 
##  ctype    ja_JP.UTF-8                 
##  tz       Asia/Tokyo                  
##  date     2020-08-22                  
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  package        * version date       lib source        
##  abind            1.4-5   2016-07-21 [1] CRAN (R 4.0.2)
##  assertthat       0.2.1   2019-03-21 [1] CRAN (R 4.0.0)
##  backports        1.1.7   2020-05-13 [1] CRAN (R 4.0.0)
##  blob             1.2.1   2020-01-20 [1] CRAN (R 4.0.0)
##  blogdown         0.20    2020-06-23 [1] CRAN (R 4.0.2)
##  bookdown         0.20    2020-06-23 [1] CRAN (R 4.0.2)
##  broom            0.7.0   2020-07-09 [1] CRAN (R 4.0.2)
##  car              3.0-8   2020-05-21 [1] CRAN (R 4.0.2)
##  carData          3.0-4   2020-05-22 [1] CRAN (R 4.0.2)
##  cellranger       1.1.0   2016-07-27 [1] CRAN (R 4.0.0)
##  cli              2.0.2   2020-02-28 [1] CRAN (R 4.0.0)
##  colorspace     * 1.4-1   2019-03-18 [1] CRAN (R 4.0.0)
##  crayon           1.3.4   2017-09-16 [1] CRAN (R 4.0.0)
##  curl             4.3     2019-12-02 [1] CRAN (R 4.0.0)
##  data.table       1.13.0  2020-07-24 [1] CRAN (R 4.0.2)
##  DBI              1.1.0   2019-12-15 [1] CRAN (R 4.0.0)
##  dbplyr           1.4.4   2020-05-27 [1] CRAN (R 4.0.0)
##  digest           0.6.25  2020-02-23 [1] CRAN (R 4.0.0)
##  dplyr          * 1.0.0   2020-05-29 [1] CRAN (R 4.0.0)
##  ellipsis         0.3.1   2020-05-15 [1] CRAN (R 4.0.0)
##  evaluate         0.14    2019-05-28 [1] CRAN (R 4.0.0)
##  fansi            0.4.1   2020-01-08 [1] CRAN (R 4.0.0)
##  farver           2.0.3   2020-01-16 [1] CRAN (R 4.0.0)
##  flair          * 0.0.2   2020-04-23 [1] CRAN (R 4.0.2)
##  forcats        * 0.5.0   2020-03-01 [1] CRAN (R 4.0.0)
##  foreign          0.8-78  2020-04-13 [1] CRAN (R 4.0.0)
##  fs               1.4.1   2020-04-04 [1] CRAN (R 4.0.0)
##  generics         0.0.2   2018-11-29 [1] CRAN (R 4.0.0)
##  ggalluvial     * 0.12.0  2020-07-14 [1] CRAN (R 4.0.2)
##  ggplot2        * 3.3.2   2020-06-19 [1] CRAN (R 4.0.2)
##  ggpubr         * 0.4.0   2020-06-27 [1] CRAN (R 4.0.2)
##  ggsignif         0.6.0   2019-08-08 [1] CRAN (R 4.0.2)
##  glue             1.4.1   2020-05-13 [1] CRAN (R 4.0.0)
##  gtable           0.3.0   2019-03-25 [1] CRAN (R 4.0.0)
##  haven            2.3.0   2020-05-24 [1] CRAN (R 4.0.0)
##  here             0.1     2017-05-28 [1] CRAN (R 4.0.2)
##  hms              0.5.3   2020-01-08 [1] CRAN (R 4.0.0)
##  htmltools        0.5.0   2020-06-16 [1] CRAN (R 4.0.2)
##  httr             1.4.1   2019-08-05 [1] CRAN (R 4.0.0)
##  jsonlite         1.6.1   2020-02-02 [1] CRAN (R 4.0.0)
##  knitr            1.28    2020-02-06 [1] CRAN (R 4.0.0)
##  labeling         0.3     2014-08-23 [1] CRAN (R 4.0.0)
##  lifecycle        0.2.0   2020-03-06 [1] CRAN (R 4.0.0)
##  lubridate        1.7.8   2020-04-06 [1] CRAN (R 4.0.0)
##  magick         * 2.4.0   2020-06-23 [1] CRAN (R 4.0.2)
##  magrittr         1.5     2014-11-22 [1] CRAN (R 4.0.0)
##  modelr           0.1.8   2020-05-19 [1] CRAN (R 4.0.0)
##  munsell        * 0.5.0   2018-06-12 [1] CRAN (R 4.0.0)
##  openxlsx         4.1.5   2020-05-06 [1] CRAN (R 4.0.2)
##  palmerpenguins * 0.1.0   2020-07-23 [1] CRAN (R 4.0.2)
##  patchwork      * 1.0.1   2020-06-22 [1] CRAN (R 4.0.2)
##  pillar           1.4.4   2020-05-05 [1] CRAN (R 4.0.0)
##  pkgconfig        2.0.3   2019-09-22 [1] CRAN (R 4.0.0)
##  purrr          * 0.3.4   2020-04-17 [1] CRAN (R 4.0.0)
##  R6               2.4.1   2019-11-12 [1] CRAN (R 4.0.0)
##  Rcpp             1.0.4.6 2020-04-09 [1] CRAN (R 4.0.0)
##  readr          * 1.3.1   2018-12-21 [1] CRAN (R 4.0.0)
##  readxl           1.3.1   2019-03-13 [1] CRAN (R 4.0.0)
##  reprex           0.3.0   2019-05-16 [1] CRAN (R 4.0.0)
##  rio              0.5.16  2018-11-26 [1] CRAN (R 4.0.2)
##  rlang            0.4.7   2020-07-09 [1] CRAN (R 4.0.2)
##  rmarkdown        2.3     2020-06-18 [1] CRAN (R 4.0.2)
##  rprojroot        1.3-2   2018-01-03 [1] CRAN (R 4.0.0)
##  rstatix          0.6.0   2020-06-18 [1] CRAN (R 4.0.2)
##  rstudioapi       0.11    2020-02-07 [1] CRAN (R 4.0.0)
##  rvest            0.3.5   2019-11-08 [1] CRAN (R 4.0.0)
##  scales           1.1.1   2020-05-11 [1] CRAN (R 4.0.0)
##  sessioninfo    * 1.1.1   2018-11-05 [1] CRAN (R 4.0.2)
##  stringi          1.4.6   2020-02-17 [1] CRAN (R 4.0.0)
##  stringr        * 1.4.0   2019-02-10 [1] CRAN (R 4.0.0)
##  tibble         * 3.0.3   2020-07-10 [1] CRAN (R 4.0.2)
##  tidyr          * 1.1.0   2020-05-20 [1] CRAN (R 4.0.0)
##  tidyselect       1.1.0   2020-05-11 [1] CRAN (R 4.0.0)
##  tidyverse      * 1.3.0   2019-11-21 [1] CRAN (R 4.0.0)
##  utf8             1.1.4   2018-05-24 [1] CRAN (R 4.0.0)
##  vctrs            0.3.1   2020-06-05 [1] CRAN (R 4.0.0)
##  withr            2.2.0   2020-04-20 [1] CRAN (R 4.0.0)
##  xfun             0.14    2020-05-20 [1] CRAN (R 4.0.0)
##  xml2             1.3.2   2020-04-23 [1] CRAN (R 4.0.0)
##  yaml             2.2.1   2020-02-01 [1] CRAN (R 4.0.0)
##  zip              2.0.4   2019-09-01 [1] CRAN (R 4.0.2)
## 
## [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
comments powered by Disqus

関連項目