class: center, middle, inverse, title-slide .title[ # Deep dive: themes + axes + annotation ] .author[ ### MACS 40700
University of Chicago ] --- class: inverse, middle <style type="text/css"> .task{ background-color: #9eafd9 } </style> # Announcements: For next class: you will need an API key: https://docs.stadiamaps.com/tutorials/getting-started-in-r-with-ggmap/#set-up-api-key-authentication --- # Agenda * Themes * Axes * Walk-through of in-depth example --- ``` r # Setup: load packages library(tidyverse) library(palmerpenguins) library(ggthemes) library(ggpomological) library(colorspace) library(lubridate) #library(hrbrthemes) library(WDI) library(ggrepel) library(ggtext) # set default theme for ggplot2 ggplot2::theme_set(ggplot2::theme_gray(base_size = 16)) # set default figure parameters for knitr knitr::opts_chunk$set( fig.width = 8, fig.asp = 0.618, fig.retina = 2, dpi = 150 ) # dplyr print min and max options(dplyr.print_max = 6, dplyr.print_min = 6) ``` --- class: middle, inverse # Themes --- # Should you stick with defaults? .pull-left[ <img src="index_files/figure-html/standard-grey-1.png" width="95%" style="display: block; margin: auto;" /> Some say these are good enough. Others want more thought. ] -- .pull-right[ <img src="https://media1.tenor.com/m/rr9KpqBthKQAAAAC/its-me-hi-taylor-swift.gif" width="85%" style="display: block; margin: auto;" /> ] --- # Or should you pursue alternatives -- like this! .pull-left[ ``` r library(ggthemes) ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species)) + geom_point(size = 2) + * theme_tufte() ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-2-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # And this! .pull-left[ ``` r library(ggthemes) ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species)) + geom_point(size = 2) + * theme_solarized() ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-3-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Or this! .pull-left[ ``` r library(ggthemes) ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species)) + geom_point(size = 2) + * scale_color_economist() + * theme_economist() ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-4-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # And even this! .pull-left[ ``` r library(ggpomological) library(showtext) font_add_google("Homemade Apple") showtext_auto() #load fonts ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species)) + geom_point(size = 2) + * scale_color_pomological() + * theme_pomological_fancy() ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-5-1.png" width="100%" style="display: block; margin: auto;" /> ] --- class: inverse, middle ## One magic, powerful function -- .huge[`theme()`] --- class: inverse, middle # The anatomy of a `ggplot()` theme --- # Theme system <img src="images/theme_elements-1024x755.png" width="80%" style="display: block; margin: auto;" /> .footnote[Source: [`ggplot2` theme elements demonstration](https://henrywang.nl/ggplot2-theme-elements-demonstration/)] --- # Theme elements ### Each element in the plot can be targeted -- - Plot title = `plot.title` -- - Grid lines = `panel.grid` -- - Legend background = `legend.background` --- # Theme functions ### Use special functions to manipulate specific elements -- - Text-based things = `element_text()` -- - Rectangular things (backgrounds) = `element_rect()` -- - Line-based things (axis lines, grid lines) = `element_line()` -- - Disable element completely = `element_blank()` --- # How to learn `theme()` - The `theme()` function has **94** possible arguments - You can get hyper-specific with things like `axis.ticks.length.x.bottom` - The only way to learn how to use `theme()` is to use it and tinker with it --- class: inverse, middle # .hand[Live coding] ```r # install if necessary install.packages(c("tidyverse", "here", "colorspace", "showtext", "scales", "ggrepel", "ggtext", "usethis")) usethis::use_course("MACS40700/themes-annotations") ``` # Note: might need to install ggtext (and others) --- class: inverse, middle # Axes --- ## Axis breaks .task[ How can the following figure be improved with custom breaks in axes, if at all? ] <img src="index_files/figure-html/unnamed-chunk-7-1.png" width="60%" style="display: block; margin: auto;" /> --- ## Context matters ``` r m_plot_imp <- m_plot + scale_y_discrete(label=abbreviate(midnights$track_name, minlength=10 ),limits=rev) + * scale_x_continuous(breaks = seq(from = 0, to = 200, by = 10)) m_plot_imp ``` <img src="index_files/figure-html/unnamed-chunk-8-1.png" width="55%" style="display: block; margin: auto;" /> --- ## Conciseness matters ``` r ggplot(midnights, aes(x = tempo, y = track_name, fill = track_name)) + geom_col(show.legend = FALSE) + expand_limits(x = c(0, 1)) + labs(y = NULL) + theme_minimal() + scale_fill_taylor_d(album = "Midnights") ``` <img src="index_files/figure-html/unnamed-chunk-9-1.png" width="55%" style="display: block; margin: auto;" /> ``` r scale_x_continuous(breaks = seq(0, 200, 50)) ``` ``` ## <ScaleContinuousPosition> ## Range: ## Limits: 0 -- 1 ``` --- ## Precision matters ``` r m_plot_imp + scale_x_continuous(breaks = seq(0, 200, 25)) + * labs(x = "Tempo (bpm)") ``` <img src="index_files/figure-html/unnamed-chunk-10-1.png" width="60%" style="display: block; margin: auto;" /> --- # Zooming: comparison .panelset[.panel[ .panel-name[Baseline] ``` r ggplot( midnights, aes(x = tempo, color = tempo)) + coord_flip() + geom_boxplot(show.legend = FALSE) + labs(x = NULL) + theme_minimal() + scale_color_pomological() + theme_pomological_fancy() + scale_x_continuous(breaks = seq(0, 200, 10)) ``` <img src="index_files/figure-html/unnamed-chunk-11-1.png" width="45%" style="display: block; margin: auto;" /> ] .panel[ .panel-name[Opt 1: subset] ``` r midnights %>% filter(tempo > 125) %>% ggplot( aes(x = tempo, color = tempo)) + geom_boxplot(show.legend = FALSE) + labs(x = NULL) + theme_minimal() + scale_color_pomological() + theme_pomological_fancy() + scale_x_continuous(breaks = seq(0, 200, 10)) + coord_flip() ``` <img src="index_files/figure-html/unnamed-chunk-12-1.png" width="45%" style="display: block; margin: auto;" /> ] .panel[.panel-name[Opt 2: limits] ``` r ggplot( midnights, aes(x = tempo, color = tempo)) + geom_boxplot(show.legend = FALSE) + labs(x = NULL) + theme_minimal() + scale_color_pomological() + theme_pomological_fancy() + xlim(125, 200) + scale_x_continuous(breaks = seq(125, 200, 10))+ coord_flip() ``` <img src="index_files/figure-html/unnamed-chunk-13-1.png" width="45%" style="display: block; margin: auto;" /> ] ] --- class: inverse, middle # Fretting the little things --- # Little details matter .pull-left[ <img src="images/stevejobs.jpg" width="80%" style="display: block; margin: auto;" /> ] -- .pull-right[ <img src="images/apple_iie.jpg" width="80%" style="display: block; margin: auto;" /> ] ??? https://commons.wikimedia.org/wiki/File:Steve_Jobs_Headshot_2010-CROP.jpg https://en.wikipedia.org/wiki/Apple_IIe#/media/File:Iie-system.jpg --- ``` r ggplot(midnights, aes(x = tempo, y = reorder(track_name, tempo, sum))) + geom_col(show.legend = FALSE, fill = 'lightgray') + expand_limits(x = c(0, 1)) + labs(y = NULL) + theme_minimal() ``` <img src="index_files/figure-html/taylor-plot-1.png" width="75%" style="display: block; margin: auto;" /> --- # Human-focused design .large[ > "This is what customers pay us for -- to sweat all these details so it's easy and pleasant for them to use our computers." ] ??? Steve Jobs in *Fortune*, as quoted in [Alice Rawsthorn, *Hello World: Where Design Meets Life*, p. 110(?)](https://www.google.com/books/edition/Hello_World/b6iMDwAAQBAJ?hl=en&gbpv=1&dq=to+sweat+all+these+details+so+it%E2%80%99s+easy+and+pleasant+for+them+to+use+our+computers&pg=PT110&printsec=frontcover) --- # Graph details: Color coding .pull-left[ <img src="index_files/figure-html/unnamed-chunk-17-1.png" width="100%" style="display: block; margin: auto;" /> ] -- .pull-right[ <img src="index_files/figure-html/unnamed-chunk-18-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Graph details: Consistent ordering .pull-left[ <img src="index_files/figure-html/unnamed-chunk-19-1.png" width="100%" style="display: block; margin: auto;" /> ] -- .pull-right[ <img src="index_files/figure-html/unnamed-chunk-20-1.png" width="100%" style="display: block; margin: auto;" /> ] ??? Source: Claus Wilke: https://clauswilke.com/dataviz/redundant-coding.html --- # Details matter .large[Worrying about tiny details in graphs…] -- .midi[…makes them easier for your audience to understand] -- .midi[…improves their beauty] -- .midi[…enhances the truth] --- class: inverse, middle # Text in plots --- # Including text on a plot ## Label actual data points `geom_text()`, `geom_label()`, `geom_text_repel()`, etc. -- ## Add arbitrary annotations `annotate()` --- # Label actual data points .pull-left[ ``` r library(gapminder) gapminder_europe <- gapminder %>% filter(year == 2007, continent == "Europe") ggplot(gapminder_europe, aes(x = gdpPercap, y = lifeExp)) + geom_point() + * geom_text(aes(label = country)) ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-21-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Label actual data points .pull-left[ ``` r ggplot(gapminder_europe, aes(x = gdpPercap, y = lifeExp)) + geom_point() + * geom_label(aes(label = country)) ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-22-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Solution 1: Repel labels .pull-left[ ``` r *library(ggrepel) ggplot(gapminder_europe, aes(x = gdpPercap, y = lifeExp)) + geom_point() + * geom_text_repel(aes(label = country)) ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-23-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Solution 1: Repel labels .pull-left[ ``` r library(ggrepel) ggplot(gapminder_europe, aes(x = gdpPercap, y = lifeExp)) + geom_point() + * geom_label_repel(aes(label = country)) ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-24-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Solution 2a: Don't use so many labels .pull-left[ .small[ ``` r gapminder_europe <- gapminder_europe %>% mutate(should_be_labeled = ifelse(country %in% c("Albania", "Norway", "Hungary"), TRUE, FALSE)) ggplot(gapminder_europe, aes(x = gdpPercap, y = lifeExp)) + geom_point() + geom_label_repel( * data = filter(gapminder_europe, * should_be_labeled == TRUE), aes(label = country) ) ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-25-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Solution 2b: Use other aesthetics too .pull-left[ ``` r ggplot(gapminder_europe, aes(x = gdpPercap, y = lifeExp)) + geom_point(aes(color = should_be_labeled)) + geom_label_repel( data = filter( gapminder_europe, should_be_labeled == TRUE ), aes(label = country, fill = should_be_labeled), color = "white" ) + scale_color_manual(values = c("grey50", "red")) + scale_fill_manual(values = c("red")) + guides(color = "none", fill = "none") ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-26-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # (Highlight non-text things too!) .pull-left[ ``` r # Color just Oceania gapminder_highlighted <- gapminder %>% mutate(is_oceania = ifelse(continent == "Oceania", TRUE, FALSE)) ggplot(gapminder_highlighted, aes(x = year, y = lifeExp, group = country, color = is_oceania, size = is_oceania)) + geom_line() + scale_color_manual(values = c("grey70", "red")) + scale_size_manual(values = c(0.1, 0.5)) + guides(color = "none", size = "none") + theme_minimal() ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-27-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Including text on a plot ## Label actual data points `geom_text()`, `geom_label()`, `geom_text_repel()`, etc. -- ## Add arbitrary annotations `annotate()` --- # Adding arbitrary annotations .pull-left[ ``` r ggplot(gapminder_europe, aes(x = gdpPercap, y = lifeExp)) + geom_point() + * annotate(geom = "text", * x = 40000, y = 76, * label = "Some text!") ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-28-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Adding arbitrary annotations .pull-left[ ``` r ggplot(gapminder_europe, aes(x = gdpPercap, y = lifeExp)) + geom_point() + * annotate(geom = "label", x = 40000, y = 76, label = "Some text!") ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-29-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Any geom works .pull-left[ ``` r ggplot(gapminder_europe, aes(x = gdpPercap, y = lifeExp)) + geom_point() + * # This is evil though!!! * # We just invented a point * annotate(geom = "point", * x = 40000, y = 76, * color = "red3") ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-30-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Any geom works .pull-left[ ``` r ggplot(gapminder_europe, aes(x = gdpPercap, y = lifeExp)) + geom_point() + * annotate(geom = "rect", * xmin = 30000, xmax = 50000, * ymin = 78, ymax = 82, * fill = "red", alpha = 0.2) ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-31-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Use multiple annotations .pull-left[ ``` r ggplot(gapminder_europe, aes(x = gdpPercap, y = lifeExp)) + geom_point() + annotate(geom = "rect", xmin = 30000, xmax = 50000, ymin = 78, ymax = 82, fill = "red", alpha = 0.2) + * annotate(geom = "label", * x = 40000, y = 76.5, * label = "Rich and long-living") + * annotate(geom = "segment", * x = 40000, xend = 40000, * y = 76.8, yend = 77.8, * arrow = arrow( * length = unit(0.1, "in"))) ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-32-1.png" width="100%" style="display: block; margin: auto;" /> ] --- class: inverse, middle # Complex annotations --- ## World development indicators ``` r #wdi_co2_raw <- read_csv(here("data", "wdi_co2.csv")) indicators <- c( "SP.POP.TOTL", # Population "EN.GHG.CO2.MT.CE.AR5", # CO2 emissions "NY.GDP.PCAP.KD") # GDP per capita wdi_co2_raw <- WDI::WDI( country = "all", indicators, extra = TRUE, start = 1995, end = 2015) ``` ``` r wdi_clean <- wdi_co2_raw %>% filter(region != "Aggregates") %>% select(iso2c, iso3c, country, year, population = SP.POP.TOTL, co2_emissions = EN.GHG.CO2.MT.CE.AR5, gdp_per_cap = NY.GDP.PCAP.KD, region, income ) ``` --- ## Clean and reshape data .tiny[ ``` r co2_rankings <- wdi_clean %>% # Get rid of smaller countries filter(population > 200000) %>% # Only look at two years filter(year %in% c(1995, 2014)) %>% # Get rid of all the rows that have missing values in co2_emissions drop_na(co2_emissions) %>% # Look at each year individually and rank countries based on their emissions that year group_by(year) %>% mutate(ranking = rank(co2_emissions)) %>% ungroup() %>% # Only select required columns select(iso3c, country, year, region, income, ranking) %>% pivot_wider(names_from = year, names_prefix = "rank_", values_from = ranking) %>% # Find the difference in ranking between 2014 and 1995 mutate(rank_diff = rank_2014 - rank_1995) %>% # Remove all rows where there's a missing value in the rank_diff column drop_na(rank_diff) ``` ] --- ## Clean and reshape data .tiny[ ``` r co2_rankings <- co2_rankings %>% # Make an indicator variable that is true of the absolute value of the # difference in rankings is greater than 25 mutate(big_change = ifelse(abs(rank_diff) >= 25, TRUE, FALSE)) %>% # Make another indicator variable that indicates if the rank improved by a # lot, worsened by a lot, or didn't change much. mutate(better_big_change = case_when( rank_diff <= -25 ~ "Rank improved", rank_diff >= 25 ~ "Rank worsened", TRUE ~ "Rank changed a little" )) ``` ] --- .pull-left[ ``` r head(wdi_clean) ``` ``` ## iso2c iso3c country year population co2_emissions gdp_per_cap region ## 1 AF AFG Afghanistan 1999 19887785 1.3633 NA South Asia ## 2 AF AFG Afghanistan 2001 20284307 0.9402 277.1181 South Asia ## 3 AF AFG Afghanistan 2014 32792523 7.9588 575.1462 South Asia ## 4 AF AFG Afghanistan 1998 19159996 1.4093 NA South Asia ## 5 AF AFG Afghanistan 1997 18452091 1.3735 NA South Asia ## 6 AF AFG Afghanistan 2000 20130327 1.0093 308.3183 South Asia ## income ## 1 Low income ## 2 Low income ## 3 Low income ## 4 Low income ## 5 Low income ## 6 Low income ``` ] .pull-right[ ``` r head(co2_rankings) ``` ``` ## # A tibble: 6 × 9 ## iso3c country region income rank_2014 rank_1995 rank_diff big_change ## <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <lgl> ## 1 AFG Afghanistan South Asia Low i… 66 38 28 TRUE ## 2 ALB Albania Europe & Ce… Upper… 48 45 3 FALSE ## 3 DZA Algeria Middle East… Upper… 142 134 8 FALSE ## 4 AGO Angola Sub-Saharan… Lower… 101 90 11 FALSE ## 5 ARG Argentina Latin Ameri… Upper… 146 143 3 FALSE ## 6 ARM Armenia Europe & Ce… Upper… 54 61 -7 FALSE ## # ℹ 1 more variable: better_big_change <chr> ``` ] --- class: inverse, middle ## Plot the data and annotate --- .task[ .tiny[ Set the random seed for reproducibility. ] ] .pull-left[ .tiny[ ``` r *set.seed(13) ``` ] ] .pull-right[ ] --- .task[ .tiny[ Initialize the `ggplot()` object. ] ] .pull-left[ .tiny[ ``` r set.seed(13) *ggplot( * data = co2_rankings, * mapping = aes(x = rank_1995, * y = rank_2014)) ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-35-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .task[ .tiny[ Create a basic scatterplot, color-coded based on rank changes. ] ] .pull-left[ .tiny[ ``` r set.seed(13) ggplot( data = co2_rankings, mapping = aes(x = rank_1995, y = rank_2014)) + * geom_point(aes( * color = better_big_change)) ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-36-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .task[ .tiny[ Label points for countries with a "big" change. ] ] .pull-left[ .tiny[ ``` r set.seed(13) ggplot( data = co2_rankings, mapping = aes(x = rank_1995, y = rank_2014)) + geom_point(aes(color = better_big_change)) + * geom_label_repel( * data = filter(co2_rankings, big_change == TRUE), * aes(label = country, fill = better_big_change), * color = "white", family = "bell") ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-37-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .task[ .tiny[ Add a reference line in the background to show what it would look like if countries did not change rank order. Note `xend` and `yend` differ due to new countries forming during the 20 year period. ] ] .pull-left[ .tiny[ ``` r set.seed(13) base <- ggplot( data = co2_rankings, mapping = aes(x = rank_1995, y = rank_2014) ) + * annotate(geom = "segment", x = 0, * xend = 172, y = 0, yend = 178) + geom_point(aes(color = better_big_change)) + geom_label_repel( data = filter(co2_rankings, big_change == TRUE), aes(label = country, fill = better_big_change), color = "white", family = "comic neue") base ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-38-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .task[ .tiny[ Annotate the plot to clarify outliers that are improving. ] ] .pull-left[ .tiny[ ``` r set.seed(13) base + * annotate( * geom = "text", x = 170, y = 6, * label = "Outliers improving", * family = "comic neue", * fontface = "italic", * hjust = 1, * color = "grey50" ) ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-39-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .task[ .tiny[ Annotate the plot to clarify the outliers that are worsening. ] ] .pull-left[ .tiny[ ``` r set.seed(13) base2 <-base + annotate( geom = "text", x = 170, y = 6, label = "Outliers improving", family = "comic neue", fontface = "italic", hjust = 1, color = "grey50" ) + * annotate( * geom = "text", x = 2, y = 170, label = "Outliers worsening", * family = "comic neue", fontface = "italic", hjust = 0, color = "grey50" ) base2 ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-40-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .task[ .tiny[ Choose a custom color palette to highlight the outliers. ] ] .pull-left[ .tiny[ ``` r set.seed(13) base2 + * scale_color_manual( * values = c("grey50", * "#0074D9", "#FF4136")) ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-41-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .task[ .tiny[ Use the same colors for the labels. Only need the last two values in the original palette. ] ] .pull-left[ .tiny[ ``` r set.seed(13) base2 + scale_color_manual( values = c("grey50", "#0074D9", "#FF4136")) + * scale_fill_manual( * values = c("#0074D9", "#FF4136")) ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-42-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .task[ .tiny[ Adjust the axis labeling and remove the padding on both axes. ] ] .pull-left[ .tiny[ ``` r set.seed(13) base2 + scale_color_manual(values = c("grey50", "#0074D9", "#FF4136")) + scale_fill_manual(values = c("#0074D9", "#FF4136")) + * scale_x_continuous(expand = c(0, 0), * breaks = * seq(0, 175, 25)) + * scale_y_continuous(expand = c(0, 0), * breaks = * seq(0, 175, 25)) ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-43-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .task[ .tiny[ Add human-readable titles, labels, etc. ] ] .pull-left[ .tiny[ ``` r set.seed(13) base2 + scale_color_manual(values = c("grey50", "#0074D9", "#FF4136")) + scale_fill_manual(values = c("#0074D9", "#FF4136")) + scale_x_continuous(expand = c(0, 0), breaks = seq(0, 175, 25)) + scale_y_continuous(expand = c(0, 0), breaks = seq(0, 175, 25)) + * labs( * x = "Rank in 1995", y = "Rank in 2014", * title = "Changes in CO2 emission rankings between 1995 and 2014", * subtitle = "Countries that improved or worsened more than 25 positions in the rankings highlighted", * caption = "Source: The World Bank.\nCountries with populations of less than 200,000 excluded." * ) ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-44-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .task[ .tiny[ Get rid of the legends - unnecessary. ] ] .pull-left[ .tiny[ ``` r set.seed(13) base3 <-base2 + scale_color_manual(values = c("grey50", "#0074D9", "#FF4136")) + scale_fill_manual(values = c("#0074D9", "#FF4136")) + scale_x_continuous(expand = c(0, 0), breaks = seq(0, 175, 25)) + scale_y_continuous(expand = c(0, 0), breaks = seq(0, 175, 25)) + labs( x = "Rank in 1995", y = "Rank in 2014", title = "Changes in CO2 emission rankings between 1995 and 2014", subtitle = "Countries that improved or worsened more than 25 positions in the rankings highlighted", caption = "Source: The World Bank.\nCountries with populations of less than 200,000 excluded." ) + * guides(color = "none", fill = "none") base3 ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-45-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .task[ .tiny[ Change the base theme and font to match the text labels. ] ] .pull-left[ .tiny[ ``` r set.seed(13) base3 + * theme_bw(base_family = "Roboto Condensed") ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-46-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .task[ .tiny[ Use HTML and Markdown syntax to customize the visual appearance of the title and subtitle. ] ] .pull-left[ .tiny[ ``` r set.seed(13) base4 <- base3 + labs( x = "Rank in 1995", y = "Rank in 2014", * title = "Changes in CO<sub>2</sub> emission rankings between 1995 and 2014", * subtitle = "Countries that <span style='color: #0074D9'>**improved**</span> or <span style='color: #FF4136'>**worsened**</span> more than 25 positions in the rankings highlighted", caption = "Source: The World Bank.\nCountries with populations of less than 200,000 excluded." ) + guides(color = "none", fill = "none") + theme_bw(base_family = "comic neue") base4 ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-47-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .task[ .tiny[ Ensure rendering of HTML/Markdown syntax with `ggtext::element_markdown()`. ] ] .pull-left[ .tiny[ ``` r set.seed(13) base4 + labs( x = "Rank in 1995", y = "Rank in 2014", title = "Changes in CO<sub>2</sub> emission rankings between 1995 and 2014", subtitle = "Countries that <span style='color: #0074D9'>**improved**</span> or <span style='color: #FF4136'>**worsened**</span> more than 25 positions in the rankings highlighted", caption = "Source: The World Bank.\nCountries with populations of less than 200,000 excluded." ) + guides(color = "none", fill = "none") + theme_bw(base_family = "comic neue") + * theme( * plot.title = element_markdown(face = "bold", * size = rel(1.6)), * plot.subtitle = element_markdown( * size = rel(1.3)), * plot.margin = unit(c(0.5, 1, 0.5, 0.5), * units = "lines")) ``` ] ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-48-1.png" width="100%" style="display: block; margin: auto;" /> ] --- <img src="index_files/figure-html/unnamed-chunk-49-1.png" width="90%" style="display: block; margin: auto;" /> --- <img src="index_files/figure-html/unnamed-chunk-50-1.png" width="90%" style="display: block; margin: auto;" /> --- # Recap * Thinking about how to customize your plots to provide clear information for readers * Select themes that work best with your data and narrative * Fonts can be finnicky!!