Lecture 2 · Data visualization
How heavy is a penguin?
We’ll make a histogram of penguin body mass, then use boxplots to compare the species.
Hi everyone! Today we’re going to make a few graphs with the same penguin data. If you’re unsure where to start, look at what one dot means. Then we can look at the boxes and see what changes when we split the data by species.
Start with the penguins
A histogram shows how the body masses are spread out. Boxplots let us compare species side by side. In our figure, the two panels help us check whether the same species tend to be heavier within each sex. The individual points also show where the groups overlap.
The graph changes when the question changes
For our ramen example, we counted products by country, so a bar chart made sense. With Gapminder we compared income and life expectancy in a scatterplot, then followed changes over time with a line. In ggplot2, start by saying what one dot or bar will show.
In R
This abbreviated version of the class figure uses the public palmerpenguins data and excludes rows with missing mass or sex.
library(ggplot2)
p <- subset(palmerpenguins::penguins,
!is.na(body_mass_g) & !is.na(sex))
ggplot(p, aes(species, body_mass_g, colour = species)) +
geom_boxplot(outlier.shape = NA) +
geom_jitter(width = 0.15, alpha = 0.35) +
facet_wrap(~ sex) +
labs(x = "Species", y = "Body mass (g)")
Use the box to compare medians and middle ranges, then check the points for overlap and unusual observations.
Key takeaways
- Pick the chart after you decide what you want to compare.
- Label the units and show where the groups overlap.



