class: title-slide, right, top background-image: url(data:image/png;base64,#img/flowers.JPG) background-position: 90% 75%, 75% 75% background-size:cover .left-column[ # GRS Workshop<br>Introduction to ggplot ] .right-column[ ### colours and facets **Eugene Hickey**<br> March 9th 2022 ] .palegrey[.left[.footnote[Graphic by [Elaine Hickey](https://photos.google.com/photo/AF1QipMjKNoaxyne8nte4HmxA6Th9-4fUfSbl_mx-_1G)]]] ??? Welcome to the workshop on ggplot. Where we'll show you how to create impressive data visualisations. --- layout: true <a class="footer-link" href="http://intro-ggplot-nhs.netlify.app">intro-ggplot-nhs — Eugene Hickey</a> <!-- this adds the link footer to all slides, depends on footer-link class in css--> --- # Choice of Colours in R <br> ### We'll also discuss faceting. - colours are very important - second only to position for perception - can carry information - also important to be visually pleasing - worthwhile to make your figures aesthetically attractive - visualisations that are engaging are more effective --- ### Types of Colour Scales .pull-left[ - qualitative - suite of colours easily distinguished - no heirarchy - caters for visual impairments - sequential - band of colours, increasingly intense - go from low to high - diverging - suite of colours from minus to plus - contrasting colours at each end - something neutral in the middle ] .pull-right[ ![](data:image/png;base64,#04-colours_files/figure-html/qualitative_palette-1.png)<!-- --> ![](data:image/png;base64,#04-colours_files/figure-html/sequential_palette-1.png)<!-- --> ![](data:image/png;base64,#04-colours_files/figure-html/diverging_palette-1.png)<!-- --> ] --- # Getting Colours in R - some really great packages - <span style='color: #B03A2E'>RColorBrewer</span> - excellent, fine control over palette choice - <span style='color: #B03A2E'>viridis</span> - excels at palettes for vision-impaired readers - <span style='color: #B03A2E'>paletteer</span> - collection of palettes from various sources --- # Getting Colours in R cont.. - some really great packages - <span style='color: #B03A2E'>wesanderson</span> - <i>names(wes_palettes)</i> followed by <i>wes_palette("BottleRocket1")</i> --- - more.... - tvthemes() - not just colours, but layouts and fonts - everything from Game of Thrones to Spongebob (yes, really) - ggsci(), palettes for scientific publications (Lancet, AAAS, etc) --- - and more.... - colorspace() - resources for picking colours - choose_color() and choose_palette() - can convert colours based on vision deficiencies - will convert from colour descriptions, e.g. hex2RGB() - and a [cheatsheet](https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/colorPaletteCheatsheet.pdf) ---
--- ## Ways of Describing Colours - by name: "red", "cyan", "violetred4", "thistle"..... - get full list of 657 available in R from _colors()_ - by hex code: "#f49340", "#40f9f9", "#ee82ef", "#d8bfd1".... (see [htmlcolors](https://htmlcolorcodes.com/)]) - by rgb values: (249, 67, 64), (64, 249, 249), (57, 14, 30), (216, 191, 209).... - note, rgb() function takes these as a fraction from 0-1 - by hcl values: (53.24, 179.04, 12.17), (91.11, 72.10, 192.17), (32.36, 63.11, 349.86), (80.08, 20.79, 307.73).... ---
--- # Investigating Colours in R - the following code shows the first "N" colours in R where N is set to 20 here: ![](data:image/png;base64,#04-colours_files/figure-html/base_colours-1.png)<!-- --> --- ## Other Usful Functions - _show_col()_ from the _scales_ package is super useful - e.g. show_col("red") or show_col("#84a412") - *rgb()* will give a hex code for a fraction of red, green, blue - e.g. rgb(0.4, 0.2, 0.5) gives "#663380" --- - the whole `farver` package is great for converting colour formats - *colourPicker()* from the `colourpicker` package - colourPicker(numCols = 4), opens up shiny app, returns colours - *col2rgb()*, also *col2hex()* from the `gplots` (not _ggplot2_) package, and *col2hcl* from the `jmw86069/jamba` package - this last is on github, so you must install the package `devtools` then do `install_github( jmw86069/jamba)` - _colorfindr_ takes an image and identifies major colours --- .pull-left[ ![](data:image/png;base64,#img/simpsons.jpeg) ] .pull-right[ ``` ## # A tibble: 6 x 3 ## col_hex col_freq col_share ## <chr> <int> <dbl> ## 1 #F9C6CB 1511 0.0300 ## 2 #7183C1 508 0.0101 ## 3 #268EB3 259 0.00515 ## 4 #AFB7DE 244 0.00485 ## 5 #6F81BF 99 0.00197 ## 6 #FAC7CC 99 0.00197 ``` ![](data:image/png;base64,#04-colours_files/figure-html/unnamed-chunk-1-1.png)<!-- --> ] --- # Some Websites and Tools - [coolors.co](coolors.co) - will generate appropriate palettes - [colorpicker](http://tristen.ca/hcl-picker/#/hlc/6/1.1/8C4443/845128) - [colorspace](http://colorspace.r-forge.r-project.org/articles/hcl_palettes.html#qualitative-palettes) - Chrome has an _**Eye Dropper**_ tool - click on part of a webpage and it will tell you the colour - Nice description of colurs from [Stowers](https://www.uv.es/conesa/CursoR/material/UsingColorInR.pdf) --- # Colours in _ggplot()_ - use for _fill_ and for _col_ aesthetics - add the _scale_fill_... and _scale_color_... layers to control - explore these by typing _?scale_fill_ and then TAB to see the range of options --- ![](data:image/png;base64,#04-colours_files/figure-html/unnamed-chunk-2-1.png)<!-- --> --- ![](data:image/png;base64,#04-colours_files/figure-html/unnamed-chunk-3-1.png)<!-- --> --- ![](data:image/png;base64,#04-colours_files/figure-html/unnamed-chunk-4-1.png)<!-- --> --- ![](data:image/png;base64,#04-colours_files/figure-html/unnamed-chunk-5-1.png)<!-- --> --- ![](data:image/png;base64,#04-colours_files/figure-html/unnamed-chunk-6-1.png)<!-- --> --- ![](data:image/png;base64,#04-colours_files/figure-html/unnamed-chunk-7-1.png)<!-- --> --- ![](data:image/png;base64,#04-colours_files/figure-html/unnamed-chunk-8-1.png)<!-- --> --- ![](data:image/png;base64,#04-colours_files/figure-html/unnamed-chunk-9-1.png)<!-- --> ---
--- class: center, inverse # _faceting_ - Faceting means producing multiple panels of a plot - Splits a plot into several versions based on a categorical variable - functions _facet_wrap()_ and _facet_grid()_ - useful when lots of data in different subsets - important to keep axis scales the same --- ```r sex <- c("Female", "Male"); names(sex) <- c("F", "M") cats %>% ### cats dataset from MASS ggplot(aes(Bwt, Hwt)) + geom_point() + geom_smooth(aes(col = Sex), show.legend = F, se = F) + facet_grid(~Sex, labeller = labeller(Sex = sex)) + labs(x = "Bodyweight (kg)", y = "Heart Weight (g)") + theme_minimal() + theme(strip.background = element_blank(), text = element_text(size = 20)) ``` ![](data:image/png;base64,#04-colours_files/figure-html/facet_example1-1.png)<!-- --> --- ![](data:image/png;base64,#04-colours_files/figure-html/facet_example2-1.png)<!-- --> --- ```r snails %>% ## snails dataset from the MASS library ggplot(aes(Exposure, Deaths, col = factor(Rel.Hum))) + facet_grid(Species ~ Temp) + geom_line() + geom_point() + theme_minimal() + theme(legend.title = element_blank(), text = element_text(size = 20)) ``` ![](data:image/png;base64,#04-colours_files/figure-html/facet_example3-1.png)<!-- -->