Lesson 4 · R Programming: Zero to Expert
ggplot2 Full Course: Data Visualisation & EDA in R | R 104
Video 4 of R Programming: Zero to Expert Goal: turn tidy data into clear charts and clear findings. R script: 04ggplot2eda.R. Download it from this page and…
- CourseR Programming: Zero to Expert
- Lesson4 of 6
- Video1 h 12 min
- FormatR script + Jupyter notebook (R) · 99 code cells
What you'll learn
Files for this lesson
Save these in one folder, open the script in RStudio and set that folder as the working directory (Session → Set Working Directory → To Source File Location).
- 04_ggplot2_eda.R16.2 KB
📓 Full notebook
Download .ipynbData Visualisation and Exploratory Analysis with ggplot2#
Video 4 of R Programming: Zero to Expert
Goal: turn tidy data into clear charts and clear findings.
R script: 04_ggplot2_eda.R. Download it from this page and open it in RStudio, or run this notebook with an R kernel (for example in Google Colab: Runtime → Change runtime type → R).
Quick recap and why we visualise#
Videos 1 to 3: base R, data structures, functions, dplyr, tidyr.
Today: ggplot2, the plotting package inside the tidyverse.
library(tidyverse)
The console lists the attached packages, and ggplot2 is right there at the top of the list. The conflicts lines just warn that dplyr's filter and lag hide the versions from base R's stats package. That's expected and harmless.
library(palmerpenguins)
Nothing prints, which in R usually means success. The penguins table is now available.
Why charts? Anscombe's quartet#
Four tiny datasets with almost identical summary statistics.
quartet <- anscombe |>
pivot_longer(
everything(),
names_to = c(".value", "set"),
names_pattern = "(.)(.)"
)
quartet |> arrange(set) |> head(5)
Now we have a tidy tibble with three columns: set, x and y. Forty four rows in total, eleven points for each of the four sets.
quartet |>
group_by(set) |>
summarise(
mean_x = mean(x),
mean_y = round(mean(y), 2),
sd_y = round(sd(y), 2),
r = round(cor(x, y), 2)
)
Look at that. All four sets have a mean x of nine, a mean y of seven point five, a standard deviation of about two point oh three, and a correlation of point eight two. If we only looked at these numbers, we'd say the four datasets are the same.
ggplot(quartet, aes(x = x, y = y)) +
geom_point(size = 3, colour = "steelblue") +
geom_smooth(method = "lm", formula = y ~ x, se = FALSE) +
facet_wrap(~ set)
Four panels, and four completely different stories. Set one is a normal noisy linear trend. Set two is a smooth curve, so a straight line is the wrong model. Set three is a perfect line with one outlier dragging the fit upwards, and set four has all points stacked at x equals eight, with a single extreme point at nineteen creating the whole trend. Same blue trend line in every panel, totally different data. That's why we always plot.
The grammar of graphics#
Every ggplot = data + aesthetic mappings + geoms (layers).
Layers are added with the plus sign.
ggplot(data = penguins)
The Plots pane shows a blank grey rectangle. ggplot knows about the data, but we haven't told it what to put on the axes, or what to draw.
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
)
Now we get labelled axes with grid lines. Flipper length runs from about one seventy to two thirty millimetres, and body mass from under three thousand to over six thousand grams. But still no points.
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_point()
Our first scatter plot. There's a clear upward pattern: penguins with longer flippers are heavier. In the console you'll also see a warning that two rows were removed because of missing values. Two penguins have no measurements, so ggplot can't place them.
peng <- penguins |> drop_na()
nrow(penguins)
nrow(peng)
We go from three hundred and forty four penguins to three hundred and thirty three. Eleven rows had a missing value somewhere, mostly in the sex column. Later, in the exploratory analysis section, we'll investigate missing values properly instead of just dropping them.
p <- ggplot(peng, aes(x = flipper_length_mm, y = body_mass_g))
class(p)
The class is gg and ggplot. Nothing was drawn, because we assigned it instead of printing it.
p + geom_point()
The same scatter plot, built from the saved object, and this time with no warning because peng has no missing values.
Two classic mistakes#
ggplot(peng, aes(x = flipper_length_mm, y = body_mass_g)) |>
geom_point()
An error. The pipe passed the whole plot into geom_point as its first argument, which is mapping. geom_point complains that the mapping must be created by aes. Rule to remember: pipes join data steps, plus signs join plot layers.
This one is meant to fail, so you can see the error message.
ggplot(peng, aes(x = flipper_length_mm, y = body_mass_g))
+ geom_point()
Line one draws an empty plot with axes only. Then R tries to run plus geom_point on its own, and ggplot2 stops with: cannot use plus with a single argument. It even hints that you probably put the plus on a new line. The fix: the plus sign always goes at the end of the line, so R knows more is coming.
This one is meant to fail, so you can see the error message.
Scatter plots: colour, shape, size and alpha#
Map a column inside aes(); set a fixed value outside aes().
ggplot(peng, aes(x = flipper_length_mm, y = body_mass_g,
colour = species)) +
geom_point()
Each species gets its own colour, and ggplot adds a legend on the right automatically. Gentoo, in blue, forms its own cluster in the top right: longer flippers around two hundred and seventeen millimetres and about five thousand grams. Adelie in red and Chinstrap in green overlap a lot lower down, at around one ninety to one ninety six millimetres and roughly three thousand seven hundred grams.
ggplot(peng, aes(x = flipper_length_mm, y = body_mass_g,
colour = species, shape = species)) +
geom_point(size = 2)
Now Adelie are circles, Chinstrap are triangles and Gentoo are squares, and they're a little bigger. ggplot merges colour and shape into one legend because they map the same column.
ggplot(peng, aes(x = bill_length_mm, y = bill_depth_mm,
colour = species, size = body_mass_g)) +
geom_point(alpha = 0.6)
Three clear groups appear. Adelie sit on the left with short, deep bills. Chinstrap sit top right with long, deep bills, and Gentoo bottom right with long but shallow bills. The Gentoo points are visibly bigger, and there are now two legends: one for species colour and one for body mass size.
ggplot(peng, aes(x = bill_length_mm, y = bill_depth_mm,
colour = "blue")) +
geom_point()
The points come out a salmon red, not blue, and there's a strange legend with a single entry called blue. Inside aes, ggplot treats blue as a data value, a constant column of the word blue, and then picks its first default colour for it.
ggplot(peng, aes(x = bill_length_mm, y = bill_depth_mm)) +
geom_point(colour = "blue")
Now every point is genuinely blue, and there's no legend, because nothing is mapped to colour.
Overplotting: too many points in one place#
nrow(mpg)
nrow(distinct(mpg, displ, hwy))
Two hundred and thirty four cars, but only one hundred and twenty six distinct positions. So more than a hundred cars will be hidden behind other points in a scatter plot.
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point()
A clear negative relationship: bigger engines use more fuel, so highway miles per gallon falls from the mid thirties to the low teens as displacement grows. The points sit on a neat grid, because displacement and miles per gallon are rounded. That grid is exactly where overplotting hides cars.
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_jitter(width = 0.1, height = 0.5, alpha = 0.6)
Now the stacks spread out into little clouds, and we can see where cars really pile up, for example around two litres and twenty nine miles per gallon, where ten cars shared one spot.
set.seed(42)
diamonds_small <- diamonds |> slice_sample(n = 2000)
nrow(diamonds)
nrow(diamonds_small)
Fifty three thousand nine hundred and forty diamonds in the full data, and two thousand in our sample. Sampling keeps scatter plots fast and readable.
ggplot(diamonds_small, aes(x = carat, y = price)) +
geom_point(alpha = 0.2)
The cloud bends upwards: price rises faster than carat, from a few hundred dollars for small stones to over eighteen thousand for the biggest. The darkest region is in the bottom left, below one carat and under five thousand dollars. You can also spot vertical stripes at round sizes like one carat and one and a half carats.
Pause and try it#
Plot mpg: displ on x, hwy on y, coloured by class.
ggplot(mpg, aes(x = displ, y = hwy, colour = class)) +
geom_point(size = 2)
Seven classes, seven colours. SUVs and pickups dominate the bottom right with big engines and poor economy, while compacts and subcompacts sit top left. And look at the 2seater points: big engines between five point seven and seven litres, but surprisingly good highway mileage in the mid twenties. Remember them; we'll come back to them in the analysis section.
Trend lines with geom_smooth()#
ggplot(peng, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(alpha = 0.5) +
geom_smooth(method = "lm", formula = y ~ x)
A blue straight line rises through the cloud, surrounded by a narrow grey band. The band is a confidence interval: our uncertainty about where the true line lies. The line climbs about fifty grams for every extra millimetre of flipper. We'll learn how to get that number exactly in the modelling video.
ggplot(peng, aes(x = bill_length_mm, y = bill_depth_mm)) +
geom_point(aes(colour = species)) +
geom_smooth(method = "lm", formula = y ~ x,
colour = "black")
The single black line slopes gently downwards. Taken at face value, it says longer bills tend to be shallower. The overall correlation is about minus point two three.
ggplot(peng, aes(x = bill_length_mm, y = bill_depth_mm,
colour = species)) +
geom_point() +
geom_smooth(method = "lm", formula = y ~ x)
All three lines now slope upwards! Within every species, longer bills are also deeper bills, with slopes around point two millimetres of depth per millimetre of length. The overall downward line was an illusion created by mixing three groups. This is called Simpson's paradox, and only the grouped plot reveals it.
Distributions: histograms and density plots#
One numeric variable: where are the values, how spread out?
ggplot(peng, aes(x = body_mass_g)) +
geom_histogram()
We get grey bars with most penguins between about three thousand two hundred and four thousand two hundred grams, and a long tail stretching right to six thousand three hundred. The console prints a message: stat bin using bins equals thirty, pick a better value with binwidth. ggplot is nudging us to choose the bins ourselves.
ggplot(peng, aes(x = body_mass_g)) +
geom_histogram(binwidth = 250, fill = "steelblue",
colour = "white")
Much nicer. The tallest bars sit between three thousand two hundred and fifty and four thousand grams, with about forty five penguins each. Then counts fall away, but there's a second, smaller bump between five thousand two hundred and fifty and five thousand seven hundred and fifty grams. That's a hint that there's a heavier group hidden in the data.
ggplot(peng, aes(x = body_mass_g, fill = species)) +
geom_histogram(binwidth = 250, alpha = 0.5,
position = "identity")
There's our heavy group. The blue Gentoo histogram sits on the right, centred around five thousand grams. The red Adelie and green Chinstrap histograms overlap almost completely, around three thousand seven hundred grams.
ggplot(peng, aes(x = body_mass_g, fill = species)) +
geom_density(alpha = 0.4)
Three smooth curves. Adelie and Chinstrap peak at almost the same spot, just under three thousand eight hundred grams, while Gentoo peaks near five thousand. The y axis is now density, not counts. The area under each curve is one, so a small group like Chinstrap gets the same visual weight as a big one.
A skewed distribution: diamond prices#
ggplot(diamonds, aes(x = price)) +
geom_histogram(binwidth = 500, fill = "grey40")
A strongly right skewed shape. The tallest bars are on the far left, below fifteen hundred dollars, and then a long tail stretches all the way to nearly nineteen thousand. With skewed data like this, the mean, about three thousand nine hundred, is pulled well above the median of about two thousand four hundred.
diamonds |>
filter(price < 2500) |>
ggplot(aes(x = price)) +
geom_histogram(binwidth = 25)
Look closely around fifteen hundred dollars: there's a clear gap, a hole in the histogram where almost no diamonds are priced. Wide bins hid it completely. This is why it pays to try several binwidths.
diamonds |>
mutate(band = floor(price / 50) * 50) |>
count(band) |>
filter(between(band, 1350, 1650))
Most bands hold four to six hundred diamonds, but the band starting at fourteen fifty has only twenty, and the one starting at fifteen hundred only forty three. Is it a data collection quirk, or real pricing? We don't know yet, but the chart told us exactly where to dig.
Pause and try it#
Histogram of hwy from mpg with binwidth = 2 and white outlines.
ggplot(mpg, aes(x = hwy)) +
geom_histogram(binwidth = 2, colour = "white")
This one has two humps. One group of cars sits around sixteen to eighteen miles per gallon, the trucks and SUVs, and a second, larger group around twenty six to twenty eight, the smaller cars. A distribution with two peaks is called bimodal, and it usually means two kinds of things are mixed together.
Comparing groups: boxplots and violins#
ggplot(peng, aes(x = species, y = body_mass_g)) +
geom_boxplot()
Three boxes. The thick line in each box is the median: three thousand seven hundred grams for both Adelie and Chinstrap, and five thousand and fifty for Gentoo. The box covers the middle half of the data, from the first to the third quartile. The whiskers reach the furthest points within one and a half box lengths, and anything beyond is drawn as a dot. Only Chinstrap shows outlier dots: one very heavy penguin at four thousand eight hundred grams and one very light one at two thousand seven hundred.
ggplot(peng, aes(x = species, y = body_mass_g, fill = sex)) +
geom_boxplot()
Within every species the male box, in teal, sits clearly higher than the female box, in red. The gap is biggest for Gentoo, where males average almost five and a half kilos and females under four point seven.
set.seed(1)
ggplot(peng, aes(x = species, y = flipper_length_mm)) +
geom_violin(fill = "lightblue") +
geom_jitter(width = 0.1, alpha = 0.4)
Each violin is widest where most penguins are. Adelie bulge around one ninety millimetres, Chinstrap just under two hundred, and Gentoo higher up, around two hundred and fifteen to two hundred and twenty. Layering raw points over a summary shape is a great habit. It shows both the pattern and how much data is behind it.
ggplot(mpg, aes(x = fct_reorder(class, hwy, median),
y = hwy)) +
geom_boxplot(fill = "wheat") +
labs(x = "class")
Now the boxes climb from left to right like a staircase: pickup with a median of seventeen, then suv, minivan, 2seater, subcompact, and finally compact and midsize, both with a median of twenty seven. Ordering by value is one of the simplest ways to make any chart instantly readable.
Bar charts: geom_bar() counts, geom_col() values#
ggplot(peng, aes(x = species)) +
geom_bar()
Three bars. Adelie is tallest at one hundred and forty six penguins, then Gentoo at one hundred and nineteen, and Chinstrap at sixty eight.
ggplot(peng, aes(x = species, fill = island)) +
geom_bar()
The Adelie bar is split into three colours, so Adelie live on all three islands: Biscoe, Dream and Torgersen. Chinstrap is entirely green, only on Dream, and Gentoo is entirely red, only on Biscoe.
ggplot(peng, aes(x = species, fill = island)) +
geom_bar(position = "dodge")
Now the three Adelie bars stand next to each other: forty four on Biscoe, fifty five on Dream and forty seven on Torgersen. Very even. But notice the Chinstrap and Gentoo bars are three times wider. With only one island, each fills the whole space.
ggplot(peng, aes(x = species, fill = island)) +
geom_bar(position = position_dodge(preserve = "single"))
All bars are now the same width. The single Chinstrap and Gentoo bars are now narrow, leaving empty space in their slots, which honestly shows that the other islands are simply missing.
ggplot(peng, aes(x = island, fill = species)) +
geom_bar(position = "fill")
Each bar now reaches one. Biscoe is about three quarters Gentoo and one quarter Adelie, Dream is roughly half Chinstrap and half Adelie, and Torgersen is one hundred percent Adelie. The y axis still says count, though, which is misleading. We'll fix that with a percent scale later.
mass_by_species <- peng |>
group_by(species) |>
summarise(mean_mass = mean(body_mass_g))
mass_by_species
Three rows, one per species, with mean masses of about three thousand seven hundred and six, three thousand seven hundred and thirty three, and five thousand and ninety two grams.
ggplot(mass_by_species, aes(x = species, y = mean_mass)) +
geom_bar()
The error says there was a problem while computing the stat, and in RStudio the full message adds that stat count must only have an x or y aesthetic. geom_bar's counting statistic doesn't know what to do with the y we supplied.
This one is meant to fail, so you can see the error message.
ggplot(mass_by_species, aes(x = species, y = mean_mass)) +
geom_col(fill = "darkseagreen")
Three bars at the mean masses. Adelie and Chinstrap are almost the same height, around three thousand seven hundred, and Gentoo towers above at about five thousand one hundred. So remember: geom_bar when R should count the rows, geom_col when you already have the numbers.
ggplot(mpg, aes(x = fct_infreq(class))) +
geom_bar(fill = "steelblue") +
coord_flip() +
labs(x = NULL, y = "number of cars")
Now the bars run sideways and form a neat ranking. Because the axes are flipped, the most frequent class, suv with sixty two cars, sits at the bottom, followed upwards by compact, midsize, subcompact, pickup, minivan and finally 2seater with only five.
class_counts <- mpg |> count(class, sort = TRUE)
ggplot(class_counts, aes(x = reorder(class, n), y = n)) +
geom_col(fill = "grey50") +
geom_text(aes(label = n), hjust = -0.3) +
coord_flip() +
labs(x = NULL, y = "cars")
Horizontal bars, longest at the top this time, because reorder sorts ascending and the flipped axis puts the last level on top. Each bar has its exact count printed beside it: sixty two, forty seven, forty one, and so on down to five. Labels like these are great on small bar charts, but on busy charts they quickly turn into clutter, so use them sparingly.
Pause and try it#
Show the share of each class within each drv (drive type).
Hint: geom_bar() with position = "fill".
ggplot(mpg, aes(x = drv, fill = class)) +
geom_bar(position = "fill")
Three full height bars. Four wheel drive is mostly SUVs and pickups, front wheel drive is mostly compact and midsize cars with some minivans and subcompacts, and rear wheel drive is a mix of SUVs, subcompacts and all the 2seaters.
Line charts over time: the economics dataset#
head(economics, 4)
range(economics$date)
Each row is one month. There are columns for personal spending, population in thousands, the personal savings rate as a percentage, the median weeks someone stays unemployed, and the number of unemployed people, also in thousands. The data runs from July nineteen sixty seven to April twenty fifteen, almost forty eight years.
ggplot(economics, aes(x = date, y = unemploy)) +
geom_line()
A jagged line with clear waves. Unemployment peaks in nineteen seventy five at about eight point four million, in late nineteen eighty two at about twelve million, then around nineteen ninety two and two thousand and three. The highest peak is in October two thousand and nine, at over fifteen point three million people, right after the financial crisis.
economics |>
mutate(rate = unemploy / pop * 100) |>
ggplot(aes(x = date, y = rate)) +
geom_line(colour = "firebrick", linewidth = 1)
Same waves, but the long upward drift is gone. Now the late nineteen eighty two peak, at about five point two percent of the population, is actually slightly higher than the two thousand and nine peak, just under five percent. Dividing by population turned a misleading trend into a fair comparison across decades.
econ_long <- economics |>
select(date, psavert, uempmed) |>
pivot_longer(-date, names_to = "measure",
values_to = "value")
econ_long |> head(4)
Now each date appears twice: once for psavert and once for uempmed. In July nineteen sixty seven the savings rate was twelve point six percent, and the median unemployment spell was four and a half weeks.
ggplot(econ_long, aes(x = date, y = value,
colour = measure)) +
geom_line()
Two lines with a legend. The red savings rate drifts down from a high of seventeen percent in the mid seventies to a low of about two percent in two thousand and five. The teal line, median weeks unemployed, rises, with a huge spike to twenty five weeks around twenty ten. But they share one y axis, even though one is a percentage and one is weeks.
Facets: small multiples#
facet_wrap(~ var) for one variable, facet_grid(row ~ col) for two.
ggplot(econ_long, aes(x = date, y = value)) +
geom_line() +
facet_wrap(~ measure, ncol = 1, scales = "free_y")
Two panels stacked on top of each other, sharing the date axis. The top panel shows the savings rate on its own scale up to about seventeen, and the bottom shows unemployment duration up to about twenty five weeks. Now each series uses its full height, and we can compare their timing directly: when unemployment duration spikes after two thousand and eight, the savings rate climbs back up as well.
ggplot(peng, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(colour = "steelblue", alpha = 0.7) +
facet_wrap(~ species)
Three side by side panels labelled Adelie, Chinstrap and Gentoo. By default all panels share the same axes, which makes comparison honest: the Gentoo cloud sits clearly higher and further right than the others. Within each panel there's still a positive relationship between flipper length and mass.
ggplot(peng, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(colour = species), show.legend = FALSE) +
facet_grid(sex ~ species)
A grid of six panels: females on the top row and males on the bottom, one column per species. In every column, the male cloud sits higher than the female cloud. With one look we're comparing six groups. That's the power of small multiples.
ggplot(mpg, aes(x = hwy)) +
geom_histogram(binwidth = 2) +
facet_wrap(~ class, nrow = 2)
Seven small histograms over two rows. Pickups and SUVs sit on the left, mostly under twenty miles per gallon, while compact, midsize and subcompact sit on the right in the mid to high twenties. So the two humps we saw were two families of vehicles mixed together, exactly as we guessed.
Pause and try it#
Histogram of flipper_length_mm, one panel per island.
ggplot(peng, aes(x = flipper_length_mm, fill = species)) +
geom_histogram(binwidth = 3) +
facet_wrap(~ island, ncol = 1)
Biscoe shows two groups: red Adelie on the left around one ninety, and many more blue Gentoo on the right above two hundred and ten. That's why Biscoe's mean flipper length is the highest, about two hundred and ten. Dream mixes red Adelie and green Chinstrap in the one eighty to two hundred range, and Torgersen has only Adelie.
Polishing: labels, themes and legends#
Exploration charts can be rough. Charts for others must be clear.
base_plot <- ggplot(peng, aes(x = flipper_length_mm,
y = body_mass_g,
colour = species)) +
geom_point(size = 2, alpha = 0.8)
base_plot
The familiar coloured scatter. Notice the axis titles are raw column names with underscores, flipper length mm and body mass g. Readers shouldn't have to decode those.
labelled_plot <- base_plot +
labs(
title = "Bigger flippers, heavier penguins",
subtitle = "Palmer Archipelago, Antarctica, 2007-2009",
x = "Flipper length (mm)",
y = "Body mass (g)",
colour = "Species",
caption = "Source: palmerpenguins R package"
)
labelled_plot
Now the chart explains itself. The bold message is at the top left, the subtitle sits underneath, the axes have units, the legend says Species with a capital S, and the source appears in small text at the bottom right.
labelled_plot + theme_minimal()
The grey background is gone, leaving a white canvas with light grey grid lines. Clean and modern, this is a popular choice for reports and slides.
labelled_plot + theme_bw(base_size = 14)
A crisp black panel border, a white background, and noticeably bigger text everywhere. Bigger base sizes are great for videos and presentations.
labelled_plot +
theme_minimal(base_size = 13) +
theme(
legend.position = "bottom",
plot.title = element_text(face = "bold"),
panel.grid.minor = element_blank()
)
The legend now sits under the chart in a row, the plot area is wider, the title is bold, and only the major grid lines remain. Fewer lines, less clutter.
labelled_plot +
theme(legend.position = "bottom") +
theme_minimal()
The legend is back on the right! The complete theme came last and reset every setting, wiping out our tweak. So the rule is: complete theme first, then your theme tweaks on top.
theme_set(theme_minimal(base_size = 13))
Nothing prints and nothing is drawn. The new default will show up on our next plot.
Scales: colours, axis labels and log axes#
Every aesthetic has a scale: scale_
labelled_plot +
scale_colour_brewer(palette = "Dark2")
Same points, new colours: Adelie in teal green, Chinstrap in orange and Gentoo in purple. And notice the minimal theme is applied automatically now, thanks to theme set.
ggplot(peng, aes(x = species, fill = island)) +
geom_bar() +
scale_fill_viridis_d()
The island segments are now dark purple for Biscoe, teal for Dream, and bright yellow for Torgersen. Viridis colours also differ in brightness, so they stay distinguishable even without colour.
ggplot(peng, aes(x = island, fill = species)) +
geom_bar(position = "fill") +
scale_y_continuous(labels = scales::percent) +
labs(y = "share of penguins")
The y axis now reads zero percent, twenty five percent, fifty, seventy five and one hundred percent. It's now obvious that Torgersen is one hundred percent Adelie, and Gentoo make up roughly three quarters of Biscoe.
economics |>
mutate(unemployed = unemploy * 1000) |>
ggplot(aes(x = date, y = unemployed)) +
geom_line() +
scale_y_continuous(labels = scales::comma)
The y axis labels are now full numbers with commas, like five million, ten million and fifteen million, written out with thousands separators. Without the comma scale, ggplot would show them in scientific notation, like five e plus zero six, which few readers enjoy.
ggplot(diamonds_small, aes(x = carat, y = price)) +
geom_point(alpha = 0.3) +
scale_x_log10() +
scale_y_log10(labels = scales::comma)
The curved cloud from before becomes an almost straight band rising from bottom left to top right. On log axes, equal steps mean equal multiplication, like going from one thousand to ten thousand dollars. A straight line on log log axes means price grows as a power of carat. The correlation between the logs is about point nine seven, even stronger than the point nine two on the raw scale.
Pause and try it#
Bar chart of mpg drv counts: viridis fill by class,
a clear title, and the legend at the bottom.
ggplot(mpg, aes(x = drv, fill = class)) +
geom_bar() +
scale_fill_viridis_d() +
labs(title = "Front-wheel drive is mostly compacts",
x = "drive type", y = "cars", fill = NULL) +
theme(legend.position = "bottom")
Three stacked bars: front wheel drive is the tallest at one hundred and six cars, four wheel drive is close behind at one hundred and three, and rear wheel drive has only twenty five. The class colours run from dark purple to yellow, with the legend in a row along the bottom.
Coordinates: zooming the right way#
Scale limits DROP data; coord_cartesian() just zooms.
ggplot(peng, aes(x = species, y = body_mass_g)) +
geom_boxplot() +
scale_y_continuous(limits = c(3000, 5000))
The console warns that sixty nine rows were removed, because they fall outside the limits. Look at the Gentoo box: its median dropped from five thousand and fifty to about four thousand seven hundred. We didn't just zoom; we deleted the heaviest Gentoos and recalculated the boxplot. That's a misleading chart.
ggplot(peng, aes(x = species, y = body_mass_g)) +
geom_boxplot() +
coord_cartesian(ylim = c(3000, 5000))
No warning this time. The boxes are computed from all the data, and we just look through a smaller window. The Gentoo median stays at five thousand and fifty, and its box now runs off the top of the panel, which is honest. Rule of thumb: to zoom, use coord cartesian. Use scale limits only when you really mean to exclude values.
Annotations: guide the reader's eye#
peak <- economics |> slice_max(unemploy, n = 1)
peak |> select(date, unemploy)
The peak is October two thousand and nine, with fifteen million three hundred and fifty two thousand people unemployed, stored as fifteen thousand three hundred and fifty two.
ggplot(economics, aes(x = date, y = unemploy / 1000)) +
annotate("rect", xmin = as.Date("2007-12-01"),
xmax = as.Date("2009-06-01"), ymin = -Inf,
ymax = Inf, fill = "firebrick", alpha = 0.15) +
geom_line() +
geom_point(data = peak, colour = "firebrick", size = 3) +
annotate("text", x = as.Date("2003-01-01"), y = 15.8,
label = "Peak: Oct 2009", colour = "firebrick") +
labs(x = NULL, y = "Unemployed (millions)",
title = "US unemployment peaked after the 2008 crisis")
The line now runs from about three million to over fifteen million, labelled in millions. A pale red band highlights the recession period, a red dot marks the October two thousand and nine peak, and red text reading Peak Oct twenty oh nine floats to its left. Notice how geom_point used different data from the main plot. Each layer can have its own data, which is how we highlight special rows.
centres <- peng |>
group_by(species) |>
summarise(flipper_length_mm = mean(flipper_length_mm),
body_mass_g = mean(body_mass_g))
centres
The centres are: Adelie at one hundred and ninety millimetres and three thousand seven hundred and six grams, Chinstrap at one hundred and ninety six and three thousand seven hundred and thirty three, and Gentoo at two hundred and seventeen and five thousand and ninety two.
ggplot(peng, aes(x = flipper_length_mm, y = body_mass_g,
colour = species)) +
geom_point(alpha = 0.4) +
geom_label(data = centres, aes(label = species),
fontface = "bold", show.legend = FALSE) +
theme(legend.position = "none")
Three bold, coloured labels sit right in the middle of their clouds: Adelie and Chinstrap close together lower left, and Gentoo up on the right. Readers no longer need to match colours to a legend. Use geom_label and geom_text sparingly: a few well placed labels help, but labelling every point creates a mess.
Saving plots with ggsave()#
final_plot <- labelled_plot +
geom_smooth(method = "lm", formula = y ~ x, se = FALSE) +
scale_colour_brewer(palette = "Dark2") +
theme(legend.position = "bottom")
final_plot
A polished chart: bold title and subtitle, clear axis titles, three colours from the Dark2 palette with a trend line for each species, and the legend underneath. Every species line rises from left to right.
ggsave("penguins_flipper_mass.png", plot = final_plot,
width = 8, height = 5, dpi = 300)
file.exists("penguins_flipper_mass.png")
The console prints TRUE. The image is saved in the working folder, and you can see it in the Files pane. Without the plot argument, ggsave saves the last plot that was displayed, which is easy to get wrong, so passing the plot is safer.
A full EDA workflow on penguins#
Shape 2. Missing values 3. Distributions
Relationships 5. Groups 6. Outliers
dim(penguins)
glimpse(penguins)
Three hundred and forty four rows and eight columns. Species, island and sex are factors, the bill measurements are doubles, flipper length, body mass and year are integers, and we can already see an NA in the fourth row.
colSums(is.na(penguins))
round(colMeans(is.na(penguins)) * 100, 1)
The four body measurements each have two missing values, which is just point six percent. Sex has eleven missing values, about three point two percent. Species, island and year are complete.
penguins |>
filter(is.na(sex)) |>
select(species, island, bill_length_mm, body_mass_g)
Eleven rows. Two of them, an Adelie on Torgersen and a Gentoo on Biscoe, have no measurements at all, so they're useless and safe to drop. The other nine have all their measurements, just no sex. So it's reasonable to keep them for species comparisons, and drop them only when sex matters. That's the kind of decision E D A helps you make deliberately.
peng |>
select(species, bill_length_mm:body_mass_g) |>
pivot_longer(-species, names_to = "measure") |>
ggplot(aes(x = value, fill = species)) +
geom_histogram(bins = 25) +
facet_wrap(~ measure, scales = "free")
Four panels in one picture. Body mass and flipper length both show the Gentoo group on the right. Bill depth is the opposite: the blue Gentoo sit on the left with shallow bills. And in bill length, Adelie stand alone on the left, while Chinstrap and Gentoo overlap. Each measure separates the species in a different way.
peng |>
select(bill_len = bill_length_mm, bill_dep = bill_depth_mm,
flipper = flipper_length_mm, mass = body_mass_g) |>
cor() |>
round(2)
A four by four matrix, with ones on the diagonal, since every variable correlates perfectly with itself. The strongest relationship is flipper with mass, at point eight seven. Bill depth correlates negatively with everything, for example minus point two three with bill length. But remember Simpson's paradox: within each species, that relationship is positive. Correlations across mixed groups can mislead.
peng |>
group_by(species, sex) |>
summarise(
n = n(),
mass_g = round(mean(body_mass_g)),
flipper_mm = round(mean(flipper_length_mm)),
.groups = "drop"
)
Six rows. In every species males are heavier: Adelie males average four thousand and forty three grams versus three thousand three hundred and sixty nine for females, and Gentoo males five thousand four hundred and eighty five versus four thousand six hundred and eighty. Males also have slightly longer flippers in every species. Group sizes are well balanced, for example seventy three female and seventy three male Adelie.
Outliers with the IQR rule#
Outlier if below Q1 - 1.5 * IQR or above Q3 + 1.5 * IQR.
q <- quantile(peng$body_mass_g, c(0.25, 0.75))
iqr <- IQR(peng$body_mass_g)
c(lower = unname(q[1] - 1.5 * iqr),
upper = unname(q[2] + 1.5 * iqr))
range(peng$body_mass_g)
The lower fence is one thousand seven hundred and twelve and a half grams, and the upper fence six thousand six hundred and twelve and a half. The real masses run from two thousand seven hundred to six thousand three hundred, so across all penguins there are no outliers at all.
mass_outliers <- peng |>
group_by(species) |>
mutate(
q1 = quantile(body_mass_g, 0.25),
q3 = quantile(body_mass_g, 0.75),
is_outlier = body_mass_g < q1 - 1.5 * (q3 - q1) |
body_mass_g > q3 + 1.5 * (q3 - q1)
) |>
ungroup() |>
filter(is_outlier)
mass_outliers |> select(species, island, sex, body_mass_g)
Two outliers, both Chinstrap on Dream island: a male at four thousand eight hundred grams and a female at only two thousand seven hundred. These are exactly the two dots we saw on the Chinstrap boxplot earlier. Overall they look normal, but within their species they stand out.
set.seed(7)
ggplot(peng, aes(x = species, y = body_mass_g)) +
geom_jitter(width = 0.15, alpha = 0.3) +
geom_point(data = mass_outliers, colour = "red",
size = 4, shape = 21, stroke = 1.5) +
labs(title = "Two Chinstrap penguins are unusual",
x = NULL, y = "Body mass (g)")
Three faint columns of points, and two red circles in the Chinstrap column, one at the very top near four thousand eight hundred and one at the very bottom at two thousand seven hundred. The rings sit at the exact spots, not jittered, so they may sit slightly beside the faint point for the same penguin.
Pause and try it#
Find flipper_length_mm outliers within each species.
peng |>
group_by(species) |>
filter(
flipper_length_mm < quantile(flipper_length_mm, 0.25) -
1.5 * IQR(flipper_length_mm) |
flipper_length_mm > quantile(flipper_length_mm, 0.75) +
1.5 * IQR(flipper_length_mm)
) |>
select(species, sex, flipper_length_mm)
Two outliers, both Adelie: a female with a very short flipper of one hundred and seventy two millimetres, and a male with a long one of two hundred and ten. The output is still grouped by species, which the tibble header tells us.
Quick EDA on mpg: who breaks the pattern?#
mpg |>
filter(displ > 5, hwy > 22) |>
select(manufacturer, model, year, displ, hwy, class)
Six cars: five Chevrolet Corvettes with engines from five point seven to seven litres, and one Pontiac Grand Prix. The Corvettes are light sports cars, so they're efficient for their engine size.
two_seaters <- mpg |> filter(class == "2seater")
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(colour = "grey60") +
geom_point(data = two_seaters, colour = "firebrick",
size = 3) +
annotate("text", x = 6.2, y = 30, label = "Corvettes",
colour = "firebrick", fontface = "bold") +
labs(x = "Engine size (litres)", y = "Highway mpg",
title = "Sports cars beat the big-engine trend")
Grey points fall from top left to bottom right, and the five red Corvette points stand out on the right, around twenty three to twenty six miles per gallon, well above the trucks and SUVs with similar engines. The bold label Corvettes sits just above them. Grey for context and one highlight colour for the story is one of the most effective chart designs there is.
Quick EDA on diamonds: a price paradox#
ggplot(diamonds, aes(x = cut, y = price)) +
geom_boxplot(fill = "lightblue") +
scale_y_log10(labels = scales::dollar)
Surprise: the Ideal box, the best cut, has the lowest median price of all five. Fair and Premium sit highest. Better cut, lower price? That can't be the whole story.
diamonds |>
group_by(cut) |>
summarise(n = n(),
median_price = median(price),
median_carat = median(carat))
There's the explanation. Ideal diamonds have a median price of eighteen hundred and ten dollars, but also the smallest median size, just point five four carats. Fair diamonds are the biggest, at one full carat, with a median price of three thousand two hundred and eighty two. Ideal is also by far the most common cut, with over twenty one thousand diamonds.
diamonds |>
filter(between(carat, 0.9, 1.1)) |>
group_by(cut) |>
summarise(n = n(), median_price = median(price))
Now the paradox disappears. For diamonds of about one carat, prices climb with cut quality: three thousand seven hundred and thirty dollars for Fair, about forty seven hundred for Very Good and Premium, which are nearly tied, and five thousand two hundred and twenty for Ideal. Comparing groups without controlling for a key variable like size can completely reverse the conclusion.
diamonds |> filter(x == 0 | y == 0 | z == 0) |> nrow()
diamonds |>
filter(y > 20 | z > 20) |>
select(carat, price, x, y, z)
Twenty diamonds have a zero dimension, which is impossible, so those are data entry errors. And three diamonds are bigger than twenty millimetres: one with a width of almost fifty nine millimetres, which is over five centimetres for a two carat stone. These are recording errors, not real diamonds. In a real project we'd set them to missing or remove them before modelling.
Storytelling: from EDA to three clear findings#
Finding 1: Gentoo are the heaviest, about 5.1 kg vs 3.7 kg.
Finding 2: Flipper length tracks body mass closely (r = 0.87).
Finding 3: Bill length vs depth reverses when split by species.
Each finding = one sentence + one chart + one number.
story_plot <- ggplot(peng, aes(x = bill_length_mm,
y = bill_depth_mm)) +
geom_smooth(method = "lm", formula = y ~ x,
colour = "grey40", linetype = "dashed",
se = FALSE) +
geom_point(aes(colour = species), alpha = 0.6) +
geom_smooth(aes(colour = species), method = "lm",
formula = y ~ x, se = FALSE) +
scale_colour_brewer(palette = "Dark2") +
labs(title = "The overall trend hides the real pattern",
subtitle = "Dashed: all penguins. Coloured: each species",
x = "Bill length (mm)", y = "Bill depth (mm)",
colour = NULL) +
theme(legend.position = "top")
story_plot
The dashed grey line slopes gently down across the whole chart, while the three coloured lines, green for Adelie, orange for Chinstrap and purple for Gentoo, all slope upwards. The contradiction is visible in a single glance. That's a chart that tells a story: the title states the finding, the subtitle explains how to read it, and the design does the rest.
ggsave("finding3_simpsons_paradox.png", plot = story_plot,
width = 8, height = 5, dpi = 300)
list.files(pattern = "png$")
Two p n g files are listed: our new Simpson's paradox chart, and the flipper chart from earlier.
Recap#
Grammar: data + aes() + geoms, layered with +.
Geoms: point, smooth, histogram, density, boxplot, bar, col, line.
Polish: facets, labs(), themes, scales, coords, annotate, ggsave().
EDA: shape, missing, distributions, relationships, groups, outliers.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



