class: title-slide, right, top background-image: url(data:image/png;base64,#img/fog.png) background-position: 90% 75%, 75% 75% background-size:cover .left-column[ # NHS Workshop<br>Introduction to ggplot ] .right-column[ ### fonts and themes **Eugene Hickey**<br> January 21st 2021 ] .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--> --- class: center, inverse # <p style="font-family:'Cabin Sketch'">Using Themes in R</p> <br> ### We'll also discuss fonts (first) and scales. - themes give fine control to the appearance of your plots - control over text size, fonts, text colour, etc - position of legends, titles, captions, etc - colours of backgrounds - delete unwanted features (like, say, tick marks on an axis) - large number of preset themes - several packages with neat bundle of useful themes - we'll discuss some of these later - and, of course, we can develop our own theme to have consistent graphics --- ## Fonts - we'll discuss this first, as often themes require fonts which might not be present - fonts are a whole world of their own - see [practicaltypography.com](practicaltypography.com) - great talk by [Will Chase](https://rstudio.com/resources/rstudioconf-2020/the-glamour-of-graphics/) at RStudioConf2020 --- <img src="data:image/png;base64,#img/youll-always-be-mine.jpg" width="50%" /> --- ![](data:image/png;base64,#img/Bad_Font_Choices_-_Megaflicks.jpg) --- ![](data:image/png;base64,#img/Bad_Font_Choices_-_My_Favorite_Color_Is_Glitter.jpg) --- ![](data:image/png;base64,#img/Bad_Font_Choices_-_Kickboxing.jpg) --- - need to add fonts before we can use them - simplest way is using *font_add_google* from the [showtext](https://github.com/yixuan/showtext) package .pull-left[ ```r theme_update(text = element_text(size = 32)) library(showtext) font_add_google(name = "Covered By Your Grace", family = "grace") *showtext_auto() msleep %>% ggplot(aes(x = bodywt, y = brainwt, colour = order)) + geom_point(show.legend = F) + scale_x_log10(labels = scales::label_comma()) + scale_y_log10(labels = scales::label_comma()) + labs(title = "Bigger Animals-Bigger Brains") + * theme(text = element_text(family = "grace")) ``` ] .pull-right[ ![](data:image/png;base64,#05-themes_files/figure-html/font_add_google-out-1.png)<!-- --> ] --- - what if we want more than just the google fonts - download font from the web (need *.ttf* file) - see [fonts.google.com](https://fonts.google.com/) - or [fontsquirrel.com](https://www.fontsquirrel.com/) - or [1001freefonts.com](https://www.1001freefonts.com/) - use *showtext::font_add(family = "some neat font name", regular = "path/to/downloaded.ttf")* where we give the directory where we downloaded the .ttf file - again, need *showtext_auto()* to make them live --- - above processes have to be carried out every time we run R - can make them permanently available too - need to be in the system, load them into windows / mac / linux - only really works for true type fonts (.ttf) - go to folder where the R library lives and seek out fonts - type **.libPaths()** to find out where - e.g. R/x86_64-pc/3.6/tvthemes/fonts/SpongeBob - click on .ttf files to install - then need to capture them in R - install package *extrafont* - run *ttf_import()* with path = folder from above - run *View(fonttable())* to check available fonts - usually need to restart R (*Session* then *Restart R*) - alternative is to use [thematic](remotes::install_github("rstudio/thematic")), see below ---
--- ## Complete Themes - these set up ggplots with standard appearances - can always adjust these, but do so in a layer after invoking the theme - some defaults in ggplot2, see [here](https://ggplot2.tidyverse.org/reference/ggtheme.html) - you should experiment with these to see how they look --- count: false .panel1-my_rotate1-rotate[ ```r palmerpenguins::penguins %>% ggplot(aes(body_mass_g, flipper_length_mm, col = island)) + geom_point() + * theme_classic() ``` ] .panel2-my_rotate1-rotate[ ![](data:image/png;base64,#05-themes_files/figure-html/my_rotate1_rotate_01_output-1.png)<!-- --> ] --- count: false .panel1-my_rotate1-rotate[ ```r palmerpenguins::penguins %>% ggplot(aes(body_mass_g, flipper_length_mm, col = island)) + geom_point() + * theme_dark() ``` ] .panel2-my_rotate1-rotate[ ![](data:image/png;base64,#05-themes_files/figure-html/my_rotate1_rotate_02_output-1.png)<!-- --> ] --- count: false .panel1-my_rotate1-rotate[ ```r palmerpenguins::penguins %>% ggplot(aes(body_mass_g, flipper_length_mm, col = island)) + geom_point() + * theme_excel() ``` ] .panel2-my_rotate1-rotate[ ![](data:image/png;base64,#05-themes_files/figure-html/my_rotate1_rotate_03_output-1.png)<!-- --> ] --- count: false .panel1-my_rotate1-rotate[ ```r palmerpenguins::penguins %>% ggplot(aes(body_mass_g, flipper_length_mm, col = island)) + geom_point() + * theme_economist() ``` ] .panel2-my_rotate1-rotate[ ![](data:image/png;base64,#05-themes_files/figure-html/my_rotate1_rotate_04_output-1.png)<!-- --> ] --- count: false .panel1-my_rotate1-rotate[ ```r palmerpenguins::penguins %>% ggplot(aes(body_mass_g, flipper_length_mm, col = island)) + geom_point() + * theme_fivethirtyeight() ``` ] .panel2-my_rotate1-rotate[ ![](data:image/png;base64,#05-themes_files/figure-html/my_rotate1_rotate_05_output-1.png)<!-- --> ] --- count: false .panel1-my_rotate1-rotate[ ```r palmerpenguins::penguins %>% ggplot(aes(body_mass_g, flipper_length_mm, col = island)) + geom_point() + * theme_wsj() ``` ] .panel2-my_rotate1-rotate[ ![](data:image/png;base64,#05-themes_files/figure-html/my_rotate1_rotate_06_output-1.png)<!-- --> ] <style> .panel1-my_rotate1-rotate { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-my_rotate1-rotate { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-my_rotate1-rotate { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ## Complete Themes - other package provide supplementary themes - *ggthemes* - *ggthemr* (see [here](https://github.com/cttobin/ggthemr)) - *tvthemes* (see [here](https://ryo-n7.github.io/2019-05-16-introducing-tvthemes-package/)) - *hrbrthemes* see [here](https://github.com/hrbrmstr/hrbrthemes) - *firatheme* (see [here](https://github.com/vankesteren/firatheme)) - *bbplot*, themes from the BBC (see [here](https://github.com/bbc/bbplot)) - *ggtech* themes from companies, e.g. Facebook. See [here](https://github.com/ricardo-bion/ggtech) - [This website](https://rfortherestofus.com/2019/08/themes-to-improve-your-ggplot-figures/) is pretty good on themes - again, make sure you experiment with these - have to mention [thematic](remotes::install_github("rstudio/thematic")) here, simplifies adding google fonts too. --- ## thematic example .pull-left[ ```r thematic_on(bg = "#111111", fg = "white", accent = "#0CE3AC", font = "Permanent Marker") gapminder::gapminder %>% dplyr::filter(year == "2007", continent == "Europe") %>% ggplot(aes(lifeExp, gdpPercap, label = country, colour = pop)) + geom_point(show.legend = FALSE) + ggrepel::geom_text_repel(show.legend = FALSE) *thematic_off() ``` ] .pull-right[ ![](data:image/png;base64,#05-themes_files/figure-html/thematic-example-out-1.png)<!-- --> ] --- ## Fine Control Over Themes .panelset[ .panel[ .panel-name[Themes] .pull-left[ - we can change any feature of a theme that we want - type *?theme* on the console to see available themes - there are a lot of themes - use *element_&&&&&* to replace theme - example: *theme(text = element_text(family = "Roboto Sans"))* ] .pull-right[ ![](data:image/png;base64,#img/theme.gif) ] ] <!-- close first panel --> .panel[ .panel-name[Change Font] .pull-left[ ```r font_add("Cabin Sketch", regular = "fonts/CabinSketch-Regular.ttf") showtext_auto() palmerpenguins::penguins %>% ggplot(aes(body_mass_g, flipper_length_mm, col = island)) + geom_point() + labs(x = "Body mass(g)", y = "Flipper Length (mm)") + theme(text = element_text(family = "Cabin Sketch", size = 24, face = "bold")) ``` ] .pull-right[ ![](data:image/png;base64,#05-themes_files/figure-html/themes3-out-1.png)<!-- --> ] ] <!-- close second panel --> .panel[ .panel-name[Change Legend] .pull-left[ ```r palmerpenguins::penguins %>% ggplot(aes(body_mass_g, flipper_length_mm, col = island)) + geom_point() + labs(x = "Body mass(g)", y = "Flipper Length (mm)") + theme(legend.position = "bottom", axis.text.y = element_blank()) ``` ] .pull-right[ ![](data:image/png;base64,#05-themes_files/figure-html/themes4-out-1.png)<!-- --> ] ] <!-- close third panel --> ] <!-- close panelset --> --- - *theme_set()*, *theme_get()*, and *theme_update()* pretty useful .pull-left[ ```r font_add("Fiendish", "fonts/Fiendish.ttf") showtext_auto() theme_update(text = element_text(family = "Fiendish", size = 18, colour = "#691740", face = "bold")) palmerpenguins::penguins %>% ggplot(aes(body_mass_g, flipper_length_mm, col = island)) + geom_point() ``` ] .pull-right[ ![](data:image/png;base64,#05-themes_files/figure-html/themes5-out-1.png)<!-- --> ] --- .pull-left[ ```r font_add("Get Schwifty", regular = "fonts/get_schwifty.ttf") showtext_auto() theme_bluewhite <- function (base_size = 11, base_family = "Get Schwifty") { theme_bw() %+replace% theme( text = element_text(size = base_size, family = base_family), panel.grid.major = element_line(color = "white"), panel.background = element_rect(fill = "lightblue"), panel.border = element_rect(color = "lightblue", fill = NA), axis.line = element_line(color = "lightblue"), axis.ticks = element_line(color = "lightblue"), axis.text = element_text(color = "steelblue") ) } # Using our new theme ggplot(ToothGrowth, aes(factor(dose), len)) + geom_boxplot() + theme_bluewhite(base_size = 24) + labs(x = "Supplement Dose (mg/day)", y = "Tooth Length (mm)") ``` ] .pull-right[ ![](data:image/png;base64,#05-themes_files/figure-html/teeth-out-1.png)<!-- --> ] --- class: center, inverse # _scales_ - I like the scales package and feel it's worth discussing - Includes features like dollar signs, commas, and scientific notation - (also, has the _show_col()_ function we used earlier) - can be used to change to scientific notation, or not - utilities like _comma()_, _dollar()_, _label_number_si()_, _percent()_, _pretty_breaks()_ - [this is a nice overview](https://bookdown.org/Maxine/ggplot2-maps/posts/2019-11-27-using-scales-package-to-modify-ggplot2-scale/) --- count: false .panel1-gapminder_plot-user[ ```r *theme_update(legend.position = "none") *gapminder::gapminder %>% * filter(continent == "Americas") ``` ] .panel2-gapminder_plot-user[ ``` ## # A tibble: 300 x 6 ## country continent year lifeExp pop gdpPercap ## <fct> <fct> <int> <dbl> <int> <dbl> ## 1 Argentina Americas 1952 62.5 17876956 5911. ## 2 Argentina Americas 1957 64.4 19610538 6857. ## 3 Argentina Americas 1962 65.1 21283783 7133. ## 4 Argentina Americas 1967 65.6 22934225 8053. ## 5 Argentina Americas 1972 67.1 24779799 9443. ## 6 Argentina Americas 1977 68.5 26983828 10079. ## 7 Argentina Americas 1982 69.9 29341374 8998. ## 8 Argentina Americas 1987 70.8 31620918 9140. ## 9 Argentina Americas 1992 71.9 33958947 9308. ## 10 Argentina Americas 1997 73.3 36203463 10967. ## # ... with 290 more rows ``` ] --- count: false .panel1-gapminder_plot-user[ ```r theme_update(legend.position = "none") gapminder::gapminder %>% filter(continent == "Americas") %>% * ggplot(aes(gdpPercap, pop)) + * geom_line(aes(col = country)) ``` ] .panel2-gapminder_plot-user[ ![](data:image/png;base64,#05-themes_files/figure-html/gapminder_plot_user_02_output-1.png)<!-- --> ] --- count: false .panel1-gapminder_plot-user[ ```r theme_update(legend.position = "none") gapminder::gapminder %>% filter(continent == "Americas") %>% ggplot(aes(gdpPercap, pop)) + geom_line(aes(col = country)) + * scale_y_log10(labels = label_number_si(unit = " people")) ``` ] .panel2-gapminder_plot-user[ ![](data:image/png;base64,#05-themes_files/figure-html/gapminder_plot_user_03_output-1.png)<!-- --> ] --- count: false .panel1-gapminder_plot-user[ ```r theme_update(legend.position = "none") gapminder::gapminder %>% filter(continent == "Americas") %>% ggplot(aes(gdpPercap, pop)) + geom_line(aes(col = country)) + scale_y_log10(labels = label_number_si(unit = " people")) + * scale_x_log10(labels = dollar) ``` ] .panel2-gapminder_plot-user[ ![](data:image/png;base64,#05-themes_files/figure-html/gapminder_plot_user_04_output-1.png)<!-- --> ] --- count: false .panel1-gapminder_plot-user[ ```r theme_update(legend.position = "none") gapminder::gapminder %>% filter(continent == "Americas") %>% ggplot(aes(gdpPercap, pop)) + geom_line(aes(col = country)) + scale_y_log10(labels = label_number_si(unit = " people")) + scale_x_log10(labels = dollar) + * theme_minimal() ``` ] .panel2-gapminder_plot-user[ ![](data:image/png;base64,#05-themes_files/figure-html/gapminder_plot_user_05_output-1.png)<!-- --> ] --- count: false .panel1-gapminder_plot-user[ ```r theme_update(legend.position = "none") gapminder::gapminder %>% filter(continent == "Americas") %>% ggplot(aes(gdpPercap, pop)) + geom_line(aes(col = country)) + scale_y_log10(labels = label_number_si(unit = " people")) + scale_x_log10(labels = dollar) + theme_minimal() + * theme(text = element_text(size = 16), * axis.title.y = element_blank(), * legend.position = "bottom", * legend.title = element_blank()) ``` ] .panel2-gapminder_plot-user[ ![](data:image/png;base64,#05-themes_files/figure-html/gapminder_plot_user_06_output-1.png)<!-- --> ] <style> .panel1-gapminder_plot-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-gapminder_plot-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-gapminder_plot-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> ---
---