class: center, middle, inverse, title-slide .title[ # Taking your plot to the next level - themes, axes, and annotation ] .subtitle[ ##
College of the Atlantic ] .author[ ### Laurie Baker ] --- --- # Recap: How do we express visuals in words? - **Data** to be visualized -- - **`Aes`thetic mappings** from data to visual component -- - **`Geom`etric objects** that appear on the plot -- - **`Stat`istics** transform data on the way to visualization -- - **`Coord`inates** organize location of geometric objects -- - **`Scale`s** define the range of values for aesthetics -- - **`Facet`s** group into subplots -- - **`Theme`s** the visual elements of the plot not linked to the data --- # Today's Focus - **Theme**s: the visual elements of the plot not linked to the data - **Geom**etric objects: `geom_vline` and `geom_hline` - **Scale**s: define the range of values for aesthetics - **Annotations:** text, highlight, etc. --- ## Packages ```r library(tidyverse) library(ggthemes) library(gapminder) ``` --- # head(gapminder) ``` ## # A tibble: 6 × 6 ## country continent year lifeExp pop gdpPercap ## <fct> <fct> <int> <dbl> <int> <dbl> ## 1 Afghanistan Asia 1952 28.8 8425333 779. ## 2 Afghanistan Asia 1957 30.3 9240934 821. ## 3 Afghanistan Asia 1962 32.0 10267083 853. ## 4 Afghanistan Asia 1967 34.0 11537966 836. ## 5 Afghanistan Asia 1972 36.1 13079460 740. ## 6 Afghanistan Asia 1977 38.4 14880372 786. ``` --- # glimpse(gapminder) ``` Rows: 1,704 Columns: 6 $ country <fct> "Afghanistan", "Afghanistan", "Afghanistan", … $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asi… $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 198… $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.43… $ pop <int> 8425333, 9240934, 10267083, 11537966, 1307946… $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9… ``` --- # Life Expectancy .panelset[ <div class="panel"> <div class="panel-name">Code</div> ```r (p1 <- gapminder |> filter(continent == "Asia") |> ggplot(aes(x = year, y = lifeExp, group = country)) + geom_line() + labs(x = "Year", y = "Life Expectancy (years)", title = "Life Expectancy in Asia")) ``` </div> <div class="panel"> <div class="panel-name">Plot</div> <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-3-1.png" width="60%" style="display: block; margin: auto;" /> </div> ] --- # themes - black and white ```r p1 + theme_bw() ``` <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-4-1.png" width="60%" style="display: block; margin: auto;" /> --- # themes - dark ```r p1 + theme_dark() ``` <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-5-1.png" width="60%" style="display: block; margin: auto;" /> --- # themes - minimal ```r p1 + theme_minimal() ``` <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-6-1.png" width="60%" style="display: block; margin: auto;" /> --- # themes - build your own .right-column[ Huge number of parameters, grouped by plot area: - Global options: `line`, `rect`, `text`, `title` - `axis`: x-, y- or other axis title, ticks, lines - `legend`: Plot legends - `panel`: Actual plot area - `plot`: Whole image - `strip`: Facet labels ] --- # themes - build your own .right-column[ Theme options are supported by helper functions: - `element_blank()` removes the element - `element_line()` - `element_rect()` - `element_text()` ```r my_theme <- theme_bw() + theme(text = element_text(color = "hotpink", size = 20), axis.ticks.x = element_blank()) ``` ``` ] --- .right-column[ ```r p1 + my_theme ``` <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-8-1.png" width="90%" style="display: block; margin: auto;" /> ] --- # ggthemes - Economist ```r p1 + theme_economist() ``` <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-9-1.png" width="60%" style="display: block; margin: auto;" /> --- # Life Expectancy - highlight .panelset[ <div class="panel"> <div class="panel-name">Code</div> ```r (p1 <- p1 + geom_line(data = gapminder |> filter(country == "Cambodia"), color = "orange", lwd = 2, aes(x = year, y = lifeExp, group = country)) + theme_bw()) ``` </div> <div class="panel"> <div class="panel-name">Plot</div> <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-10-1.png" width="60%" style="display: block; margin: auto;" /> </div> ] - [Cambodian Genocide](https://sfi.usc.edu/collections/cambodian-genocide) --- # Add annotations with `geom_vline` .panelset[ <div class="panel"> <div class="panel-name">Code</div> ```r (p1 <- p1 + geom_vline(xintercept = 1975, linetype = "dotted", color = "red") + geom_vline(xintercept = 1979, linetype = "dotted", color = "red")) ``` </div> <div class="panel"> <div class="panel-name">Plot</div> <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-11-1.png" width="60%" style="display: block; margin: auto;" /> </div> ] --- # Add annotations with `annotate` .panelset[ <div class="panel"> <div class="panel-name">Code</div> ```r p1 + annotate("rect", xmin = 1975, xmax = 1979, ymin = 0, ymax = 100, alpha = 0.2, fill = "red") ``` </div> <div class="panel"> <div class="panel-name">Plot</div> <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-12-1.png" width="60%" style="display: block; margin: auto;" /> </div> ] --- # Scales .left-column[ ```r + scale_*_*() ``` ] .right-column[ `scale` + `_` + `<aes>` + `_` + `<type>` + `()` `<aes>` = parameter to adjust; `<type>` = Parameter Type ] --- # Rescale y-axis as log `scale_y_log10()` .panelset[ <div class="panel"> <div class="panel-name">Code</div> ```r p1 + annotate("rect", xmin = 1975, xmax = 1979, ymin = 0, ymax = 100, alpha = 0.2, fill = "red") + scale_y_log10() ``` </div> <div class="panel"> <div class="panel-name">Plot</div> ``` ## Warning: Transformation introduced infinite values in continuous ## y-axis ``` <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-13-1.png" width="60%" style="display: block; margin: auto;" /> </div> ] --- # Change x-axis `scale_x_continuous()` .panelset[ <div class="panel"> <div class="panel-name">Code</div> ```r p1 + annotate("rect", xmin = 1975, xmax = 1979, ymin = 0, ymax = 100, alpha = 0.2, fill = "red") + scale_x_continuous(breaks = seq(from = 1952, to = 2007, by = 5)) ``` </div> <div class="panel"> <div class="panel-name">Plot</div> <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-14-1.png" width="60%" style="display: block; margin: auto;" /> </div> ] --- # Annotation - geom_text .panelset[ <div class="panel"> <div class="panel-name">Code</div> ```r p1 + annotate("rect", xmin = 1975, xmax = 1979, ymin = 0, ymax = 100, alpha = 0.2, fill = "red") + geom_text(aes(x = 1985, y = 25), label = "Khmer Rouge") ``` </div> <div class="panel"> <div class="panel-name">Plot</div> <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-15-1.png" width="60%" style="display: block; margin: auto;" /> </div> ] --- # Annotation - geom_text .panelset[ <div class="panel"> <div class="panel-name">Code</div> ```r (p2 <- gapminder |> filter(country == "Portugal") |> ggplot(aes(x = year, y = lifeExp)) + geom_point() + labs(x = "Year", y = "Life Expectancy (years)")) + geom_text(aes(label = year)) ``` </div> <div class="panel"> <div class="panel-name">Plot</div> <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-16-1.png" width="60%" style="display: block; margin: auto;" /> </div> ] --- # Annotation - geom_text .panelset[ <div class="panel"> <div class="panel-name">Code</div> ```r (p2 <- gapminder |> filter(country == "Portugal") |> ggplot(aes(x = year, y = lifeExp)) + geom_point() + labs(x = "Year", y = "Life Expectancy (years)", title = "Life Expectancy in Portugal over time") + geom_text(aes(label = year), nudge_x = 3.75, nudge_y = -0.3)) ``` </div> <div class="panel"> <div class="panel-name">Plot</div> <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-17-1.png" width="60%" style="display: block; margin: auto;" /> </div> ] --- # Annotation - gghighlight .panelset[ <div class="panel"> <div class="panel-name">Code</div> ```r gapminder |> ggplot(aes(x = year, y = lifeExp, group = country, color = continent)) + geom_line() + labs(x = "Year", y = "Life Expectancy (years)") + gghighlight(mean(lifeExp) > 75) ``` </div> <div class="panel"> <div class="panel-name">Plot</div> <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-18-1.png" width="60%" style="display: block; margin: auto;" /> </div> ] --- # Annotation - gghighlight .panelset[ <div class="panel"> <div class="panel-name">Code</div> ```r gapminder |> ggplot(aes(x = year, y = lifeExp, group = country, color = continent)) + geom_line() + labs(x = "Year", y = "Life Expectancy (years)") + facet_wrap(~continent) + gghighlight(mean(lifeExp) > 74.5) ``` </div> <div class="panel"> <div class="panel-name">Plot</div> <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-19-1.png" width="60%" style="display: block; margin: auto;" /> </div> ] --- # Annotation - gghighlight .panelset[ <div class="panel"> <div class="panel-name">Code</div> ```r gapminder |> ggplot(aes(x = year, y = lifeExp, group = country, color = continent)) + geom_line() + labs(x = "Year", y = "Life Expectancy (years)") + gghighlight(min(lifeExp) < 40 & max(lifeExp) > 70) ``` </div> <div class="panel"> <div class="panel-name">Plot</div> <img src="u5-d10-axes-themes_files/figure-html/unnamed-chunk-20-1.png" width="60%" style="display: block; margin: auto;" /> </div> ] --- # Your Turn - Create your own plot highlighting a county of interest and a labelled historical event.