class: title-slide, center, bottom <img src="data:image/png;base64,#images/lter_penguins.png" width="400" /> # An Antarctic Tour of the Tidyverse ## R-Ladies Chicago ### Silvia Canelón, PhD • August 31, 2020 ###
GitHub
repo: [spcanelon/tour-of-the-tidyverse](https://github.com/spcanelon/tour-of-the-tidyverse) --- class: about-me, middle, center ## Silvia Canelón ### Postdoctoral Research Scientist ### University of Pennsylvania, Philadelphia, PA, USA <img style="border-radius: 50%;" src="data:image/png;base64,#https://www.silviacanelon.com/about/sidebar/avatar.png" width="150px"/> [
Link
silviacanelon.com](https://www.silviacanelon.com)<br/> [
Twitter
@spcanelon](https://twitter.com/spcanelon)<br/> [
GitHub
@spcanelon](https://github.com/spcanelon) .footnote[<span>Photo by <a href="https://unsplash.com/@lukehuff?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">lucas huffman</a> on <a href="https://unsplash.com/s/photos/antarctica?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></span>] --- class: left, middle # Acknowledgments ### [palmerpenguins](https://allisonhorst.github.io/palmerpenguins/articles/intro.html) 📦 developed by Drs. [Allison Horst](https://www.allisonhorst.com/), [Alison Hill](https://alison.rbind.io/), and [Kristen Gorman](https://www.uaf.edu/cfos/people/faculty/detail/kristen-gorman.php). ### Penguin artwork by Allison Horst ([@allison_horst](https://twitter.com/allison_horst)) ### Slide inspiration from Alison Hill ([@apreshill](https://twitter.com/apreshill))'s recent education training materials "[Teaching in Production](https://rstudio-education.github.io/teaching-in-production/)" ### Slides made using Dr. Yihui Xie's [xaringan](https://github.com/yihui/xaringan) 📦 and Garrick Aden-Buie's [xaringanExtra](https://github.com/gadenbuie/xaringanExtra) 📦, and adapted from the [R-Ladies `xaringan` theme designed by Alison Hill](https://alison.rbind.io/post/2017-12-18-r-ladies-presentation-ninja/) ### Photographs from various photographers on Unsplash, and noted on the relevant slide --- class: left, top background-image: url(data:image/png;base64,#images/logo.png) background-position: 1050px 50px background-size: 80px # Meet our penguin friends! <div class="flex" style="margin: 0 0em;"> <div class="column"> <h3> Chinstrap </h3> <img src="https://github.com/spcanelon/slides/blob/main/tour-of-the-tidyverse/images/penguin_chinstrap.jpg?raw=true" style="width: 100%;"> </div> <div class="column" style="margin: 0 1em;"> <h3> Gentoo </h3> <img src="https://github.com/spcanelon/slides/blob/main/tour-of-the-tidyverse/images/penguin_gentoo.jpg?raw=true" style=""> </div> <div class="column" style="margin: 0 0em;"> <h3> Adélie </h3> <img src="https://github.com/spcanelon/slides/blob/main/tour-of-the-tidyverse/images/penguin_adelie.jpg?raw=true" style=""> </div> </div> .footnote[🐧<span>Photos by <a href="https://unsplash.com/@longmaspirit?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Long Ma</a> on <a href="https://unsplash.com/collections/12240655/palmerpenguins/d5aed8c855e26061e5e651d3f180b76d?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></span> ] --- class: right, top background-image: url(data:image/png;base64,#images/pptx/tidyverse.png) background-size: 1150px ## Collection of R packages, including <br/> these 8 core packages (and more!) --- class: penguin-tour <img src="data:image/png;base64,#images/pptx/01-readr.png" width="1200" /> .footnote[<span>Photo by <a href="https://unsplash.com/@eadesstudio?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">James Eades</a> on <a href="https://unsplash.com/collections/12240655/palmerpenguins/d5aed8c855e26061e5e651d3f180b76d?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></span> ] --- background-image: url(data:image/png;base64,#images/hex/readr.png) background-position: 1050px 50px background-size: 80px # readr: info .panelset[ .panel[.panel-name[Overview] .pull-left[ ### Importing data is the very first step! <br/> You can use `readr` to import rectangular data. ] .pull-right[ ### You can import... - comma separated (CSV) files with `read_csv()` - tab separated files with `read_tsv()` - general delimited files with `read_delim()` - fixed width files with `read_fwf()` - tabular files where columns are separated by white-space with `read_table()` - web log files with `read_log()` ] ] .panel[.panel-name[Cheatsheet]
PDF: https://github.com/rstudio/cheatsheets/raw/main/data-import.pdf ![](data:image/png;base64,#https://raw.githubusercontent.com/rstudio/cheatsheets/master/pngs/thumbnails/data-import-cheatsheet-thumbs.png) ] .panel[.panel-name[Reading] .left-column[ <img src="data:image/png;base64,#images/r4ds-cover.png" width="222" /> ] .right-column[ ### R for Data Science: [Ch 11 Data import](https://r4ds.had.co.nz/data-import.html) ### Package documentation: https://readr.tidyverse.org/ ] ] ] --- background-image: url(data:image/png;base64,#images/hex/readr.png) background-position: 1050px 50px background-size: 80px # readr: exercise .panelset[ .panel[.panel-name[Read data in] .center[ ### Both options below will get you the same dataset!] Option 1 ```r # option 1: load using URL ---- raw_adelie_url <- read_csv("https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-pal.219.3&entityid=002f3893385f710df69eeebe893144ff") ``` Option 2 ```r # option 2: load using filepath ---- raw_adelie_filepath <- read_csv("tutorial/raw_adelie.csv") ``` ] .panel[.panel-name[Save data] Lucky for us, the `palmerpenguins` 📦 compiles data from all three species together for us! .pull-left[ `penguins` contains a clean dataset ```r # saves package tibble into global environment penguins <- palmerpenguins::penguins head(penguins) ## # A tibble: 6 × 8 ## species island bill_length_mm ## <fct> <fct> <dbl> ## 1 Adelie Torgersen 39.1 ## 2 Adelie Torgersen 39.5 ## 3 Adelie Torgersen 40.3 ## 4 Adelie Torgersen NA ## 5 Adelie Torgersen 36.7 ## 6 Adelie Torgersen 39.3 ## # … with 5 more variables: ## # bill_depth_mm <dbl>, ## # flipper_length_mm <int>, ## # body_mass_g <int>, sex <fct>, ## # year <int> ``` ] .pull-right[ `penguins_raw` contains raw data ```r penguins_raw <- palmerpenguins::penguins_raw head(penguins_raw) ## # A tibble: 6 × 17 ## studyName `Sample Number` Species Region ## <chr> <dbl> <chr> <chr> ## 1 PAL0708 1 Adelie … Anvers ## 2 PAL0708 2 Adelie … Anvers ## 3 PAL0708 3 Adelie … Anvers ## 4 PAL0708 4 Adelie … Anvers ## 5 PAL0708 5 Adelie … Anvers ## 6 PAL0708 6 Adelie … Anvers ## # … with 13 more variables: Island <chr>, ## # Stage <chr>, Individual ID <chr>, ## # Clutch Completion <chr>, ## # Date Egg <date>, ## # Culmen Length (mm) <dbl>, ## # Culmen Depth (mm) <dbl>, ## # Flipper Length (mm) <dbl>, … ``` ] ] ] --- class: penguin-tour <img src="data:image/png;base64,#images/pptx/02-tibble.png" width="1200" /> .footnote[ <span>Photo by <a href="https://unsplash.com/@eadesstudio?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">James Eades</a> on <a href="https://unsplash.com/collections/12240655/palmerpenguins/d5aed8c855e26061e5e651d3f180b76d?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></span> ] --- background-image: url(data:image/png;base64,#images/hex/tibble.png) background-position: 1050px 50px background-size: 80px # tibble: info .panelset[ .panel[.panel-name[Overview] .pull-left[ ### A `tibble` is much like the `dataframe` in base R, but optimized for use in the Tidyverse. ] ] .panel[.panel-name[Cheatsheet]
PDF (tidyr): https://github.com/rstudio/cheatsheets/raw/main/data-transformation.pdf ![](data:image/png;base64,#https://raw.githubusercontent.com/rstudio/cheatsheets/master/pngs/thumbnails/data-import-cheatsheet-thumbs.png) ] .panel[.panel-name[Reading] .left-column[ <img src="data:image/png;base64,#images/r4ds-cover.png" width="222" /> ] .right-column[ ### R for Data Science: [Ch 10 Tibbles](https://r4ds.had.co.nz/tibbles.html) ### Package documentation: https://tibble.tidyverse.org/ ] ] ] --- background-image: url(data:image/png;base64,#images/hex/tibble.png) background-position: 1050px 50px background-size: 80px # tibble: exercise .panelset[ .panel[.panel-name[Code] Let's take a look at the differences! ```r # try each of these commands in the console and see if you can spot the differences! as_tibble(penguins) as.data.frame(penguins) ``` ] .panel[.panel-name[Result] .pull-left[ ```r as_tibble(penguins) ## # A tibble: 344 × 8 ## species island bill_length_mm ## <fct> <fct> <dbl> ## 1 Adelie Torgersen 39.1 ## 2 Adelie Torgersen 39.5 ## 3 Adelie Torgersen 40.3 ## 4 Adelie Torgersen NA ## 5 Adelie Torgersen 36.7 ## 6 Adelie Torgersen 39.3 ## 7 Adelie Torgersen 38.9 ## 8 Adelie Torgersen 39.2 ## 9 Adelie Torgersen 34.1 ## 10 Adelie Torgersen 42 ## # … with 334 more rows, and 5 more ## # variables: bill_depth_mm <dbl>, ## # flipper_length_mm <int>, ## # body_mass_g <int>, sex <fct>, ## # year <int> ``` ] .pull-right[ ```r as.data.frame(penguins) ## species island bill_length_mm ## 1 Adelie Torgersen 39.1 ## 2 Adelie Torgersen 39.5 ## 3 Adelie Torgersen 40.3 ## 4 Adelie Torgersen NA ## 5 Adelie Torgersen 36.7 ## 6 Adelie Torgersen 39.3 ## 7 Adelie Torgersen 38.9 ## 8 Adelie Torgersen 39.2 ## 9 Adelie Torgersen 34.1 ## 10 Adelie Torgersen 42.0 ## 11 Adelie Torgersen 37.8 ## 12 Adelie Torgersen 37.8 ## 13 Adelie Torgersen 41.1 ## 14 Adelie Torgersen 38.6 ## 15 Adelie Torgersen 34.6 ## 16 Adelie Torgersen 36.6 ## 17 Adelie Torgersen 38.7 ## 18 Adelie Torgersen 42.5 ## 19 Adelie Torgersen 34.4 ## 20 Adelie Torgersen 46.0 ## 21 Adelie Biscoe 37.8 ## 22 Adelie Biscoe 37.7 ## 23 Adelie Biscoe 35.9 ## 24 Adelie Biscoe 38.2 ## 25 Adelie Biscoe 38.8 ## 26 Adelie Biscoe 35.3 ## 27 Adelie Biscoe 40.6 ## 28 Adelie Biscoe 40.5 ## 29 Adelie Biscoe 37.9 ## 30 Adelie Biscoe 40.5 ## 31 Adelie Dream 39.5 ## 32 Adelie Dream 37.2 ## 33 Adelie Dream 39.5 ## 34 Adelie Dream 40.9 ## 35 Adelie Dream 36.4 ## 36 Adelie Dream 39.2 ## 37 Adelie Dream 38.8 ## 38 Adelie Dream 42.2 ## 39 Adelie Dream 37.6 ## 40 Adelie Dream 39.8 ## 41 Adelie Dream 36.5 ## 42 Adelie Dream 40.8 ## 43 Adelie Dream 36.0 ## 44 Adelie Dream 44.1 ## 45 Adelie Dream 37.0 ## 46 Adelie Dream 39.6 ## 47 Adelie Dream 41.1 ## 48 Adelie Dream 37.5 ## 49 Adelie Dream 36.0 ## 50 Adelie Dream 42.3 ## 51 Adelie Biscoe 39.6 ## 52 Adelie Biscoe 40.1 ## 53 Adelie Biscoe 35.0 ## 54 Adelie Biscoe 42.0 ## 55 Adelie Biscoe 34.5 ## 56 Adelie Biscoe 41.4 ## 57 Adelie Biscoe 39.0 ## 58 Adelie Biscoe 40.6 ## 59 Adelie Biscoe 36.5 ## 60 Adelie Biscoe 37.6 ## 61 Adelie Biscoe 35.7 ## 62 Adelie Biscoe 41.3 ## 63 Adelie Biscoe 37.6 ## 64 Adelie Biscoe 41.1 ## 65 Adelie Biscoe 36.4 ## 66 Adelie Biscoe 41.6 ## 67 Adelie Biscoe 35.5 ## 68 Adelie Biscoe 41.1 ## 69 Adelie Torgersen 35.9 ## 70 Adelie Torgersen 41.8 ## 71 Adelie Torgersen 33.5 ## 72 Adelie Torgersen 39.7 ## 73 Adelie Torgersen 39.6 ## 74 Adelie Torgersen 45.8 ## 75 Adelie Torgersen 35.5 ## 76 Adelie Torgersen 42.8 ## 77 Adelie Torgersen 40.9 ## 78 Adelie Torgersen 37.2 ## 79 Adelie Torgersen 36.2 ## 80 Adelie Torgersen 42.1 ## 81 Adelie Torgersen 34.6 ## 82 Adelie Torgersen 42.9 ## 83 Adelie Torgersen 36.7 ## 84 Adelie Torgersen 35.1 ## 85 Adelie Dream 37.3 ## 86 Adelie Dream 41.3 ## 87 Adelie Dream 36.3 ## 88 Adelie Dream 36.9 ## 89 Adelie Dream 38.3 ## 90 Adelie Dream 38.9 ## 91 Adelie Dream 35.7 ## 92 Adelie Dream 41.1 ## 93 Adelie Dream 34.0 ## 94 Adelie Dream 39.6 ## 95 Adelie Dream 36.2 ## 96 Adelie Dream 40.8 ## 97 Adelie Dream 38.1 ## 98 Adelie Dream 40.3 ## 99 Adelie Dream 33.1 ## 100 Adelie Dream 43.2 ## 101 Adelie Biscoe 35.0 ## 102 Adelie Biscoe 41.0 ## 103 Adelie Biscoe 37.7 ## 104 Adelie Biscoe 37.8 ## 105 Adelie Biscoe 37.9 ## 106 Adelie Biscoe 39.7 ## 107 Adelie Biscoe 38.6 ## 108 Adelie Biscoe 38.2 ## 109 Adelie Biscoe 38.1 ## 110 Adelie Biscoe 43.2 ## 111 Adelie Biscoe 38.1 ## 112 Adelie Biscoe 45.6 ## 113 Adelie Biscoe 39.7 ## 114 Adelie Biscoe 42.2 ## 115 Adelie Biscoe 39.6 ## 116 Adelie Biscoe 42.7 ## 117 Adelie Torgersen 38.6 ## 118 Adelie Torgersen 37.3 ## 119 Adelie Torgersen 35.7 ## 120 Adelie Torgersen 41.1 ## 121 Adelie Torgersen 36.2 ## 122 Adelie Torgersen 37.7 ## 123 Adelie Torgersen 40.2 ## 124 Adelie Torgersen 41.4 ## 125 Adelie Torgersen 35.2 ## bill_depth_mm flipper_length_mm ## 1 18.7 181 ## 2 17.4 186 ## 3 18.0 195 ## 4 NA NA ## 5 19.3 193 ## 6 20.6 190 ## 7 17.8 181 ## 8 19.6 195 ## 9 18.1 193 ## 10 20.2 190 ## 11 17.1 186 ## 12 17.3 180 ## 13 17.6 182 ## 14 21.2 191 ## 15 21.1 198 ## 16 17.8 185 ## 17 19.0 195 ## 18 20.7 197 ## 19 18.4 184 ## 20 21.5 194 ## 21 18.3 174 ## 22 18.7 180 ## 23 19.2 189 ## 24 18.1 185 ## 25 17.2 180 ## 26 18.9 187 ## 27 18.6 183 ## 28 17.9 187 ## 29 18.6 172 ## 30 18.9 180 ## 31 16.7 178 ## 32 18.1 178 ## 33 17.8 188 ## 34 18.9 184 ## 35 17.0 195 ## 36 21.1 196 ## 37 20.0 190 ## 38 18.5 180 ## 39 19.3 181 ## 40 19.1 184 ## 41 18.0 182 ## 42 18.4 195 ## 43 18.5 186 ## 44 19.7 196 ## 45 16.9 185 ## 46 18.8 190 ## 47 19.0 182 ## 48 18.9 179 ## 49 17.9 190 ## 50 21.2 191 ## 51 17.7 186 ## 52 18.9 188 ## 53 17.9 190 ## 54 19.5 200 ## 55 18.1 187 ## 56 18.6 191 ## 57 17.5 186 ## 58 18.8 193 ## 59 16.6 181 ## 60 19.1 194 ## 61 16.9 185 ## 62 21.1 195 ## 63 17.0 185 ## 64 18.2 192 ## 65 17.1 184 ## 66 18.0 192 ## 67 16.2 195 ## 68 19.1 188 ## 69 16.6 190 ## 70 19.4 198 ## 71 19.0 190 ## 72 18.4 190 ## 73 17.2 196 ## 74 18.9 197 ## 75 17.5 190 ## 76 18.5 195 ## 77 16.8 191 ## 78 19.4 184 ## 79 16.1 187 ## 80 19.1 195 ## 81 17.2 189 ## 82 17.6 196 ## 83 18.8 187 ## 84 19.4 193 ## 85 17.8 191 ## 86 20.3 194 ## 87 19.5 190 ## 88 18.6 189 ## 89 19.2 189 ## 90 18.8 190 ## 91 18.0 202 ## 92 18.1 205 ## 93 17.1 185 ## 94 18.1 186 ## 95 17.3 187 ## 96 18.9 208 ## 97 18.6 190 ## 98 18.5 196 ## 99 16.1 178 ## 100 18.5 192 ## 101 17.9 192 ## 102 20.0 203 ## 103 16.0 183 ## 104 20.0 190 ## 105 18.6 193 ## 106 18.9 184 ## 107 17.2 199 ## 108 20.0 190 ## 109 17.0 181 ## 110 19.0 197 ## 111 16.5 198 ## 112 20.3 191 ## 113 17.7 193 ## 114 19.5 197 ## 115 20.7 191 ## 116 18.3 196 ## 117 17.0 188 ## 118 20.5 199 ## 119 17.0 189 ## 120 18.6 189 ## 121 17.2 187 ## 122 19.8 198 ## 123 17.0 176 ## 124 18.5 202 ## 125 15.9 186 ## body_mass_g sex year ## 1 3750 male 2007 ## 2 3800 female 2007 ## 3 3250 female 2007 ## 4 NA <NA> 2007 ## 5 3450 female 2007 ## 6 3650 male 2007 ## 7 3625 female 2007 ## 8 4675 male 2007 ## 9 3475 <NA> 2007 ## 10 4250 <NA> 2007 ## 11 3300 <NA> 2007 ## 12 3700 <NA> 2007 ## 13 3200 female 2007 ## 14 3800 male 2007 ## 15 4400 male 2007 ## 16 3700 female 2007 ## 17 3450 female 2007 ## 18 4500 male 2007 ## 19 3325 female 2007 ## 20 4200 male 2007 ## 21 3400 female 2007 ## 22 3600 male 2007 ## 23 3800 female 2007 ## 24 3950 male 2007 ## 25 3800 male 2007 ## 26 3800 female 2007 ## 27 3550 male 2007 ## 28 3200 female 2007 ## 29 3150 female 2007 ## 30 3950 male 2007 ## 31 3250 female 2007 ## 32 3900 male 2007 ## 33 3300 female 2007 ## 34 3900 male 2007 ## 35 3325 female 2007 ## 36 4150 male 2007 ## 37 3950 male 2007 ## 38 3550 female 2007 ## 39 3300 female 2007 ## 40 4650 male 2007 ## 41 3150 female 2007 ## 42 3900 male 2007 ## 43 3100 female 2007 ## 44 4400 male 2007 ## 45 3000 female 2007 ## 46 4600 male 2007 ## 47 3425 male 2007 ## 48 2975 <NA> 2007 ## 49 3450 female 2007 ## 50 4150 male 2007 ## 51 3500 female 2008 ## 52 4300 male 2008 ## 53 3450 female 2008 ## 54 4050 male 2008 ## 55 2900 female 2008 ## 56 3700 male 2008 ## 57 3550 female 2008 ## 58 3800 male 2008 ## 59 2850 female 2008 ## 60 3750 male 2008 ## 61 3150 female 2008 ## 62 4400 male 2008 ## 63 3600 female 2008 ## 64 4050 male 2008 ## 65 2850 female 2008 ## 66 3950 male 2008 ## 67 3350 female 2008 ## 68 4100 male 2008 ## 69 3050 female 2008 ## 70 4450 male 2008 ## 71 3600 female 2008 ## 72 3900 male 2008 ## 73 3550 female 2008 ## 74 4150 male 2008 ## 75 3700 female 2008 ## 76 4250 male 2008 ## 77 3700 female 2008 ## 78 3900 male 2008 ## 79 3550 female 2008 ## 80 4000 male 2008 ## 81 3200 female 2008 ## 82 4700 male 2008 ## 83 3800 female 2008 ## 84 4200 male 2008 ## 85 3350 female 2008 ## 86 3550 male 2008 ## 87 3800 male 2008 ## 88 3500 female 2008 ## 89 3950 male 2008 ## 90 3600 female 2008 ## 91 3550 female 2008 ## 92 4300 male 2008 ## 93 3400 female 2008 ## 94 4450 male 2008 ## 95 3300 female 2008 ## 96 4300 male 2008 ## 97 3700 female 2008 ## 98 4350 male 2008 ## 99 2900 female 2008 ## 100 4100 male 2008 ## 101 3725 female 2009 ## 102 4725 male 2009 ## 103 3075 female 2009 ## 104 4250 male 2009 ## 105 2925 female 2009 ## 106 3550 male 2009 ## 107 3750 female 2009 ## 108 3900 male 2009 ## 109 3175 female 2009 ## 110 4775 male 2009 ## 111 3825 female 2009 ## 112 4600 male 2009 ## 113 3200 female 2009 ## 114 4275 male 2009 ## 115 3900 female 2009 ## 116 4075 male 2009 ## 117 2900 female 2009 ## 118 3775 male 2009 ## 119 3350 female 2009 ## 120 3325 male 2009 ## 121 3150 female 2009 ## 122 3500 male 2009 ## 123 3450 female 2009 ## 124 3875 male 2009 ## 125 3050 female 2009 ## [ reached 'max' / getOption("max.print") -- omitted 219 rows ] ``` ] ] .panel[.panel-name[Chat] ### What differences do you see? You might see a `tibble` prints: - variable classes - only 10 rows - only as many columns as can fit on the screen - `NA`s are highlighted in console so they're easy to spot (font highlighting and styling in `tibble`) Not so much a concern in an R Markdown file, but noticeable in the console. Print method makes it easier to work with large datasets. ] .panel[.panel-name[More] There are a couple of other main differences, namely in **subsetting** and **recycling**. Check them out in the [`vignette("tibble")`](https://tibble.tidyverse.org/articles/tibble.html) Try it out here! ```r vignette("tibble") ``` ] ] --- class: penguin-tour <img src="data:image/png;base64,#images/pptx/03-ggplot2.png" width="1200" /> .footnote[<span>Photo by <a href="https://unsplash.com/@eadesstudio?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">James Eades</a> on <a href="https://unsplash.com/collections/12240655/palmerpenguins/d5aed8c855e26061e5e651d3f180b76d?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></span> ] --- background-image: url(data:image/png;base64,#images/hex/ggplot2.png) background-position: 1050px 50px background-size: 80px # ggplot2: info .panelset[ .panel[.panel-name[Overview] ### Let's start by making a simple plot of our data! ### `ggplot2` uses the "Grammar of Graphics" and layers graphical components together to create a plot. ] .panel[.panel-name[Cheatsheet]
PDF: https://github.com/rstudio/cheatsheets/raw/main/data-visualization-2.1.pdf ![](data:image/png;base64,#https://raw.githubusercontent.com/rstudio/cheatsheets/master/pngs/thumbnails/data-visualization-cheatsheet-thumbs.png) ] .panel[.panel-name[Reading] .left-column[ <img src="data:image/png;base64,#images/r4ds-cover.png" width="222" /> ] .right-column[ ### R for Data Science: [Ch 3 Data visualization](https://r4ds.had.co.nz/data-visualisation.html) ### Package documentation: https://ggplot2.tidyverse.org/ ] ] ] --- background-image: url(data:image/png;base64,#images/hex/ggplot2.png) background-position: 1050px 50px background-size: 80px # ggplot2: exercise .panelset[ .panel[.panel-name[View the data] .pull-left[ ### Get a full view of the dataset: ```r View(penguins) ``` ] .pull-right[ ### Or catch a `glimpse`: ```r glimpse(penguins) ## Rows: 344 ## Columns: 8 ## $ species <fct> Adelie, Adelie,… ## $ island <fct> Torgersen, Torg… ## $ bill_length_mm <dbl> 39.1, 39.5, 40.… ## $ bill_depth_mm <dbl> 18.7, 17.4, 18.… ## $ flipper_length_mm <int> 181, 186, 195, … ## $ body_mass_g <int> 3750, 3800, 325… ## $ sex <fct> male, female, f… ## $ year <int> 2007, 2007, 200… ``` ] ] .panel[.panel-name[Scatterplot] Let's see if body mass varies by penguin sex .pull-left[ ```r ggplot(data = penguins, * aes(x = sex, y = body_mass_g)) + geom_point() ``` ] .pull-right[ <img src="data:image/png;base64,#index_files/figure-html/unnamed-chunk-18-1.png" width="504" /> ] ] .panel[.panel-name[Boxplot] .pull-left[ ```r ggplot(data = penguins, aes(x = sex, y = body_mass_g)) + * geom_boxplot() ``` ] .pull-right[ <img src="data:image/png;base64,#index_files/figure-html/unnamed-chunk-20-1.png" width="504" /> ] ] .panel[.panel-name[By Species] .pull-left[ ```r ggplot(data = penguins, aes(x = sex, y = body_mass_g)) + * geom_boxplot(aes(fill = species)) ``` ### <br/> What do you notice? ] .pull-right[ <img src="data:image/png;base64,#index_files/figure-html/unnamed-chunk-22-1.png" width="504" /> ] ] .panel[.panel-name[Chat] ### You might see... .pull-left[ - Gentoo penguins have higher body mass than Adélie and Chinstrap penguins - Higher body mass among male Gentoo penguins compared to female penguins - Pattern not as discernible when comparing Adélie and Chinstrap penguins - No *NA*s among Chinstrap penguin data points! **sex** was available for each observation ] .pull-right[ <img src="data:image/png;base64,#index_files/figure-html/unnamed-chunk-23-1.png" width="504" /> ] ] ] --- class: penguin-tour <img src="data:image/png;base64,#images/pptx/04-dplyr.png" width="1200" /> .footnote[<span>Photo by <a href="https://unsplash.com/@eadesstudio?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">James Eades</a> on <a href="https://unsplash.com/collections/12240655/palmerpenguins/d5aed8c855e26061e5e651d3f180b76d?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></span> ] --- background-image: url(data:image/png;base64,#images/hex/dplyr.png) background-position: 1050px 50px background-size: 80px # dplyr: info .panelset[ .panel[.panel-name[Overview] .pull-left[ ### Data transformation helps you get the data in exactly the right form you need. <br/> With `dplyr` you can: - create new variables - create summaries - rename variables - reorder observations - ...and more! ] .pull-right[ - Pick observations by their values with `filter()`. - Reorder the rows with `arrange()`. - Pick variables by their names `select()`. - Create new variables with functions of existing variables with `mutate()`. - Collapse many values down to a single summary with `summarize()`. - `group_by()` gets the above functions to operate group-by-group rather than on the entire dataset. - and `count()` + `add_count()` simplify `group_by()` + `summarize()` when you just want to count ] ] .panel[.panel-name[Cheatsheet]
PDF: https://github.com/rstudio/cheatsheets/raw/main/data-transformation.pdf ![](data:image/png;base64,#https://raw.githubusercontent.com/rstudio/cheatsheets/master/pngs/thumbnails/data-transformation-cheatsheet-thumbs.png) ] .panel[.panel-name[Reading] .left-column[ <img src="data:image/png;base64,#images/r4ds-cover.png" width="222" /> ] .right-column[ ### R for Data Science: [Ch 11 Data transformation](https://r4ds.had.co.nz/transform.html) ### Package documentation: https://dplyr.tidyverse.org/ ] ] ] --- background-image: url(data:image/png;base64,#images/hex/dplyr.png) background-position: 1050px 50px background-size: 80px # dplyr: exercise .panelset[ .panel[.panel-name[Select] .center[ ### Can you spot the difference in performing the same operation? ] .pull-left[ ```r select(penguins, species, sex, body_mass_g) ## # A tibble: 344 × 3 ## species sex body_mass_g ## <fct> <fct> <int> ## 1 Adelie male 3750 ## 2 Adelie female 3800 ## 3 Adelie female 3250 ## 4 Adelie <NA> NA ## 5 Adelie female 3450 ## 6 Adelie male 3650 ## 7 Adelie female 3625 ## 8 Adelie male 4675 ## 9 Adelie <NA> 3475 ## 10 Adelie <NA> 4250 ## # … with 334 more rows ``` ] .pull-right[ ```r penguins %>% select(species, sex, body_mass_g) ## # A tibble: 344 × 3 ## species sex body_mass_g ## <fct> <fct> <int> ## 1 Adelie male 3750 ## 2 Adelie female 3800 ## 3 Adelie female 3250 ## 4 Adelie <NA> NA ## 5 Adelie female 3450 ## 6 Adelie male 3650 ## 7 Adelie female 3625 ## 8 Adelie male 4675 ## 9 Adelie <NA> 3475 ## 10 Adelie <NA> 4250 ## # … with 334 more rows ``` ] ] .panel[.panel-name[Arrange] We can use `arrange()` to arrange our data in descending order by **body_mass_g** .pull-left[ ```r glimpse(penguins) ## Rows: 344 ## Columns: 8 ## $ species <fct> Adelie, Adelie,… ## $ island <fct> Torgersen, Torg… ## $ bill_length_mm <dbl> 39.1, 39.5, 40.… ## $ bill_depth_mm <dbl> 18.7, 17.4, 18.… ## $ flipper_length_mm <int> 181, 186, 195, … ## $ body_mass_g <int> 3750, 3800, 325… ## $ sex <fct> male, female, f… ## $ year <int> 2007, 2007, 200… ``` ] .pull-right[ ```r penguins %>% select(species, sex, body_mass_g) %>% * arrange(desc(body_mass_g)) ## # A tibble: 344 × 3 ## species sex body_mass_g ## <fct> <fct> <int> ## 1 Gentoo male 6300 ## 2 Gentoo male 6050 ## 3 Gentoo male 6000 ## 4 Gentoo male 6000 ## 5 Gentoo male 5950 ## 6 Gentoo male 5950 ## 7 Gentoo male 5850 ## 8 Gentoo male 5850 ## 9 Gentoo male 5850 ## 10 Gentoo male 5800 ## # … with 334 more rows ``` ] ] .panel[.panel-name[Group By & Summarize] .pull-left[ .middle[We can use `group_by()` to group our data by **species** and **sex**, and `summarize()` to calculate the average **body_mass_g** for each grouping.] ] .pull-right[ ```r penguins %>% select(species, sex, body_mass_g) %>% * group_by(species, sex) %>% * summarize(mean = mean(body_mass_g)) ## # A tibble: 8 × 3 ## # Groups: species [3] ## species sex mean ## <fct> <fct> <dbl> ## 1 Adelie female 3369. ## 2 Adelie male 4043. ## 3 Adelie <NA> NA ## 4 Chinstrap female 3527. ## 5 Chinstrap male 3939. ## 6 Gentoo female 4680. ## 7 Gentoo male 5485. ## 8 Gentoo <NA> NA ``` ] ] .panel[.panel-name[Counting 1] If we're just interested in _counting_ the observations in each grouping, we can group and summarize with special functions `count()` and `add_count()`. ---- .pull-left[ Counting can be done with `group_by()` and `summarize()`, but it's a little cumbersome. It involves... 1. using `mutate()` to create an intermediate variable **n_species** that adds up all observations per **species**, and 2. an `ungroup()`-ing step ] .pull-right[ ```r penguins %>% group_by(species) %>% * mutate(n_species = n()) %>% * ungroup() %>% group_by(species, sex, n_species) %>% summarize(n = n()) ## # A tibble: 8 × 4 ## # Groups: species, sex [8] ## species sex n_species n ## <fct> <fct> <int> <int> ## 1 Adelie female 152 73 ## 2 Adelie male 152 73 ## 3 Adelie <NA> 152 6 ## 4 Chinstrap female 68 34 ## 5 Chinstrap male 68 34 ## 6 Gentoo female 124 58 ## 7 Gentoo male 124 61 ## 8 Gentoo <NA> 124 5 ``` ] ] .panel[.panel-name[Counting 2] If we're just interested in _counting_ the observations in each grouping, we can group and summarize with special functions `count()` and `add_count()`. ---- .pull-left[ In contrast, `count()` and `add_count()` offer a simplified approach .small-text[Example kindly [contributed by Alison Hill (@apreshill)](https://github.com/spcanelon/2020-rladies-chi-tidyverse/issues/2)] ] .pull-right[ ```r penguins %>% count(species, sex) %>% * add_count(species, wt = n, * name = "n_species") ## # A tibble: 8 × 4 ## species sex n n_species ## <fct> <fct> <int> <int> ## 1 Adelie female 73 152 ## 2 Adelie male 73 152 ## 3 Adelie <NA> 6 152 ## 4 Chinstrap female 34 68 ## 5 Chinstrap male 34 68 ## 6 Gentoo female 58 124 ## 7 Gentoo male 61 124 ## 8 Gentoo <NA> 5 124 ``` ] ] .panel[.panel-name[Mutate] .pull-left[ We can add to our counting example by using `mutate()` to create a new variable **prop**, which represents the proportion of penguins of each **sex**, grouped by **species** .small-text[Example kindly [contributed by Alison Hill (@apreshill)](https://github.com/spcanelon/2020-rladies-chi-tidyverse/issues/2)] ] .pull-right[ ```r penguins %>% count(species, sex) %>% add_count(species, wt = n, name = "n_species") %>% * mutate(prop = n/n_species*100) ## # A tibble: 8 × 5 ## species sex n n_species prop ## <fct> <fct> <int> <int> <dbl> ## 1 Adelie female 73 152 48.0 ## 2 Adelie male 73 152 48.0 ## 3 Adelie <NA> 6 152 3.95 ## 4 Chinstrap female 34 68 50 ## 5 Chinstrap male 34 68 50 ## 6 Gentoo female 58 124 46.8 ## 7 Gentoo male 61 124 49.2 ## 8 Gentoo <NA> 5 124 4.03 ``` ] ] .panel[.panel-name[Filter] .pull-left[ Finally, we can filter rows to only show us **Chinstrap** penguin summaries by adding `filter()` to our pipeline] .pull-right[ ```r penguins %>% count(species, sex) %>% add_count(species, wt = n, name = "n_species") %>% mutate(prop = n/n_species*100) %>% * filter(species == "Chinstrap") ## # A tibble: 2 × 5 ## species sex n n_species prop ## <fct> <fct> <int> <int> <dbl> ## 1 Chinstrap female 34 68 50 ## 2 Chinstrap male 34 68 50 ``` ] ] ] --- class: penguin-tour <img src="data:image/png;base64,#images/pptx/05-forcats.png" width="1200" /> .footnote[<span>Photo by <a href="https://unsplash.com/@eadesstudio?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">James Eades</a> on <a href="https://unsplash.com/collections/12240655/palmerpenguins/d5aed8c855e26061e5e651d3f180b76d?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></span> ] --- background-image: url(data:image/png;base64,#images/hex/forcats.png) background-position: 1050px 50px background-size: 80px # forcats: info .panelset[ .panel[.panel-name[Overview] ### Helps us work with **categorical variables** or factors. ### These are variables that have a fixed and known set of possible values, like **species**, **island**, and **sex** in our `penguins` dataset. ] .panel[.panel-name[Cheatsheet]
PDF: https://github.com/rstudio/cheatsheets/raw/main/factors.pdf ![](data:image/png;base64,#https://raw.githubusercontent.com/rstudio/cheatsheets/master/pngs/thumbnails/forcats-cheatsheet-thumbs.png) ] .panel[.panel-name[Reading] .left-column[ <img src="data:image/png;base64,#images/r4ds-cover.png" width="222" /> ] .right-column[ ### R for Data Science: [Ch 15 Factors](https://r4ds.had.co.nz/factors.html) ### Package documentation: https://forcats.tidyverse.org/ ] ] ] --- background-image: url(data:image/png;base64,#images/hex/forcats.png) background-position: 1050px 50px background-size: 80px # forcats: exercise .panelset[ .panel[.panel-name[Code] .pull-left[ ### Currently the **year** variable in `penguins` is continuous from 2007 to 2009. ### There may be situations where this isn't what we want and we might want to turn it into a categorical variable instead. ] .pull-right[ ### The `factor()` function is perfect for this. ```r penguins %>% mutate(year_factor = * factor(year, * levels = unique(year))) ``` ] ] .panel[.panel-name[Result] ### The result is a new factor **year_factor** with levels **2007**, **2008**, and **2009** .pull-left[ ```r penguins_new <- penguins %>% mutate(year_factor = * factor(year, * levels = unique(year))) penguins_new ## # A tibble: 344 × 9 ## species island bill_length_mm ## <fct> <fct> <dbl> ## 1 Adelie Torgersen 39.1 ## 2 Adelie Torgersen 39.5 ## 3 Adelie Torgersen 40.3 ## 4 Adelie Torgersen NA ## 5 Adelie Torgersen 36.7 ## 6 Adelie Torgersen 39.3 ## 7 Adelie Torgersen 38.9 ## 8 Adelie Torgersen 39.2 ## 9 Adelie Torgersen 34.1 ## 10 Adelie Torgersen 42 ## # … with 334 more rows, and 6 more ## # variables: bill_depth_mm <dbl>, ## # flipper_length_mm <int>, ## # body_mass_g <int>, sex <fct>, ## # year <int>, year_factor <fct> ``` ] .pull-right[ ```r class(penguins_new$year_factor) ## [1] "factor" levels(penguins_new$year_factor) ## [1] "2007" "2008" "2009" ``` ] ] ] --- class: penguin-tour <img src="data:image/png;base64,#images/pptx/06-stringr.png" width="1200" /> .footnote[<span>Photo by <a href="https://unsplash.com/@eadesstudio?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">James Eades</a> on <a href="https://unsplash.com/collections/12240655/palmerpenguins/d5aed8c855e26061e5e651d3f180b76d?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></span> ] --- background-image: url(data:image/png;base64,#images/hex/stringr.png) background-position: 1050px 50px background-size: 80px # stringr: info .panelset[ .panel[.panel-name[Overview] .pull-left[ ### `stringr` helps us manipulate strings! The package includes many functions to help us with **regular expressions**, which are a concise language for describing patterns in strings. ] .pull-right[ ### These functions help us - detect matches - subset strings - manage string lengths - mutate strings - join and split strings - order strings - ...and more! ] ] .panel[.panel-name[Cheatsheet]
PDF: https://github.com/rstudio/cheatsheets/raw/main/strings.pdf ![](data:image/png;base64,#https://raw.githubusercontent.com/rstudio/cheatsheets/master/pngs/thumbnails/strings-cheatsheet-thumbs.png) ] .panel[.panel-name[Reading] .left-column[ <img src="data:image/png;base64,#images/r4ds-cover.png" width="222" /> ] .right-column[ ### R for Data Science: [Ch 14 Strings](https://r4ds.had.co.nz/strings.html) ### Package documentation: https://stringr.tidyverse.org/ ] ] ] --- background-image: url(data:image/png;base64,#images/hex/stringr.png) background-position: 1050px 50px background-size: 80px # stringr: exercise .panelset[ .panel[.panel-name[Mutate] ### What does this chunk do? ```r penguins %>% select(species, island) %>% * mutate(ISLAND = str_to_upper(island)) ## # A tibble: 344 × 3 ## species island ISLAND ## <fct> <fct> <chr> ## 1 Adelie Torgersen TORGERSEN ## 2 Adelie Torgersen TORGERSEN ## 3 Adelie Torgersen TORGERSEN ## 4 Adelie Torgersen TORGERSEN ## 5 Adelie Torgersen TORGERSEN ## 6 Adelie Torgersen TORGERSEN ## 7 Adelie Torgersen TORGERSEN ## 8 Adelie Torgersen TORGERSEN ## 9 Adelie Torgersen TORGERSEN ## 10 Adelie Torgersen TORGERSEN ## # … with 334 more rows ``` ] .panel[.panel-name[Join] ### How about this one? ```r penguins %>% select(species, island) %>% mutate(ISLAND = str_to_upper(island)) %>% * mutate(species_island = str_c(species, ISLAND, sep = "_")) ## # A tibble: 344 × 4 ## species island ISLAND species_island ## <fct> <fct> <chr> <chr> ## 1 Adelie Torgersen TORGER… Adelie_TORGER… ## 2 Adelie Torgersen TORGER… Adelie_TORGER… ## 3 Adelie Torgersen TORGER… Adelie_TORGER… ## 4 Adelie Torgersen TORGER… Adelie_TORGER… ## 5 Adelie Torgersen TORGER… Adelie_TORGER… ## 6 Adelie Torgersen TORGER… Adelie_TORGER… ## 7 Adelie Torgersen TORGER… Adelie_TORGER… ## 8 Adelie Torgersen TORGER… Adelie_TORGER… ## 9 Adelie Torgersen TORGER… Adelie_TORGER… ## 10 Adelie Torgersen TORGER… Adelie_TORGER… ## # … with 334 more rows ``` ] ] --- class: penguin-tour <img src="data:image/png;base64,#images/pptx/07-tidyr.png" width="1200" /> .footnote[<span>Photo by <a href="https://unsplash.com/@eadesstudio?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">James Eades</a> on <a href="https://unsplash.com/collections/12240655/palmerpenguins/d5aed8c855e26061e5e651d3f180b76d?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></span> ] --- background-image: url(data:image/png;base64,#images/hex/tidyr.png) background-position: 1050px 50px background-size: 80px # tidyr: info .panelset[ .panel[.panel-name[Overview] [From R for Data Science](https://r4ds.had.co.nz/tidy-data.html): > There are three interrelated rules which make a dataset tidy: > - Each variable must have its own column. > - Each observation must have its own row. > - Each value must have its own cell. ![](data:image/png;base64,#https://d33wubrfki0l68.cloudfront.net/6f1ddb544fc5c69a2478e444ab8112fb0eea23f8/91adc/images/tidy-1.png) ] .panel[.panel-name[Cheatsheet]
PDF: https://github.com/rstudio/cheatsheets/raw/main/data-import.pdf ![](data:image/png;base64,#https://raw.githubusercontent.com/rstudio/cheatsheets/master/pngs/thumbnails/data-import-cheatsheet-thumbs.png) ] .panel[.panel-name[Reading] .left-column[ <img src="data:image/png;base64,#images/r4ds-cover.png" width="222" /> ] .right-column[ ### R for Data Science: [Ch 12 Tidy data](https://r4ds.had.co.nz/tidy-data.html) ### Package documentation: https://tidyr.tidyverse.org/ ] ] ] --- background-image: url(data:image/png;base64,#images/hex/tidyr.png) background-position: 1050px 50px background-size: 80px # tidyr: exercise .panelset[ .panel[.panel-name[Un-tidying] ### Both penguin datasets are already tidy! We can pretend that `penguins` wasn't tidy and that it looked instead like `untidy_penguins` below, where **body_mass_g** was recorded separately for *male*, *female*, and *NA* **sex** penguins. ```r untidy_penguins <- penguins %>% pivot_wider(names_from = sex, values_from = body_mass_g) untidy_penguins ## # A tibble: 344 × 9 ## species island bill_length_mm ## <fct> <fct> <dbl> ## 1 Adelie Torgersen 39.1 ## 2 Adelie Torgersen 39.5 ## 3 Adelie Torgersen 40.3 ## 4 Adelie Torgersen NA ## 5 Adelie Torgersen 36.7 ## 6 Adelie Torgersen 39.3 ## 7 Adelie Torgersen 38.9 ## 8 Adelie Torgersen 39.2 ## 9 Adelie Torgersen 34.1 ## 10 Adelie Torgersen 42 ## # … with 334 more rows, and 6 more ## # variables: bill_depth_mm <dbl>, ## # flipper_length_mm <int>, year <int>, ## # male <int>, female <int>, NA <int> ``` ] .panel[.panel-name[Re-tidying] ### Now let's make it tidy again! We'll use the help of `pivot_longer()` ```r untidy_penguins %>% * pivot_longer(cols = male:`NA`, * names_to = "sex", * values_to = "body_mass_g") ## # A tibble: 1,032 × 8 ## species island bill_length_mm ## <fct> <fct> <dbl> ## 1 Adelie Torgersen 39.1 ## 2 Adelie Torgersen 39.1 ## 3 Adelie Torgersen 39.1 ## 4 Adelie Torgersen 39.5 ## 5 Adelie Torgersen 39.5 ## 6 Adelie Torgersen 39.5 ## 7 Adelie Torgersen 40.3 ## 8 Adelie Torgersen 40.3 ## 9 Adelie Torgersen 40.3 ## 10 Adelie Torgersen NA ## # … with 1,022 more rows, and 5 more ## # variables: bill_depth_mm <dbl>, ## # flipper_length_mm <int>, year <int>, ## # sex <chr>, body_mass_g <int> ``` ] ] --- class: penguin-tour <img src="data:image/png;base64,#images/pptx/08-purrr.png" width="1200" /> .footnote[<span>Photo by <a href="https://unsplash.com/@eadesstudio?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">James Eades</a> on <a href="https://unsplash.com/collections/12240655/palmerpenguins/d5aed8c855e26061e5e651d3f180b76d?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></span> ] --- background-image: url(data:image/png;base64,#images/hex/purrr.png) background-position: 1050px 50px background-size: 80px # purrr: info .panelset[ .panel[.panel-name[Overview] .pull-left[ ### Provides tools for working with functions and vectors ### The `purrr` family of functions helps us replace for loops, making our code easier to read and more succint. ] .pull-right[ ### With `purrr` you can - Iterate over a single input with `map()` - Iterate over two inputs in parallel with `map2()` - Iterate with multiple arguments with `pmap()` - Iterate with multiple arguments and functions with `invoke_map()` - Call a function for its side-effects with `walk()`, `walk2()`, and `pwalk()` ] ] .panel[.panel-name[Cheatsheet]
PDF: https://github.com/rstudio/cheatsheets/raw/main/purrr.pdf ![](data:image/png;base64,#https://raw.githubusercontent.com/rstudio/cheatsheets/master/pngs/thumbnails/purrr-cheatsheet-thumbs.png) ] .panel[.panel-name[Reading] .left-column[ <img src="data:image/png;base64,#images/r4ds-cover.png" width="222" /> ] .right-column[ ### R for Data Science: [Ch 21 Iteration](https://r4ds.had.co.nz/iteration.html) ### Package documentation: https://purrr.tidyverse.org/ ] ] ] --- background-image: url(data:image/png;base64,#images/hex/purrr.png) background-position: 1050px 50px background-size: 80px # purrr: exercise .panelset[ .panel[.panel-name[Time for a change?] .pull-left[ ### Ok, we love our earlier boxplot showing us **body_mass_g** by **sex** and colored by **species**... but let's change up the colors to keep with our Antarctica theme! ### I'm a big fan of the color palettes in the `nord` 📦 ] .pull-right[ ![](data:image/png;base64,#https://raw.githubusercontent.com/jkaupp/nord/master/man/figures/README-palettes-1.png) ] ] .panel[.panel-name[Goal] .pull-left[ ### Let's turn this plot <img src="data:image/png;base64,#index_files/figure-html/unnamed-chunk-50-1.png" width="504" /> ] .pull-right[ ### Into this one! <img src="data:image/png;base64,#index_files/figure-html/unnamed-chunk-51-1.png" width="504" /> .panel[.panel-name[Option 1] .pull-left[ ```r library(nord) # you can choose colors using # the color hex codes nord::nord_palettes$frost ## [1] "#8FBCBB" "#88C0D0" "#81A1C1" "#5E81AC" ``` ```r # and assign them using the # `scale_fill_manual()` function penguins %>% ggplot(aes(x = sex, y = body_mass_g)) + geom_boxplot(aes(fill = species)) + * scale_fill_manual(values = c("#8FBCBB", "#88C0D0", "#81A1C1")) ``` ] .pull-right[ <img src="data:image/png;base64,#index_files/figure-html/unnamed-chunk-54-1.png" width="504" /> ] ] .panel[.panel-name[Options 2 & 3] .pull-left[ ...but you might prefer to use the palette name! <br/> ```r penguins %>% ggplot(aes(x = sex, y = body_mass_g)) + geom_boxplot(aes(fill = species)) + * scale_fill_manual(values = nord::nord_palettes$frost) ``` <img src="data:image/png;base64,#index_files/figure-html/unnamed-chunk-55-1.png" width="360" /> ] .pull-right[ And some color palette packages also come with their own functions like `scale_fill_nord()` ```r penguins %>% ggplot(aes(x = sex, y = body_mass_g)) + geom_boxplot(aes(fill = species)) + * nord::scale_fill_nord(palette = "frost") ``` <img src="data:image/png;base64,#index_files/figure-html/unnamed-chunk-56-1.png" width="360" /> ] ] .panel[.panel-name[Purrr?] .pull-left[ The `prismatic` 📦 helps us **see** the colors that correspond to each color hex code (mostly), with the `color()` function ```r library(prismatic) ``` ```r prismatic::color(nord::nord_palettes$frost) ``` ![](data:image/png;base64,#images/nord_frost.png) ] .pull-right[ `purrr`'s `map()` function can help us iterate `color()` over all palettes in a palette package like `nord`! ```r nord::nord_palettes %>% map(prismatic::color) ``` ![](data:image/png;base64,#images/nord_multiple.png) ] ] ] ] .panel[.panel-name[More palettes!] .pull-left[ ### 🎨 [r-color-palettes repo](https://github.com/EmilHvitfeldt/r-color-palettes) from Emil Hvitfeldt ### Like this Wes Anderson themed one! And many, many others. 🤩 ] .pull-right[ ![](data:image/png;base64,#images/wesanderson_example.png) ] ] ] --- class: about-me, middle, center # Thank you! ## Any questions?