Lecture 2
Overview
- Identify the importance of graphics in data analysis
- Define the layered grammar of graphics
- Practice generating layered graphics using
ggplot2
- Learn best practices for coding style in R
Before class
- Make sure your setup is completed and works as expected! See the “Before class” from Lecture 1 for details, and let us know (via Ed Discussion) if you have questions!
- Check Homework 1
Readings
Coding style:
- Chapter 4 “Workflow: code style” from “R for Data Science” 2nd Edition
- “The
tidyverse
style guide”: skim it for today, and refer back to it whenever you are unsure!
Grammar of Graphics and ggplot2
:
- Required: read “Chapter 1 Data visualization” from “R for Data Science” 2nd Edition. To be able to follow today’s lecture, you need to read this chapter!
- Recommended: read Hadley Wickham A Layered Grammar of Graphics – the “pre-print” version of the article can be downloaded for free. This article is optional, but I encourage you to skim through it to familiarize yourself with the theory behind the Grammar of Graphics. See especially section 3 “Components of the Layered Grammar” and section 4 “A Hierarchy of Defaults.” Understanding the logic of the Grammar of Graphics will make it much easier to apply it in R
Additional resources on ggplot2
(check these out whenever you need to make plots for this class or later on):
- ggplot2 Cheat Sheet
- ggplot2 Documentation
- ggplot2: Elegant Graphics for Data Analysis, 2nd Edition by Hadley Wickham. Excellent resource for learning the intricacies of
ggplot2
- R Graphics Cookbook, 2nd edition by Winston Chang. A practical guide with 150 examples to generate quality statistical graphics based on the data you wish to present
- Tufte, Edward R. The Visual Display of Quantitative Information. Classic book on statistical graphics and visualization design.
- Healey, Kieran. Data Visualization: A Practical Guide. An applied introduction to graphical design with lots of code examples.
Class materials
Run the code below in your console to download today’s materials: usethis::use_course("css-materials/grammar-of-graphics")
1
If you are using R from your local machine: make sure you have installed “usethis” (see Lecture 1 footnote to install it) and “gapminder” by typing
install.packages("gapminder")
in your console ↩︎