class: center, middle, inverse, title-slide # Data Visualization ## Theme ### Andrew Irwin,
a.irwin@dal.ca
### Math & Stats, Dalhousie University ### 2021-03-26 (updated: 2021-03-12) --- class: middle # Plan * What is the `theme` of a visualization? * Ready-to-use themes * Titles * Text size, colour, bold, italics, and more * Spacing and positioning --- ### Starting figure <img src="32-theme_files/figure-html/unnamed-chunk-1-1.svg" width="70%" style="display: block; margin: auto;" /> --- ### Ready to use themes ```r p1 + theme_bw() ``` <img src="32-theme_files/figure-html/unnamed-chunk-2-1.svg" width="70%" style="display: block; margin: auto;" /> --- ### Ready to use themes ```r p1 + theme_classic() ``` <img src="32-theme_files/figure-html/unnamed-chunk-3-1.svg" width="70%" style="display: block; margin: auto;" /> --- ### More themes * `theme_dark`, `theme_gray`, `theme_light` * From `ggthemes`: `theme_wsj`, `theme_tufte`, `theme_fivethirtyeight`, `theme_economist`, `theme_clean` --- ### All text larger ```r p1 + theme(text = element_text(size=22)) ``` <img src="32-theme_files/figure-html/unnamed-chunk-4-1.svg" width="70%" style="display: block; margin: auto;" /> --- ### Sizes for different elements ```r p1 + theme(axis.text.x = element_text(size=16), axis.title.y = element_text(size=18), legend.title = element_text(size=16), legend.text = element_text(size=18), plot.title = element_text(size=21), plot.subtitle = element_text(size=20)) + labs(x = "Year", y = "Life Expectancy at birth", title = "Life expectancy averaged by continent", subtitle = "From Gapminder.org") ``` <img src="32-theme_files/figure-html/unnamed-chunk-5-1.svg" width="70%" style="display: block; margin: auto;" /> --- ### Other features of text ```r p1 + labs(title = "Average life expectancy") + theme(plot.title = element_text(family = "serif", face = "italic", color = "blue", size = 30, hjust = 1, lineheight = 2)) ``` <img src="32-theme_files/figure-html/unnamed-chunk-6-1.svg" width="70%" style="display: block; margin: auto;" /> --- ### Fancy text: ggtext ```r library(ggtext) p1 + labs(title = "**Gapminder** data for _life expectancy_<sup>1</sup>", caption = "<sup>1</sup>from gapminder.org") + theme(plot.title = element_markdown(size = 24), plot.caption = element_markdown(size = 13)) ``` <img src="32-theme_files/figure-html/unnamed-chunk-7-1.svg" width="70%" style="display: block; margin: auto;" /> --- ### Move and redesign the legend ```r p1 + theme(legend.position = "top", legend.direction = "horizontal", legend.text = element_text(size = 11, vjust = 0.5), legend.title = element_text(size = 12, vjust = 0.5), legend.key.size = unit(0.9, "cm")) ``` <img src="32-theme_files/figure-html/unnamed-chunk-8-1.svg" width="70%" style="display: block; margin: auto;" /> --- ### Move and redesign the legend ```r p1 + theme_bw() + theme(legend.position = c(0.03, 0.98), legend.justification = c(0,1), legend.direction = "vertical", legend.background = element_rect(fill = "transparent"), legend.text = element_text(size = 10, vjust = 0.5), legend.title = element_blank(), legend.key.size = unit(0.8, "cm")) + guides(color = guide_legend(ncol = 3)) ``` <img src="32-theme_files/figure-html/unnamed-chunk-9-1.svg" width="70%" style="display: block; margin: auto;" /> --- ### Formatting numbers on scales ```r p1 + scale_x_continuous(labels = scales::dollar) + scale_y_continuous(labels = scales::percent) + # also comma theme(axis.text = element_text(size=14)) ``` <img src="32-theme_files/figure-html/unnamed-chunk-10-1.svg" width="70%" style="display: block; margin: auto;" /> --- class: middle # Further reading * Course notes * Healy and Wilke sections on colour --- class: middle, inverse ## Task * Practice using colour * No assigned task