ggplot2 & Data Visualization Principlesfacet_xxx(~variable_name, scales = ..., space = ...)
wrap or grid.
grid allows var1 ~ var2, rows ~ colswrap allows ~var1var1 + var2 to get a combinationfixed, free_x,free_y, orfree` (e.g. x and y)
free options work best with facet_wrapfree_x, free_y, fixed – only one column can be free space
free_x scalesfree_x for space and scalesfree_y scalesfree_y for space and scalesggplot2ggthemes packagelibrary(ggthemes) and p + theme_xxx()
ggthemr packageJust because you can doesn’t mean you should
Would you rather have a chart, or a data table
to understand a relationship?
to estimate a statistic?
to compute exact quantities?
to explain something?
Human retinas have a “bandwidth” of 8.75 megabits per second1
Reading speed is ~ 200 words per minute2, or ~ 0.0229 megabits per second
Most Accurate
Least Accurate
Well designed charts make it easy for the viewer to think about the data, rather than the chart
To design a good graphic, first consider:
What is the most important comparison?
What secondary comparisons do you want to show?
p1 <- penguins_reorder |>
filter(!is.na(sex)) |>
ggplot(aes(x = bill_len,
y = bill_dep,
color = sex)) +
geom_point(alpha = 0.5) +
coord_fixed(xlim = c(30, 60),
ylim = c(12.5, 22.5)) +
facet_wrap(~species, ncol = 2) +
labs(x = "Bill Length (mm)",
y = "Bill Depth (mm)",
color = "Sex") +
theme(legend.position = "bottom")
p2 <- penguins |>
filter(!is.na(sex)) |>
ggplot(aes(x = bill_len,
y = bill_dep,
color = species)) +
geom_point(alpha = 0.5) +
coord_fixed(xlim = c(30, 60),
ylim = c(12.5, 22.5)) +
facet_wrap(~sex, ncol = 1) +
labs(x = "Bill Length (mm)",
y = "Bill Depth (mm)",
color = "Species") +
theme(legend.position = "bottom")Which comparisons are easier with each type of plot?
Does it help to have reordered the species in the left plot so that you can compare bill depth and length?
Are there colors associated with your variables?
Using common color associations reduces cognitive load
Minimize categories: Keep # legend entries \(\leq 7\)
\(7 \pm 2\) is thought to be the limit of short-term memory.
- Use the smallest dimension possible - don’t use area when length will do - fake 3D perspective is misleading - Pixel accuracy - the number of pixels should match the magnitude of the data! - Infographics, logos, etc. look cute (sometimes?) but are often less accurately perceived.
**Calling this a chart is generous
