How to make heatmaps with R using ggplot2 and leaflet

We’re often asked by clients to make maps, and oftentimes they ask us to make heatmaps.

For example, we made heatmaps with {ggplot2} in these reports on 2020 U.S. Census outreach efforts.

And we made interactive heatmaps with the {leaflet} package in this map for the Asian Pacific Islander Council.

Also known as choropleth maps, heatmaps are incredibly useful and easy to understand maps. At their simplest, heatmaps require two things:

  • Shapefiles for the regions of interest. Your regions might be countries, states or other country subdivisions like census tracts.
  • A value for each region of interest. This could be a numeric value (like population) or it could be a categorical variable like the most popular streaming service in a state.

I really love mapping with R because it’s fairly simple to start off and we can build really beautiful static and interactive maps. Our Mapping with R course will take you through all the steps required to build choropleth. But let me take you through the basic steps so you know what’s involved.

Shapefiles and the {sf} package

In the video below I’m going to introduce the {sf} package which is the backbone of most mapping things you’ll do with R. I’ll use the {tidycensus} package to obtain the population for all US states from the US Census. The great thing about {sf} datasets is can use them inside of a tidyverse workflow to extract just the contiguous United States.

Here’s the code that I wrote in the video:

If you’re working with other regions you’ll need to obtain shapefiles and read them into R using read_sf() from {sf}, which I cover in the introduction to Mapping with R.

Quickly view data with {mapview}

It’s absolutely crucial when working with map data to be able to see your data (the map) – and that’s exactly what the {mapview} package is for. It will take any geospatial dataset, whether it’s from {sf}, {sp}, {raster}, {stars} and many others and … just visualise it.

Here’s the map made with {mapview}.

And here’s all the code necessary to make this heatmap with {mapview}.

This is literally as far as I ever go with {mapview}. It’s job is to quickly visualise your map data, and that’s it. My next step would be to decide between making a static map with {ggplot2} and an interactive one with {leaflet}.

How to make heatmaps with {ggplot2}

The {ggplot2} package is an incredibly powerful data visualisation tool that is able to visualise {sf} datasets. This means that we can create choropleth (and other!) maps using our existing {ggplot2} knowledge. In the video below I’ll show how to make this map.

How to make interactive heatmaps with {leaflet}

{ggplot2} is fine if you’re making a map you need to print. But if you’re going to publish your map to the web… why not make an interactive heatmap with the {leaflet} package?

It takes a little bit more effort to create a {leaflet} map as I needed to create a user-defined function for the popup, but it’s worth it. Interactivity allows your readers to explore and really understand what’s shown in the heatmap.

Here’s the exact code I showed in the video.

Going further with heatmaps

This is just the start of what you can do when making heatmaps in R. You can also use heatmaps in combination with small multiples. Small multiples is an excellent data visualisation technique for comparing how a variable varies across a range of categories. It’s particularly powerful when working with heatmaps, as shown in the map below (and associated code) which I built with {ggplot2} and show you how to build in our Mapping with R course.

Ready to make heatmaps with R?

Inspired to start making your own heatmaps with R? Check out Mapping with R. It will teach you to make heatmaps with {mapview}, {ggplot2}, and {leaflet}. Get started today!

Want articles like this in your email? Sign up for the R for the Rest of Us newsletter.

Have any questions? Put them below. David or Charlie Hadley will help you out!