Class 2: Data Visualization

Note: graphing paper will be useful today in class.

Key Ideas

A plot maps variables to positions, lengths, or symbols on a page. Which plot to draw depends on how many variables are involved and on whether each is categorical or numeric. Four plots cover most needs at this stage.

A bar plot displays a categorical variable: one bar per category, with height equal to the count (or share) of observations in that category.

A histogram displays a numeric variable. Its range is divided into intervals called bins, and each bar’s height is the number of observations falling in that bin. The resulting shape is the variable’s distribution.

A box plot compresses a numeric variable into five numbers: the minimum, the first quartile, the median, the third quartile, and the maximum. The quartiles are the medians of the lower and upper halves of the sorted data, so the box spans the middle half of the observations. Whiskers extend from the box to the most extreme observations within 1.5 times the box’s length; any observation beyond that is drawn as a point and called an outlier.

A scatterplot displays two numeric variables at once, one on each axis, with one point per observation. A line of best fit is a straight line drawn through the points to summarize their overall trend. For now, draw it by eye; a formal rule for placing it comes later in the course.

Problems of the Day

Q3: Equally Rested?

Say we take two samples of numbers of hours sleeping. Group A: 4, 7, 7, 7, 10. Group B: 4, 4, 7, 10, 10. For each group, calculate the minimum, maximum, range, and mean. Are these two groups equally well rested? If your gut says no, then the min, max, mean, and range don’t do a good job summarizing an important fact about the differences between these two groups. Invent a measurement that captures the difference.

Hint: think about how far each observation is from the mean (negative and positive).




To answer Q4, consult these summaries of the class data:

# A tibble: 5 × 2
  pet_type      n
  <chr>     <int>
1 Dog          28
2 Dog, Cat      7
3 Cat           6
4 Bird          2
5 Dog, Fish     2
# A tibble: 12 × 2
   sleep_hours     n
         <dbl> <int>
 1        10       4
 2         9       6
 3         8      25
 4         7.5     3
 5         7      15
 6         6.5     4
 7         6      20
 8         5       8
 9         4.5     1
10         4       7
11         3       1
12        NA       1
# A tibble: 16 × 3
   year   sleep_hours     n
   <chr>        <dbl> <int>
 1 First            4     6
 2 First            5     3
 3 First            6     8
 4 First            7     8
 5 First            8    16
 6 First            9     3
 7 First           10     3
 8 Fourth           6     3
 9 Fourth           8     3
10 Second           6     5
11 Second           7     3
12 Second           8     3
13 Third            5     4
14 Third            6     4
15 Third            7     3
16 Third            8     3
# A tibble: 10 × 2
   commute_miles sleep_hours
           <dbl>       <dbl>
 1             0         8  
 2            10         6  
 3            20         7  
 4            30        10  
 5             0         8  
 6             0         4  
 7            14         6  
 8             0         6  
 9            20         4  
10             7         7.5

Q4: The Dean’s Five Questions

The dean wants to know these five things about our class, answered in pictures instead of numbers.

  1. What pets do students have?
  2. How many hours do students sleep, and is 7 hours typical?
  3. Do the four years in school differ in hours of sleep?
  4. Do students with longer commutes sleep less?
  5. A new student commutes 12 miles. How much sleep should the dean expect them to get?

You have five tools: a scatterplot, and a scatterplot with a line of best fit drawn through it by eye, a box plot, a bar plot, and a histogram. Each fits exactly one question. Draw all five.

Q5: The Night Before Challenger

January 27, 1986. Engineers at Morton Thiokol are on a conference call with NASA, arguing that tomorrow’s forecast of 31°F is too cold for the rocket’s rubber O-rings. To make the case, they plot O-ring damage against launch temperature for every previous flight that had damage. NASA looks at the chart, sees no pattern, and approves the launch. Challenger breaks apart 73 seconds after liftoff.

There were 23 earlier flights. Click here to view the data (launch temperature in °F and the number of damaged O-rings for each flight).

Shuttle flights before Challenger

flight temperature (°F) damaged O-rings
1 66 0
2 70 1
3 69 0
4 68 0
5 67 0
6 72 0
7 73 0
8 70 0
9 57 1
10 63 1
11 70 1
12 78 0
13 67 0
14 53 2
15 67 0
16 75 0
17 70 0
18 81 0
19 76 0
20 79 0
21 75 2
22 76 0
23 58 1

Draw the chart the engineers drew. Then draw the chart they should have drawn. What changed, and why did leaving out the “boring” flights hide the pattern?

Extension. The coldest launch on record was 53°F. Add a line by eye to your full scatterplot and read off a prediction at 31°F. How much should anyone trust that number, and what should the engineers have said instead?

Answers

Q3: Equally Rested?

For both groups the minimum is 4, the maximum is 10, the range is 6, and the mean is 7, yet the groups are not alike. Group A (4, 7, 7, 7, 10) is mostly clustered at 7, with one short sleeper and one long sleeper, while Group B (4, 4, 7, 10, 10) is split, with nearly everyone at either 4 or 10. The four summaries cannot see this because the range only looks at the two extremes and the mean only looks at the total. The hint points to the deviations from the mean, \(x_i - \bar{x}\): for Group A these are \(-3, 0, 0, 0, 3\) and for Group B they are \(-3, -3, 0, 3, 3\). Group B has more observations far from the mean, which is exactly the feature we want to capture, but both lists sum to zero (the negatives exactly cancel the positives), so averaging the deviations tells us nothing.

