Grammer of Graphics

Lecture 3

Dr. Elijah Meyer + Konnie Huang

Duke University
STA 199 - Fall 2022

September 5th, 2022

Checklist

– Have a GitHub account and have it connected with R (Lab 0)

– Have checked the GitHub username sheet

– Keeping up with the Prepare link on course website

– Go to the course GitHub org and find your ae-02-starwars (repo name will be suffixed with your GitHub name). – Clone the repo in your container, open the Quarto document in the repo

Goals for today

– Think about what to do (and not to do) with visualizations

– Understand the fundamentals of ggplot

– Build appropriate visualizations

– More practice with R

Announcements

– Turn in AE’s via GitHub (will go over today)

– Will turn in Lab 1 via GitHub (Thursday)

– Release HW 1 Wednesday (Turned in on Gradescope: Being set up now)

Prepare Reading Questions

https://app.sli.do/event/g6fnKCDuZ1sw8NKiZTu3Rb

Reading Highlights

– Sometimes you’ll run the code and nothing happens. Check the left-hand of your console: if it’s a +, it means that R doesn’t think you’ve typed a complete expression and it’s waiting for you to finish it.

Examining data visualization

Discuss the following for the visualization.

– What is the visualization trying to show?

– What is effective, i.e. what is done well?

– What is ineffective, i.e. what could be improved?

– What are you curious about after looking at the visualization?

04:00

Source: Twitter

Application exercise

ae-02-starwars

  • Go to the course GitHub org and find your ae-02-starwars (repo name will be suffixed with your GitHub name).
  • Clone the repo in your container, open the Quarto document in the repo, and follow along and complete the exercises.
  • Render, commit, and push your edits by the AE deadline – 3 days from today (Thursday by 11:59p ET).

Exercises 2.3.1

Match the variables to plots

– 1 categorical variable, 1 quantitative variable (often ordinal)

– 2 quantitative variables

– 1 quantitative variable

04:00

– geom_histogram

– geom_point

– geom_bar

Recap of AE

– Construct plots with ggplot().

– Layers of ggplots are separated by +s.

– The formula is (almost) always as follows:

ggplot(DATA, aes(x = X-VAR, y = Y-VAR, ...)) +
  geom_XXX()

– Aesthetic attributes of a geometries (color, size, transparency, etc.) can be mapped to variables in the data or set by the user.

– Use facet_wrap() when faceting (creating small multiples) by one variable and facet_grid() when faceting by two variables.