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:

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):

Class materials

Run the code below in your console to download today’s materials: usethis::use_course("css-materials/grammar-of-graphics")1


  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 ↩︎