Data frames · intro to data graphics

Day 1 - Tuesday, May 26, 2026

NoteToday: Tuesday May 26

Before class

Plan for today

  • Quick recap of the pipe
  • What’s a data frame, really?
  • Five genres of data graphics
  • How to read each one
  • In-class activity

Helpful links

Quick recap

Last Friday

We learned the pipe operator: x |> f() is the same as f(x).

c(1, 4, 9, 16, 25) |>
  mean() |>
  sqrt()
[1] 3.316625

Today we’ll learn to leverage the pipee to push data frames through chains of operations and into plots.

Data frames

What is a data frame?

A data frame is essentially a table: rows and columns. It’s very similar to a spreadsheet, but it has specific rules and requirements.

  • Each row is one case (one penguin, one song, one person, one tweet).
  • Each column is one variable (a measurement or attribute of that case).
  • All values in a column have the same type (numbers, text, dates, etc.).
penguins
# 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>

Rows are cases, columns are variables

TipRule 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.

Anatomy of a data frame

glimpse(penguins)
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.)
  • Each column has a type next to it: <fct> (factor), <dbl> (decimal number), <int> (whole number).

Common variable types

While there are many types of variables, these are some of the most common that you’ll encounter:

  • Numeric (<dbl>, <int>): body_mass_g, flipper_length_mm. You can do math on them.
  • Categorical (<fct>, <chr>): species, island, sex. Labels, not numbers.
  • Logical (<lgl>): TRUE / FALSE.
  • Date/time (<date>, <dttm>): calendar dates and timestamps.

The type of a variable determines what kind of plot makes sense for it.

Data frame real-life examples

Almost every dataset you’ll see in this course — and in the real world — is a data frame.

  • Spotify’s “Wrapped” stats? A data frame: one row per song you played.
  • 538’s election forecasts? A data frame: one row per state per day.
  • Your Strava history? A data frame: one row per run.
  • The CDC’s COVID dashboards? Data frames stacked on data frames.

The skill we’re building: take any of these and ask good questions of them with code.

Why graphics?

Why we plot

You could stare at this:

penguins |> select(species, body_mass_g) |> head(10)
# 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.

Five genres of data graphics

The reading lays out five common types. Each one answers a different kind of question.

The big five.
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.

Scatter plots

Scatter plot: two numbers, one dot per case

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.

What to look for in a scatter plot

  • Direction: do the dots trend up, down, or neither?
  • Strength: tight line/group or random scatter?
  • Groups: do the dots clump into clusters? (Color-coding a categorical variable helps.)
  • Outliers: any dots way off on their own?
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.

Distributions

Histograms: how is one variable spread out?

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.

Frequency polygons: distributions, side by side

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.

What to look for in a distribution

  • Center: where do most values sit?
  • Spread: are they bunched up or scattered widely?
  • Shape: bell curve, skewed, two peaks, flat?
  • Gaps and outliers: any empty stretches, any extreme values?
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.

Bar charts

Bar charts: comparing groups

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.

When to use a bar chart

  • Comparing average salary across job titles
  • Counting goals scored by each team in the Premier League
  • Showing how many people picked each option in a survey
WarningBar 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.

Maps

Maps: visualizing spatial data

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:

  • The geography itself carries meaning (election results, disease outbreaks, internet access, weather).
  • You want to spot spatial clusters or regional patterns.
WarningMaps 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.

Networks

Networks: who connects to whom

A network diagram consists of two different structures:

  • Vertices (nodes, dots): the things.
  • Edges (lines): the connections between them.

Some examples you’ve probably seen without knowing it:

  • Your “mutuals” graph on social media: each user is a vertex, each follow is an edge.
  • The cast of every Marvel movie: actors as vertices, edges between two actors who appeared in the same film.
  • The internet itself: routers as vertices, cables as edges.

Insights from network graphics

The shape of the network tells you things a table can’t:

  • Clusters: groups of nodes tightly connected to each other (your friend group within a larger school)
  • Bridges: single nodes that connect otherwise-separate clusters
  • Hubs: nodes with way more connections than average

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.

Picking the right one

Match the graphic to the question

Before you make a plot, ask: what question am I trying to answer?

Question → graphic.
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

A bad-plot story

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.

ImportantTakeaway

Graphics are persuasive. Pick the right one, label it clearly, and don’t let the chart say something the data doesn’t.

In-class activity

Activity: read some plots (~ 20 min)

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.

Activity debrief

Common things to watch for:

  • A chart needs a question. “Make a plot of the data” is not a plan. “Does flipper length differ by species?” is.
  • Axis labels matter. “value” is not a label. “Body mass (g)” is.
  • Match the graphic to the data type. Two numeric variables? Scatter plot. One numeric variable? Histogram. Numeric vs. category? Bar chart or boxplot.
  • More chart isn’t better chart. 3D bars, rainbow gradients, and pie charts with 14 slices all hide information instead of showing it.

Wrap-up: what we covered today

  • A data frame is rows (cases) and columns (variables), with each column having a single type.
  • The type of a variable determines what kinds of plots make sense for it.
  • Five common genres: scatter plots, histograms/freq. polygons, bar charts, maps, networks.
  • Each genre answers a different kind of question. Pick the one that matches your question.
  • Graphics are persuasive: be honest with them.

Wrap-up & looking ahead

Wednesday: We start building plots ourselves with ggplot2: the grammar of graphics.

Upcoming Deadlines

Reach out

Stuck? Office hours, or email me (cgc5478@psu.edu) or Xinyue (xpw5228@psu.edu).

Reference

Syllabus · AI policy · Schedule · Data Computing