Lecture 3 · Advanced R and mapping
When a spreadsheet becomes a map
We’ll map the same North Carolina counties using counts and rates and see what each map can tell us.
Hi everyone! We’re doing tables and maps in the same lecture, so I know there’s a lot of code here. Let’s follow one example. We’ll look at births and deaths in North Carolina, then divide by the number of births and see what changes on the map.
Check your table before you make the map
We joined world-city records to country information and put the population years into a single column. After a join, I always check how many rows I get back and what one row means. If those numbers look wrong, we should check the join before building the map.
Compare the same counties two ways
Our historical example uses North Carolina births and deaths from July 1974 to June 1978. One map shows counts. The other shows how many babies died from sudden infant death syndrome (SIDS) for every 1,000 births. When we move from mapping to drawing a 50-km buffer, we also check that the data are projected in metres.
In R
This web example follows the lecture’s count-to-rate calculation using the built-in dataset from sf.
library(sf)
library(ggplot2)
nc <- st_read(system.file("shape/nc.shp", package = "sf"),
quiet = TRUE)
nc <- st_transform(nc, 32119)
nc$sids_rate <- 1000 * nc$SID74 / nc$BIR74
ggplot(nc) +
geom_sf(aes(fill = sids_rate)) +
labs(fill = "Deaths per 1,000 births")
The “74” variables cover July 1974 through June 1978. Both numerator and denominator use the same time window.
Key takeaways
- A count tells you how many. A rate lets you compare places of different sizes.
- Check your join key, map legend and distance units before telling the story.


