DSCI 100 Week 4 : Effective data visualization

Sky (Kehan) Sheng

Housekeeping

🔁 Week 3 Recap: Cleaning and wrangling

iClicker: Which function makes this tidy?

🔬 Demo: pivot_longer()

  • cols: which columns to collapse into longer rows
  • names_to: the new column that holds the collapsed column name (i.e., year now records either 1999 or 2000)
  • values_to: the new column that holds the values in each cell in the old columns (i.e., cases now records the number of cases in each country in each year)

🤔 And this one?

country year key value
Afghanistan 1999 cases 745
Brazil 1999 cases 37737
Brazil 1999 population 172006362
China 1999 cases 212258
China 1999 population 1272915272

iClicker: Pick the right penguins

Week 4: Effective data visualization

🚨 Visualization Crime Zone: What’s wrong with this bar plot?

The bar heights don’t match the numbers.

Is global warming real?

Same data, honest axis

🤔 Is the air changing?

CO₂ traps heat in the atmosphere, so more of it means a warmer planet. Scientists on Mauna Loa, Hawaii, measured the CO₂ in the air every month from 1959 to 1997.

CO₂ (ppm) month
315.42 1959-01
316.31 1959-02
316.50 1959-03
317.56 1959-04
318.13 1959-05
318.00 1959-06
316.39 1959-07
314.65 1959-08

… and 460 more rows

Look at the numbers.

Is CO₂ going up, going down, or staying the same?

Could you tell from 468 numbers? 😵‍💫

🤔 Same numbers, as a picture

A good picture answers the question in one second.

🔄 Where are we in the workflow?

Week 3 was tidy and transform. Today is visualize.

✏️ Designing a visualization

  • Ask a question, then answer it.
  • The purpose of a visualization is to answer a question about a data set.
  • A good visualization answers the question clearly. A great visualization also hints at the question itself.

✏️ What questions can a plot answer?

Visualizations alone help us answer two types of questions:

  • descriptive: What are the 12 largest landmasses on Earth?
  • exploratory: Is there a relationship between penguin body mass and bill length?
  • inferential, predictive, causal, mechanistic: we need more tools than a plot for these

🧱 Plots in R with ggplot2

ggplot2 comes with tidyverse. A plot has three key pieces:

  1. aesthetic mappings aes(): which column goes to which visual property (x, y, colour, …)
  2. geometric objects geom_*(): how to draw it (points, lines, bars, …)
  3. scales: transform variables, set limits

Add the pieces one at a time with +.

Tip

Visualization is an iterative procedure. Try things, make mistakes, and refine!

🧱 Types of variables

A variable is a characteristic we measured. It comes in two kinds:

  1. Categorical: falls into groups (categories). e.g. marital status, continent
  2. Quantitative: a number on a scale, usually with units. e.g. height, horsepower

The kind of variable, and the question you ask, decide which plot to use.

⚠️ A number is not always quantitative: a car has 4, 6 or 8 cylinders. Those are three groups.

📊 Scatter plots

One of the best ways for us to battle climate change is to drive cars less frequently or drive cars with better fuel efficiency! Better fuel economy means less CO₂ generated per mile.

Is there a relationship between horsepower and fuel economy of an engine (relationship between two quantitative variables)? Does the number of cylinders change that?

🧱 What each piece added

📊 Line plots

Show a trend over an ordered quantity, usually time.

How has CO₂ in the air changed over the last 40 years?

📊 Why a line and not points?

The line connects the measurements in order, so the up and down of every year shows.

📊 Bar plots

Compare amounts across categories.

Which are the 12 largest landmasses on Earth? Are they all continents, or are some islands just as large?

stat controls how geom_bar() turns rows into bar height. By default it counts rows. stat = "identity" means: skip counting, use the y value as is.

📊 Why did geom_bar() complain?

📊 Histograms

Show the distribution of one quantitative variable: how the values are spread out.

🔬 Demo: Life expectancy by continent

Was there a difference in life expectancy across continents in 2016?

🗺️ Which plot for which question?

Question Variables Plot geom
How are two numbers related? 2 quantitative scatter geom_point()
How does it change over time? quantitative over time line geom_line()
How do amounts compare across groups? categorical + amount bar geom_bar(stat = "identity")
How are the values spread out? 1 quantitative histogram geom_histogram()

📏 Rule of thumb 1: No tables or pie charts

Which one is easier to read?

📏 Rule of thumb 2: No 3D

📏 (Bonus) Sky’s exception: when 3D earns its place

📏 Rule of thumb 3: Simple, colourblind-friendly colours

Tip

Check your own plots: Coblis, Color Blindness Simulator

Colourblind-friendly palettes in R

R has lots of colourblind-friendly palettes, such as the viridis package:

📏 Rule of thumb 4: Legible labels and legends

A great visualization tells its own story without you standing next to it.

📏 Rule of thumb 5: Avoid overplotting

Usually the fix is a different geometric object. 53,940 diamonds on one plot:

  • Add alpha = 0.2 to geom_point()
  • alpha sets transparency, between 0 (invisible) and 1 (solid)

📅 Upcoming Due Dates

Important

  • worksheet_viz due next Monday, Oct 5, 11:59pm
  • tutorial_viz due next Monday, Oct 5, 11:59pm

Tip

Before next class: please register for a free GitHub account, it will help you follow along: https://github.com/signup

Need a data viz refresh? Check out this optional video.

❓ Questions?

If not, let’s work on this week’s worksheet together.

  • Want to save these slides? Right click → Print → Save to PDF.
  • Your code and notes only save if you save as a PDF!