class: monash-bg-blue center middle hide-slide-number <div class="bg-black white" style="width:45%;right:0;bottom:0;padding-left:5px;border: solid 4px white;margin: auto;"> <i class="fas fa-exclamation-circle"></i> These slides are viewed best by Chrome and occasionally need to be refreshed if elements did not load properly. See here for <a href=part1-session3.pdf>PDF <i class="fas fa-file-pdf"></i></a>. </div> <br> .white[Push the **right arrow key** to see the next slide.] --- count: false background-image: url(images/ramakant-sharda-0aFg0U2uPJY-unsplash.jpg) background-size: cover class: hide-slide-number title-slide <div class="grid-row" style="grid: 1fr / 2fr;"> .item.center[ # <span style="text-shadow: 2px 2px 30px white;">Data Visualisation with R<br>Workshop Part 1</span> <!-- ## <span style="color:;text-shadow: 2px 2px 30px black;">Scales and color</span> --> ] .center.shade_black.animated.bounceInUp.slower[ <br><br> ## Scales and color <br> Presented by Emi Tanaka Department of Econometrics and Business Statistics <img src="images/monash-one-line-reversed.png" style="width:500px"><br>
<i class="fas fa-envelope faa-float animated "></i>
emi.tanaka@monash.edu
<i class="fab fa-twitter faa-float animated faa-fast "></i>
@statsgen .bottom_abs.width100.bg-black[ 6th Dec 2021 @ Statistical Society of Australia NSW Branch | Zoom ] ] </div> --- class: transition middle animated slideInLeft # .circle-big[3] # Scales --- class: font_smaller # π Diamonds data The `diamonds` data is part of `ggplot2` π¦ ```r glimpse(diamonds) ``` ``` ## Rows: 53,940 ## Columns: 10 ## $ carat <dbl> 0.23, 0.21, 0.23, 0.29, 0.31, 0.24, 0.24, 0.26, 0.22, 0.23, 0.β¦ ## $ cut <ord> Ideal, Premium, Good, Premium, Good, Very Good, Very Good, Verβ¦ ## $ color <ord> E, E, E, I, J, J, I, H, E, H, J, J, F, J, E, E, I, J, J, J, I,β¦ ## $ clarity <ord> SI2, SI1, VS1, VS2, SI2, VVS2, VVS1, SI1, VS2, VS1, SI1, VS1, β¦ ## $ depth <dbl> 61.5, 59.8, 56.9, 62.4, 63.3, 62.8, 62.3, 61.9, 65.1, 59.4, 64β¦ ## $ table <dbl> 55, 61, 65, 58, 58, 57, 57, 55, 61, 61, 55, 56, 61, 54, 62, 58β¦ ## $ price <int> 326, 326, 327, 334, 335, 336, 336, 337, 337, 338, 339, 340, 34β¦ ## $ x <dbl> 3.95, 3.89, 4.05, 4.20, 4.34, 3.94, 3.95, 4.07, 3.87, 4.00, 4.β¦ ## $ y <dbl> 3.98, 3.84, 4.07, 4.23, 4.35, 3.96, 3.98, 4.11, 3.78, 4.05, 4.β¦ ## $ z <dbl> 2.43, 2.31, 2.31, 2.63, 2.75, 2.48, 2.47, 2.53, 2.49, 2.39, 2.β¦ ``` --- class: font_smaller # Scales * Scales control the mapping from .blue[data] to .blue[aesthetics]. <center> <img src="images/ggplot-scale.png" width = "60%"/> </center> ```r g <- ggplot(diamonds, aes(carat, price) ) + geom_hex() ``` --- .grid[ .item.border-right[ ```r g + scale_y_continuous() + scale_x_continuous() ``` <img src="images/part1-session3/diamond1-1.png" style="display: block; margin: auto;" /> ] .item.border-right[ ```r g + scale_x_reverse() + scale_y_continuous(trans="log10") ``` <img src="images/part1-session3/diamond2-1.png" style="display: block; margin: auto;" /> ] .item[ ```r g + scale_y_log10() + scale_x_sqrt() ``` <img src="images/part1-session3/diamond3-1.png" style="display: block; margin: auto;" /> ] ] --- name: scale class: hide-slide-number # `scale`
--- # Guide: an axis or a legend * The scale creates a .blue[guide]: an .blue[axis] or .blue[legend]. * So to modify these you generally use .blue[`scale_*`] or other handy functions (`guides`, `labs`, `xlab`, `ylab` and so on). <center> <img src="images/guides-dissection.png" width = "60%"/> </center> --- # Modify axis ```r g + scale_y_continuous(name = "Price", breaks = c(0, 10000), labels = c("0", "More\n than\n 10K")) + geom_hline(yintercept = 10000, color = "red", size = 2) ``` <img src="images/part1-session3/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> --- # Nicer formatting functions in `scales` π¦ ```r g + scale_y_continuous( * label = scales::dollar_format() ) ``` <img src="images/part1-session3/unnamed-chunk-7-1.png" style="display: block; margin: auto;" /> --- # Modifying legend ```r g + scale_fill_continuous( breaks = c(0, 10, 100, 1000, 4000), trans = "log10" ) ``` <img src="images/part1-session3/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> --- # Removing legend ```r g + scale_fill_continuous( guide = "none" ) ``` <img src="images/part1-session3/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> --- # Alternative control of guides ```r g + ylab("Price") + # Changes the y axis label labs(x = "Carat", # Changes the x axis label fill = "Count") # Changes the legend name ``` <img src="images/part1-session3/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> ```r g + guides(fill = "none") # remove the legend ``` --- class: transition middle animated slideInLeft # .circle-big[4] # Color space .footnote.monash-bg-blue[ Zeileis, Fisher, Hornik, Ihaka, McWhite, Murrell, Stauffer, Wilke (2019). colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes. *arXiv 1903.06490* Zeileis, Hornik, Murrell (2009). Escaping RGBland: Selecting Colors for Statistical Graphics. _Computational Statistics & Data Analysis_ 53(9) 3259-3270 ] --- class: font_smaller center # Qualitative palettes designed for categorical variable with no particular ordering ```r colorspace::hcl_palettes("Qualitative", plot = "TRUE", n = 7) ``` <img src="images/part1-session3/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> --- class: font_smaller center # Sequential palettes designed for ordered categorical variable or number going from low to high (or vice-versa) ```r colorspace::hcl_palettes("Sequential", plot = "TRUE", n = 7) ``` <img src="images/part1-session3/unnamed-chunk-13-1.png" style="display: block; margin: auto;" /> --- class: font_smaller center # Diverging palettes designed for ordered categorical variable or number going from low to high (or vice-versa) with a neutral value in between ```r colorspace::hcl_palettes("Diverging", plot = "TRUE", n = 7) ``` <img src="images/part1-session3/unnamed-chunk-14-1.png" style="display: block; margin: auto;" /> --- class: no-header-strip rgb-slider-wrap center white # RGB color space made for screen projection <br><br> <div class="col-sliders"> <div> <label for="red">Red</label> <input type="number" id="redNum"> <input value="0" type="range" min="0" max="255" id="red"> </div> <div> <label for="green">Green</label> <input type="number" id="greenNum"> <input value="109" type="range" min="0" max="255" id="green"> </div> <div> <label for="blue">Blue</label> <input type="number" id="blueNum"> <input value="174" type="range" min="0" max="255" id="blue"> </div> </div> .footnote.bg-transparent[ Code adapted from https://github.com/Golobro/rgbcolorslider ] --- class: no-header-strip hcl-slider-wrap center white # HCL color space made for human visual system <br><br> <div class="col-sliders"> <div> <label for="hue">Hue</label> <input type="number" id="hueNum"> <input value="268" type="range" min="0" max="360" id="hue"> </div> <div> <label for="chroma">Chroma</label> <input type="number" id="chromaNum"> <input value="42" type="range" min="0" max="180" id="chroma"> </div> <div> <label for="luminance">Luminance</label> <input type="number" id="luminanceNum"> <input value="44" type="range" min="0" max="100" id="luminance"> </div> </div> .footnote.bg-transparent[ Color conversion using https://github.com/gka/chroma.js ] --- # `colorspace` π¦ .center[ Interactively choose/create a palette using the HCL color space. ] .grid[ .item[ ] .item[ <br> ```r library(colorspace) hcl_wizard() # OR choose_palette() ``` <br> .center.animated.rubberBand[ .tag[LIVE DEMO] <br> ] ] .item[ ] ] --- class: font_smaller # `hcl_wizard` <center> <img src="images/hclwizard.png" width = "80%"><br> Choose your palette > Export > R > Copy the command </center> --- class: font_smaller # Registering your own palette .grid[ .item[ ```r library(colorspace) # register your palette sequential_hcl(n = 7, h = c(300, 200), c = c(60, 0), l = c(25, 95), power = c(2.1, 0.8), register = "my-set") # now generate from your palette sequential_hcl(n = 3, palette = "my-set") ``` ``` ## [1] "#6B0077" "#7C8393" "#F1F1F1" ``` ] .item[ {{content}} ] ] -- ```r hcl_palettes(n = 5, palette = "my-set", plot = T) ``` <img src="images/part1-session3/unnamed-chunk-17-1.png" style="display: block; margin: auto;" /> <br> Combining with `ggplot`: ```r ggplot(penguins, aes(bill_length_mm, fill = species)) + geom_density(alpha = 0.6) + # notice here you don't need to specify the n! scale_fill_discrete_sequential(palette = "my-set") ``` <img src="images/part1-session3/unnamed-chunk-18-1.png" style="display: block; margin: auto;" /> --- class: font_smaller # Manually selecting colors <center> <img src="images/lter_penguins.png" width = "250px"/> </center> ```r g <- ggplot(penguins, aes(bill_length_mm, fill = species)) + geom_density(alpha = 0.6) + scale_fill_manual( breaks = c("Adelie", "Chinstrap", "Gentoo"), # optional but makes it more robust values = c("darkorange", "purple", "cyan4")) g ``` <img src="images/part1-session3/unnamed-chunk-19-1.png" style="display: block; margin: auto;" /> --- class: font_smaller # Check that it's colour blind friendly! ```r cols <- c("darkorange", "purple", "cyan4") ``` .grid[ .item50.border-right[ ```r g # original ``` <img src="images/part1-session3/unnamed-chunk-21-1.png" style="display: block; margin: auto;" /> ```r g + scale_fill_manual(values = deutan(cols)) ``` <img src="images/part1-session3/unnamed-chunk-21-2.png" style="display: block; margin: auto;" /> ] .item50[ ```r g + scale_fill_manual(values = protan(cols)) ``` <img src="images/part1-session3/unnamed-chunk-22-1.png" style="display: block; margin: auto;" /> ```r g + scale_fill_manual(values = tritan(cols)) ``` <img src="images/part1-session3/unnamed-chunk-22-2.png" style="display: block; margin: auto;" /> ] ] --- class: exercise middle hide-slide-number <i class="fas fa-users"></i> # <i class="fas fa-code"></i> Open `part1-exercise-03.Rmd` <center>
15
:
00
</center> --- class: font_smaller background-color: #e5e5e5 # Session Information .scroll-350[ ```r devtools::session_info() ``` ``` ## β Session info π‘ ππ» π π» βββββββββββββββββββββββββββββββββββββββββββββββββ ## hash: ferris wheel, backhand index pointing right: light skin tone, person gesturing NO: light skin tone ## ## 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_AU.UTF-8 ## ctype en_AU.UTF-8 ## tz Australia/Melbourne ## date 2021-12-06 ## pandoc 2.11.4 @ /Applications/RStudio.app/Contents/MacOS/pandoc/ (via rmarkdown) ## ## β Packages βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ ## package * version date (UTC) lib source ## anicon 0.1.0 2021-11-30 [1] Github (emitanaka/anicon@0b756df) ## assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.1.0) ## backports 1.3.0 2021-10-27 [1] CRAN (R 4.1.0) ## broom 0.7.10 2021-10-31 [1] CRAN (R 4.1.0) ## bslib 0.3.1 2021-10-06 [1] CRAN (R 4.1.0) ## cachem 1.0.6 2021-08-19 [1] CRAN (R 4.1.0) ## callr 3.7.0 2021-04-20 [1] CRAN (R 4.1.0) ## cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.1.0) ## cli 3.1.0 2021-10-27 [1] CRAN (R 4.1.0) ## colorspace * 2.0-2 2021-06-24 [1] CRAN (R 4.1.0) ## countdown 0.3.5 2021-11-30 [1] Github (gadenbuie/countdown@a544fa4) ## crayon 1.4.2 2021-10-29 [1] CRAN (R 4.1.0) ## crosstalk 1.2.0 2021-11-04 [1] CRAN (R 4.1.0) ## DBI 1.1.1 2021-01-15 [1] CRAN (R 4.1.0) ## dbplyr 2.1.1 2021-04-06 [1] CRAN (R 4.1.0) ## desc 1.4.0 2021-09-28 [1] CRAN (R 4.1.0) ## devtools 2.4.2 2021-06-07 [1] CRAN (R 4.1.0) ## digest 0.6.28 2021-09-23 [1] CRAN (R 4.1.0) ## dplyr * 1.0.7 2021-06-18 [1] CRAN (R 4.1.0) ## DT 0.20 2021-11-15 [1] CRAN (R 4.1.0) ## ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.0) ## evaluate 0.14 2019-05-28 [1] CRAN (R 4.1.0) ## fansi 0.5.0 2021-05-25 [1] CRAN (R 4.1.0) ## 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) ## forcats * 0.5.1 2021-01-27 [1] CRAN (R 4.1.0) ## fs 1.5.0 2020-07-31 [1] CRAN (R 4.1.0) ## generics 0.1.1 2021-10-25 [1] CRAN (R 4.1.0) ## ggplot2 * 3.3.5 2021-06-25 [1] CRAN (R 4.1.0) ## glue 1.5.0 2021-11-07 [1] CRAN (R 4.1.0) ## gtable 0.3.0 2019-03-25 [1] CRAN (R 4.1.0) ## haven 2.4.3 2021-08-04 [1] CRAN (R 4.1.0) ## hexbin 1.28.2 2021-01-08 [1] CRAN (R 4.1.0) ## highr 0.9 2021-04-16 [1] CRAN (R 4.1.0) ## hms 1.1.1 2021-09-26 [1] CRAN (R 4.1.0) ## htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.0) ## htmlwidgets 1.5.4 2021-09-08 [1] CRAN (R 4.1.0) ## httr 1.4.2 2020-07-20 [1] CRAN (R 4.1.0) ## icon 0.1.0 2021-11-30 [1] Github (emitanaka/icon@8458546) ## jquerylib 0.1.4 2021-04-26 [1] CRAN (R 4.1.0) ## jsonlite 1.7.2 2020-12-09 [1] CRAN (R 4.1.0) ## knitr 1.36 2021-09-29 [1] CRAN (R 4.1.0) ## labeling 0.4.2 2020-10-20 [1] CRAN (R 4.1.0) ## lattice 0.20-45 2021-09-22 [1] CRAN (R 4.1.2) ## lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.0) ## lubridate 1.8.0 2021-10-07 [1] CRAN (R 4.1.0) ## magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.1.0) ## memoise 2.0.0 2021-01-26 [1] CRAN (R 4.1.0) ## modelr 0.1.8 2020-05-19 [1] CRAN (R 4.1.0) ## munsell 0.5.0 2018-06-12 [1] CRAN (R 4.1.0) ## palmerpenguins * 0.1.0 2020-07-23 [1] CRAN (R 4.1.0) ## pillar 1.6.4 2021-10-18 [1] CRAN (R 4.1.0) ## pkgbuild 1.2.0 2020-12-15 [1] CRAN (R 4.1.0) ## pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.0) ## pkgload 1.2.3 2021-10-13 [1] CRAN (R 4.1.0) ## prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.1.0) ## processx 3.5.2 2021-04-30 [1] CRAN (R 4.1.0) ## ps 1.6.0 2021-02-28 [1] CRAN (R 4.1.0) ## purrr * 0.3.4 2020-04-17 [1] CRAN (R 4.1.0) ## R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.0) ## Rcpp 1.0.7 2021-07-07 [1] CRAN (R 4.1.0) ## readr * 2.1.0 2021-11-11 [1] CRAN (R 4.1.0) ## readxl 1.3.1 2019-03-13 [1] CRAN (R 4.1.0) ## remotes 2.4.1 2021-09-29 [1] CRAN (R 4.1.0) ## reprex 2.0.1 2021-08-05 [1] CRAN (R 4.1.0) ## rlang 0.4.12 2021-10-18 [1] CRAN (R 4.1.0) ## rmarkdown 2.11 2021-09-14 [1] CRAN (R 4.1.0) ## rprojroot 2.0.2 2020-11-15 [1] CRAN (R 4.1.0) ## rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.0) ## rvest 1.0.2 2021-10-16 [1] CRAN (R 4.1.0) ## sass 0.4.0 2021-05-12 [1] CRAN (R 4.1.0) ## scales 1.1.1 2020-05-11 [1] CRAN (R 4.1.0) ## sessioninfo 1.2.1 2021-11-02 [1] CRAN (R 4.1.0) ## stringi 1.7.5 2021-10-04 [1] CRAN (R 4.1.0) ## stringr * 1.4.0 2019-02-10 [1] CRAN (R 4.1.0) ## testthat 3.1.0 2021-10-04 [1] CRAN (R 4.1.0) ## tibble * 3.1.6 2021-11-07 [1] CRAN (R 4.1.0) ## tidyr * 1.1.4 2021-09-27 [1] CRAN (R 4.1.0) ## tidyselect 1.1.1 2021-04-30 [1] CRAN (R 4.1.0) ## tidyverse * 1.3.1 2021-04-15 [1] CRAN (R 4.1.0) ## tzdb 0.2.0 2021-10-27 [1] CRAN (R 4.1.0) ## usethis 2.1.3 2021-10-27 [1] CRAN (R 4.1.0) ## utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.0) ## vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.1.0) ## whisker 0.4 2019-08-28 [1] CRAN (R 4.1.0) ## withr 2.4.2 2021-04-18 [1] CRAN (R 4.1.0) ## xaringan 0.22 2021-06-23 [1] CRAN (R 4.1.0) ## xfun 0.28 2021-11-04 [1] CRAN (R 4.1.0) ## xml2 1.3.2 2020-04-23 [1] CRAN (R 4.1.0) ## yaml 2.2.1 2020-02-01 [1] CRAN (R 4.1.0) ## ## [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library ## ## ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ ``` ] These slides are licensed under <br><center><a href="https://creativecommons.org/licenses/by-sa/3.0/au/"><img src="images/cc.svg" style="height:2em;"/><img src="images/by.svg" style="height:2em;"/><img src="images/sa.svg" style="height:2em;"/></a></center>