
06-viz
Effective Data Visualization
Q&A
A: I was not as clear as I should have been in this! The police knew that some participants received a placebo and some did not (that’s fine!), but they also knew how long it had been since they had smoked the joint. So, they would have expected (given knowing this information) that at each subsequent field sobriety test that they should maybe expect fewer and fewer participants to be impaired. Given unlimited resources, a more ideal design would have been to bring officers in randomly, without them knowing how far along the study was, so they didn’t have any expectation/knowledge about how recently someone could have smoked (which is the case at the roadside…they don’t know)
A: The cutoffs are if we used that cutoff to determine someone had recently used then how many people would be “over the limit.”
A: Some people find tidyverse cheat sheets particulrly helpful: https://opensource.posit.co/resources/cheatsheets/data-visualization/. Chapter 9 of our textbook also goes through the basics: https://r4ds.hadley.nz/layers.html. There are a lot of additional resources - if others have found some they really like, please share!
Course Announcements
Due Dates:
- ⌨️ Lab 03 due tonight 10/16 (11:59 PM)
- 📚 HW 02 due next Sun 10/25 (11:59 PM) <- this is an update
- 🔘 Lecture Participation survey “due” after class
Notes:
- Data in these notes are in lab03
- Lab 02 scores/feedback were posted - everyone got some comment/feedback in their issue on GH
- pre-course survey EC posted
- will get an email response later
- lots of cool things you’re good at:
- Dance
- Guitar
- Running
- Big-picture thinking
- Art
- Hyping people up
- Baking
- Badminton
- Serving/waitressing
- Fast typing (~100 WPM)
- A cappella
- Rowing
- Painting Warhammer miniatures
- Video editing
- Swimming
- Gel-X nails
- Sewing
- Community organizing
- Photography and live events
- Soccer
- Architecture and floorplan drawing
- Writing poetry
- Sleeping
- Basketball
- Trip planning
- Braising pork
Suggested Reading
- R4DS Chapter 11: Data Communication
- The Glamour of Graphics: [video] [Prof’s slides inspired by Will’s talk]
Keep it simple

Use color to draw attention


Tell a story

Credit: Angela Zoss and Eric Monson, Duke DVS
Principles for effective visualizations
Principles for effective visualizations
- Order matters
- Put long categories on the y-axis
- Choose an appropriate scale
- Pick a purpose
- Keep scales consistent (and say so when you don’t)
- Select meaningful colors
- Use meaningful and nonredundant labels
- Tell a story
. . .
Today we’ll apply each of these to our CS01 data.
The data
The cleaned, combined CS01 data (the same data/cs01_combined.csv that’s in your lab03 repo):
cs01 <- read_csv("data/cs01_combined.csv")
# mostly, we'll focus on whole blood
# (2 blood samples have no recorded time, so no time window)
wb <- cs01 |>
filter(fluid_type == "WB", !is.na(timepoint))Order matters
Alphabetical is rarely ideal
How many blood samples were collected in each time window?

ggplot(wb, aes(x = timepoint)) +
geom_bar(). . .
🧠 What’s wrong with this order? Why did it happen?
Why alphabetical?
clean_cs01()madetimepointa factor with levels in time order- But a CSV file only stores text, so
read_csv()readstimepointback in as a character variable ggplot2sorts character variables alphabetically: “101-180 min” comes before “31-70 min”, and “pre-smoking” ends up last
. . .
You will run into this in CS01. Fix it once, right after reading in the data.
Use inherent level order
Put long categories on the y-axis
Overlapping labels are hard to read
And reverse the order of levels
So time reads from top to bottom:
Clean up labels
. . .
🧠 Why might some windows have more samples than others?
Choose an appropriate scale
The first 30 minutes
For the rest of today: blood THC in the first window after smoking.
wb_early <- wb |>
filter(timepoint == "0-30 min")Order matters (again)

ggplot(wb_early, aes(x = thc, y = treatment)) +
geom_boxplot(). . .
Doses are in alphabetical order (“13.4%” before “5.9%” before “Placebo”).
Relevel by dose
Skewed data hide the details

- About 34% of blood THC values are 0, and a few are very large
- On a linear scale, the placebo group (and anything small) gets squashed against zero
Transform the scale
- A log scale spreads out small values and pulls in large ones…but
log(0)is-Inf "pseudo_log"acts like a log scale for large values and handles zeros- The axis still shows the original values. Always say in the axis label that the scale is transformed.
Pick a purpose
Compare groups within each dose
Compare doses within each group
. . .
🧠 Which plot better answers “Do frequent users have higher blood THC than occasional users?” Which better answers “Does dose matter?”
Keep scales consistent
Free scales can mislead
- Each panel gets its own x-axis: the largest occasional-user value (156 ng/mL) stretches as far as the largest frequent-user value (231 ng/mL)
- When panels share units and you want to compare them, keep the default (fixed) scales
…but sometimes free scales are the right call
Same first window, three matrices (THC-treated participants only):


cs01_early <- cs01 |>
filter(timepoint %in% c("0-30 min", "0-40 min"),
treatment != "Placebo")
ggplot(cs01_early, aes(x = thc)) +
geom_histogram(bins = 30) +
facet_wrap(~fluid_type)- Breath values reach the hundreds of thousands, so blood and oral fluid disappear on a shared axis
- And breath is measured in different units, so comparing raw values across panels isn’t meaningful anyway
- If you free the scales, make it obvious (here: units in every panel label)
Select meaningful colors
Default colors don’t know your data
- Dose is ordered (none, low, high), but these colors suggest three unrelated categories
- And red next to green: red-green is the most common form of color vision deficiency (roughly 1 in 12 men)
Choosing better colors