The fix is to make the deviations positive before averaging, and squaring is the standard choice. For Group A the squared deviations are \(9, 0, 0, 0, 9\), which sum to 18. For Group B they are \(9, 9, 0, 9, 9\), which sum to 36. Group B’s total is twice as large, matching our intuition that B is more spread out. To turn a total into a typical value we divide by the number of observations, with one twist: statisticians divide by \(n - 1\) rather than \(n\). So we get \(18/4 = 4.5\) for A and \(36/4 = 9\) for B. This quantity is the sample variance, written \(s^2\):

\[s^2 = \frac{1}{n-1}\sum_{i=1}^{n}(x_i - \bar{x})^2.\]

Why \(n - 1\) and not \(n\)? The short version is that the deviations are measured from \(\bar{x}\), which was itself computed from the same five numbers, and that makes them come out a hair too small on average; dividing by a slightly smaller number corrects for it. We will return to this later in the course, and for now you can take it as the convention. Because squaring changes the units (hours become hours squared), we usually take the square root to get back to hours: \(\sqrt{4.5} \approx 2.1\) for A and \(\sqrt{9} = 3\) for B. That square root is the sample standard deviation \(s\). Taking absolute values instead of squaring also works and is a perfectly good invented measure; squaring is simply what statisticians settled on, for reasons we will see later in the course.

Q4: The Dean’s Five Questions

Each question pairs with exactly one tool, and the pairing is decided by how many variables are involved and whether each is categorical or numeric.

1. Pets: bar plot. Pet type is one categorical variable, so one bar per category. Dogs tower over everything else (28 students), then dog-and-cat households (7), cats alone (6), and a scattering of birds and fish. The point of the picture is the comparison of bar heights, which is why a bar plot works and a histogram does not: there is no numeric axis to bin.

2. Sleep, and is 7 typical: histogram. Sleep is one numeric variable. With one-hour bins the tallest bar is at 8 hours (25 students), followed by 6 hours (20) and 7 hours (15), and the distribution runs from 3 to 10. The median is 7, so in that sense 7 is typical, but the histogram shows something a single number hides: the class splits into a 6-hour crowd and an 8-hour crowd, and only about a quarter of students report between 6.5 and 7.5 hours. “Typical” is a claim about the whole shape, and the histogram is the tool that shows shape.

3. Sleep by year: box plot. One numeric variable (sleep) compared across the groups of one categorical variable (year), so one box per year, side by side. From the summary table, first, second and fourth years all have a median near 7 with a box running from about 6 to 8; third years sit lower, with a median around 6 and a box from about 5 to 7. First years have by far the longest whiskers, from 4 up to 10, partly because there are more of them. Five side-by-side boxes make this comparison in one glance, which is what the dean asked for.

4. Commute and sleep: scatterplot. Two numeric variables, so one point per student at (commute, sleep). In the ten students shown, the points form a loose cloud: a 20-mile commuter sleeps 4 hours, but so does someone with no commute, and the 25-mile commuter gets 7. There is no clear downward drift, so the honest answer is that any relationship is weak in this sample.

5. A 12-mile commuter: scatterplot with a line by eye. A prediction needs a line. Drawn through the cloud in question 4, the line comes out nearly flat at about 6.5 hours, so the dean should expect roughly 6.5 hours of sleep. The scatter around the line runs from 4 to 9 hours, though, so the prediction says more about the class average than about any one student. The line is what turns a picture of a relationship into a number, which is why it is a separate tool from the plain scatterplot.

Q5: The Night Before Challenger

The engineers’ chart. Only seven flights had damage: temperatures 53, 57, 58, 63, 70, 70 and 75, with damage counts 2, 1, 1, 1, 1, 1, 2. Plotted, these seven points are a flat smear. The two flights with the most damage sit at 53°F and 75°F, the coldest and the warmest of the seven, so temperature appears to have nothing to do with it. That is what NASA saw.

The chart they should have drawn. All 23 flights. Now sixteen zero-damage flights appear, and every one of them was launched at 66°F or warmer. Every flight below 66°F had damage (four out of four), while only three of nineteen flights at 66°F or above did. The pattern is stark: damage is a cold-weather problem.

What changed and why it mattered. Nothing about the seven damaged flights changed; the missing information was the comparison group. To show that cold causes damage you have to show that warmth avoids it, and the only evidence of that lives in the boring flights where nothing happened. Selecting on the outcome (keeping only flights with damage) threw away exactly the observations that carried the signal. This is the same lesson as the box plot in Q4: a difference between groups is invisible if you only look at one group.

Extension. A line by eye through the full scatterplot slopes downward, from near zero damaged O-rings at 80°F to a little over one at 53°F, and reading it off at 31°F gives roughly two damaged O-rings. That number deserves very little trust. It is an extrapolation: 31°F is 22 degrees colder than any shuttle had ever flown, and nothing in the data says the pattern stays a straight line that far out. It could be much worse. The line also ignores that there are six O-rings, so damage cannot grow forever, and it rests on just four cold launches. What the engineers should have said is what the honest chart says on its own: every launch below 66°F showed damage, the trend gets worse as it gets colder, we have never flown anywhere near 31°F, and we have no evidence it is safe. The burden was to show safety, not to predict a count.

Check Your Understanding


Measuring Spread

Choosing the Right Plot

Reading Histograms

Reading Box Plots

Scatterplots and Honest Charts