[1] 3.316625
Day 1 - Tuesday, May 26, 2026
Today: Tuesday May 26
Before class
Plan for today
Helpful links
We learned the pipe operator: x |> f() is the same as f(x).
Today we’ll learn to leverage the pipee to push data frames through chains of operations and into plots.
A data frame is essentially a table: rows and columns. It’s very similar to a spreadsheet, but it has specific rules and requirements.
# A tibble: 344 × 8
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
<fct> <fct> <dbl> <dbl> <int> <int>
1 Adelie Torgersen 39.1 18.7 181 3750
2 Adelie Torgersen 39.5 17.4 186 3800
3 Adelie Torgersen 40.3 18 195 3250
4 Adelie Torgersen NA NA NA NA
5 Adelie Torgersen 36.7 19.3 193 3450
6 Adelie Torgersen 39.3 20.6 190 3650
7 Adelie Torgersen 38.9 17.8 181 3625
8 Adelie Torgersen 39.2 19.6 195 4675
9 Adelie Torgersen 34.1 18.1 193 3475
10 Adelie Torgersen 42 20.2 190 4250
# ℹ 334 more rows
# ℹ 2 more variables: sex <fct>, year <int>
Rule of thumb
If you’re not sure what a “case” is in a data frame, ask: what does one row represent?
In penguins, one row = one penguin. In a Spotify dataset, one row might be one song. In NYC subway data, one row might be one turnstile entry.
The shape of the data frame depends entirely on what you decided to count.
Rows: 344
Columns: 8
$ species <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
$ island <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
$ bill_length_mm <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
$ bill_depth_mm <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
$ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186…
$ body_mass_g <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
$ sex <fct> male, female, female, NA, female, male, female, male…
$ year <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…
Rows: 344 (there are 344 penguins.)Columns: 8 (eight things we know about each one.)<fct> (factor), <dbl> (decimal number), <int> (whole number).While there are many types of variables, these are some of the most common that you’ll encounter:
<dbl>, <int>): body_mass_g, flipper_length_mm. You can do math on them.<fct>, <chr>): species, island, sex. Labels, not numbers.<lgl>): TRUE / FALSE.<date>, <dttm>): calendar dates and timestamps.The type of a variable determines what kind of plot makes sense for it.
Almost every dataset you’ll see in this course — and in the real world — is a data frame.
The skill we’re building: take any of these and ask good questions of them with code.
You could stare at this:
# A tibble: 10 × 2
species body_mass_g
<fct> <int>
1 Adelie 3750
2 Adelie 3800
3 Adelie 3250
4 Adelie NA
5 Adelie 3450
6 Adelie 3650
7 Adelie 3625
8 Adelie 4675
9 Adelie 3475
10 Adelie 4250
…or you could just look at a picture. Our eyes are very, very good at spotting patterns in plots. Tables, not so much.
The reading lays out five common types. Each one answers a different kind of question.
| Graphic | Answers the question… |
|---|---|
| Scatter plot | How do two numbers relate? |
| Histogram / freq. polygon | How is one variable distributed? |
| Bar chart | How does a quantity compare across groups? |
| Map | How does a variable vary across geography? |
| Network | How are things connected to each other? |
We’ll look at each one.
A scatter plot puts one numeric variable on the x-axis, another on the y-axis. Each case is one dot.
Use it when you want to see how two numbers move together.
The classic example: as kids get older, they get taller. Age on the x-axis, height on the y-axis, and the dots trace out a growth curve.
Bigger penguins tend to have longer bills, and the species cluster apart from each other.
Tip
A scatter plot can show a third variable through color, shape, or size. The plot above is really showing three variables at once: bill length, body mass, and species.
A histogram chops a numeric variable into bins and counts how many cases fall in each bin.
The tallest bar shows the most common range. The shape tells you whether values pile up on one side, spread evenly, or split into multiple bumps.
A frequency polygon is a histogram drawn as a line. The advantage: you can more easily overlay multiple groups on the same plot.
Now you can see that Gentoo penguins are heavier than the other two species: that wasn’t obvious in the single histogram.
Note
A real-world example: if you plot a histogram of household incomes in the US, you’ll see it’s heavily right-skewed: most people earn modest amounts, with a long tail of high earners. A scatter plot wouldn’t tell you that.
A bar chart is for comparing a single quantity across a handful of categories.
Bars next to each other are easy for the eye to compare. The length is the message.
Bar charts ≠ histograms
They look similar, but they’re different. A bar chart compares a value across categories. A histogram shows the distribution of one numeric variable. The bars in a histogram have a meaningful order (low to high); bar chart bars usually don’t.
A choropleth map shades regions (countries, states, counties) by the value of a variable. The reading shows oil production by country; you’ve seen the same idea on every election night cable broadcast.
Use a map when:
Maps can mislead
Bigger regions look more important even when they hold fewer people. Alaska on an election map dwarfs every state on the East Coast despite having fewer residents than Columbus, Ohio. Be aware of what your map is actually emphasizing.
A network diagram consists of two different structures:
Some examples you’ve probably seen without knowing it:
The shape of the network tells you things a table can’t:
The reading’s example: tumor cell lines that cluster by cancer type, suggesting that melanomas “look like” other melanomas genetically: useful if you’re trying to design a treatment.
Before you make a plot, ask: what question am I trying to answer?
| If your question is… | Choose a… |
|---|---|
| “How does X relate to Y?” (both numeric) | Scatter plot |
| “What’s a typical value of X? Are there outliers?” | Histogram or freq. polygon |
| “How does X compare across groups?” | Bar chart |
| “Where is X concentrated?” | Map |
| “How is X connected to Y?” | Network |
In 2015, Reuters published a chart of Florida gun deaths that flipped the y-axis upside down. The line dropped after a 2005 “Stand Your Ground” law went into effect: except the axis was inverted, and deaths actually rose.
The data was correct. The graphic was technically accurate. It just told the wrong story at a glance.
Takeaway
Graphics are persuasive. Pick the right one, label it clearly, and don’t let the chart say something the data doesn’t.
You won’t write much code today: the focus is on reading plots and matching them to questions. We’ll write the code to build them ourselves next week.
To turn in: Save your .qmd and rendered HTML. We’ll check at the end of class.
Common things to watch for:
Wednesday: We start building plots ourselves with ggplot2: the grammar of graphics.
Upcoming Deadlines