- Sequential: ordered values (dose, time)
- Diverging: values with a meaningful middle
- Qualitative: unordered categories (user group)
Use a sequential palette for ordered categories
Use meaningful and nonredundant labels
Be selective with redundancy
The legend repeats the y-axis labels:

ggplot(wb_early, aes(x = thc, y = treatment, fill = treatment)) +
geom_boxplot() +
scale_x_continuous(transform = "pseudo_log", breaks = c(0, 1, 10, 100)) +
scale_fill_manual(values = c("Placebo" = "#deebf7",
"5.9% THC (low dose)" = "#9ecae1",
"13.4% THC (high dose)" = "#3182bd")) +
guides(fill = "none")Use informative labels

ggplot(wb_early, aes(x = thc, y = treatment, fill = treatment)) +
geom_boxplot() +
scale_x_continuous(transform = "pseudo_log", breaks = c(0, 1, 10, 100)) +
scale_fill_manual(values = c("Placebo" = "#deebf7",
"5.9% THC (low dose)" = "#9ecae1",
"13.4% THC (high dose)" = "#3182bd")) +
guides(fill = "none") +
labs(
title = "Blood THC in the first 30 minutes after smoking",
subtitle = "Whole blood samples, 0-30 minutes after smoking began",
x = "THC (ng/mL, pseudo-log scale)",
y = NULL,
caption = "Data: CS01 (Hubbard et al., 2021)"
)Select theme

ggplot(wb_early, aes(x = thc, y = treatment, fill = treatment)) +
geom_boxplot() +
scale_x_continuous(transform = "pseudo_log", breaks = c(0, 1, 10, 100)) +
scale_fill_manual(values = c("Placebo" = "#deebf7",
"5.9% THC (low dose)" = "#9ecae1",
"13.4% THC (high dose)" = "#3182bd")) +
guides(fill = "none") +
labs(
title = "Blood THC in the first 30 minutes after smoking",
subtitle = "Whole blood samples, 0-30 minutes after smoking began",
x = "THC (ng/mL, pseudo-log scale)",
y = NULL,
caption = "Data: CS01 (Hubbard et al., 2021)"
) +
theme_minimal(base_size = 16) +
theme(plot.title.position = "plot",
panel.grid.major.y = element_blank())Tell a story
What’s the takeaway?

🧠 What do you notice about the low vs. high dose? What might explain it? (Think back to the smoking topography slides from the CS01 intro.)
Put the message in the title

ggplot(wb_early, aes(x = thc, y = treatment, fill = treatment)) +
geom_boxplot(outlier.shape = NA) +
geom_jitter(height = 0.15, alpha = 0.4, size = 1) +
scale_x_continuous(transform = "pseudo_log", breaks = c(0, 1, 10, 100)) +
scale_fill_manual(values = c("Placebo" = "#deebf7",
"5.9% THC (low dose)" = "#9ecae1",
"13.4% THC (high dose)" = "#3182bd")) +
guides(fill = "none") +
labs(
title = "Blood THC was no higher in the high dose group",
subtitle = "Whole blood samples, 0-30 minutes after smoking began",
x = "THC (ng/mL, pseudo-log scale)",
y = NULL,
caption = "Data: CS01 (Hubbard et al., 2021)"
) +
theme_minimal(base_size = 16) +
theme(plot.title.position = "plot",
panel.grid.major.y = element_blank())- A title can describe the plot (“Blood THC in the first 30 minutes”) or state its message
- Adding the points shows how many people are behind each box (and how much they vary)
Principles recap
What we covered (cite these by name in your homework and case studies):
- Keep it simple: skip 3D and pies when a bar chart does the job
- Use color to draw attention to what matters
- Tell a story: put the takeaway in the title or an annotation
- Order matters: avoid alphabetical; use inherent order or order by frequency
- Put long categories on the y-axis
- Choose an appropriate scale: transform skewed data (and say so)
- Pick a purpose: decide which comparison the plot should make easy (e.g., dodged vs. faceted)
- Keep scales consistent across panels, unless units differ (and then say so)
- Select meaningful colors: sequential for ordered values, colorblind-friendly, consistent meaning
- Use meaningful and nonredundant labels: titles, units, sources; drop redundant legends
Your Turn
New data! In September 2019, a YouGov survey asked 1,639 GB adults: In hindsight, do you think Britain was right/wrong to vote to leave EU? (Right to leave / Wrong to leave / Don’t know)
brexit <- tibble(
opinion = c(
rep("Right", 664), rep("Wrong", 787), rep("Don't know", 188)
),
region = c(
rep("london", 63), rep("rest_of_south", 241), rep("midlands_wales", 145), rep("north", 176), rep("scot", 39),
rep("london", 110), rep("rest_of_south", 257), rep("midlands_wales", 152), rep("north", 176), rep("scot", 92),
rep("london", 24), rep("rest_of_south", 49), rep("midlands_wales", 57), rep("north", 48), rep("scot", 10)
)
)- Make a bar chart of opinion by region
- Then improve it using at least three of today’s principles (name them!)
- Think of two different purposes (stories) these data could serve
Source: YouGov Survey Results, 2-3 September 2019
Put a green sticky on the front of your computer when you’re done. Put a pink if you want help/have a question.
Recap
- Can you determine what needs to be done to improve the effectiveness of your visualizations?
- Can you choose scales, colors, and labels that fit your data?
- Can you execute said improvements using
ggplot2? - Can you tell a story with data?










