Chapter exercise: Into the Tidyverse: Graphs with ggplot2
Picturing Flights to North Carolina
This exercise goes with Chapter 9, Into the Tidyverse: Graphs with ggplot2. It uses the flights to North Carolina from the Chapter 6 to 8 exercises. It should take you about one hour. Read this whole section before you start.
Save it in a new folder called ggplot-exercise inside your geog215 folder, open it in RStudio, and work through it from top to bottom. Everything on this page is also at the top of the file.
Purpose
Skills you’ll practice. By the end of this exercise you will be able to:
- Build a graph from the ggplot2 template: data, aesthetic mappings in
aes(), and a geom. - Make scatterplots, bar charts, boxplots, and line graphs, and choose the right one for a question.
- Tell setting an aesthetic (one color for every point) from mapping one (a color for each group).
- Compare groups with stacked, filled, and side-by-side bars, and with small multiples (
facet_wrap()). - Finish a graph with a title, axis labels with units, a caption, and a theme, and save it with
ggsave().
Knowledge you’ll gain. You’ll see whether flights that leave late also arrive late, which New York airports serve North Carolina, and which months were worst for delays to each North Carolina airport.
Why this matters. A table answers a precise question, but a graph shows you the pattern, the outliers, and the mistakes in your data at a glance. Every report and final project in this course needs clear, labeled graphics. ggplot2 also uses the same grammar as tmap, which you’ll use for maps in the next chapter: data, then layers, then the details. Learning the grammar here makes mapping much easier.
Task
Work through Parts 1 to 8 in order, top to bottom. In each part:
- Read the short explanation, then do each step marked with a bullet (•).
- Wherever you see
___, replace it with the missing code before you run the chunk. A chunk with___in it won’t run. - Before you run each chunk, sketch in your head what the graph will look like. Then run it and compare.
- Answer each question (Q1, Q2, …) in a sentence or two of Markdown, where it says Your answer here.
- Remember that ggplot2 layers are joined with
+, not with the pipe.
| Part | What you’ll do | About |
|---|---|---|
| 1 | Set up your file and load the data | 3 min |
| 2 | Scatterplots and trend lines | 10 min |
| 3 | Setting and mapping aesthetics | 7 min |
| 4 | Bar charts: counts and shares | 10 min |
| 5 | Boxplots | 8 min |
| 6 | Line graphs from a summary table | 8 min |
| 7 | Small multiples with facets | 6 min |
| 8 | Finish and save a graph, reflect, and turn in | 8 min |
Turn in your .Rmd file, your knitted .html file, and the PNG file you save in Part 8, on Canvas.
Criteria for success
Use this checklist before you turn your work in. Each item matches a row of the rubric below.
| Criterion | Full credit | Partial credit | No credit |
|---|---|---|---|
| Knits (2 pts) | Knits to HTML with every graph showing | Knits, but some chunks have errors | Doesn’t knit, or no HTML turned in |
| Graph code (5 pts) | Every blank filled in correctly; every graph shows what was asked | Most graphs correct | Many blanks missing or wrong |
| Questions (8 pts) | Q1 to Q11 answered, describing the pattern in the graph, with reasons | Some answers missing, or vague (“it goes up”) | Most answers missing |
| Finished graph (3 pts) | Title, labeled axes with units, caption with source, theme; PNG saved at 7 × 5 in | Some labels or the PNG missing | Not done |
| Style (1 pt) | + between layers, pipe for data steps, no install.packages() |
Several problems | |
| Reflection (1 pt) | All three answered | Missing |
What a good answer looks like. Here is a question, a weak answer, and a strong one.
Question: What does a histogram of
distancefor all flights from New York show?Weak answer: “Most flights are short.”
Strong answer: “Most flights are under about 1,200 miles, with a big peak between 500 and 1,100 miles (the East Coast and Midwest). There’s a smaller group near 2,500 miles (the West Coast), almost nothing in between, and a few flights near 5,000 miles to Hawaii.”
The strong answer describes the shape with numbers and units, names the groups, and explains them.
IMPORTANT! Save often. Before you knit, choose Run > Restart R and Run All Chunks to make sure every chunk works from a fresh start.
```{r} and ending with three backticks.
A simple way of formatting plain text: # for headings,
*italic*, **bold**, and - for
list items.
The operator |>, which passes the result on its left as
the first argument of the function on its right. Read it as “and then.”
To build an R Markdown file into a finished document (HTML, PDF, or
Word). Knitting reruns every code chunk from the top in a fresh R
session.
A chart that divides a variable’s range into bins and draws a bar for
how many values fall in each. geom_histogram() in ggplot2.