This posts describes how to make this hexbin map of the United States with R. Each state is represented by an hexagone, and the color represents the number of weeding per 1000 people. In this sense, it is a kind of chloropleth map.

The first thing we need is a geospatial object that gives the boundaries of each hexagone. Fortunately this file is available on this website. The page #325 explains how to read a geoJSON file with R, so I just remember here the line of codes:


OK we have our geospatial object! We can now plot it with ggplot2. I just add a little trick to add the state code on each hexbin, which request to calculate the centroid of each bin..

Sweet! Now you probably want to adjust the color of each hexagon, following the value of a specific variable (we call it a chloropleth map). In this post I propose to represent the number of wedding per thousand people. The data have been found here, and stored in the gallery. Let’s start by loading this information and represent its distribution:


Hmm, interesting, it looks like most of the state have between 5 and 10 weddings / 1000 inhabitants. But we have 2 outliers with high values (16 and 32). Let’s represent this information on a map. We have a column with the state id in both the geospatial and the numerical datasets. So we can merge both information and plot it:

This is not too bad but quite disappointing, our outlier state with 30 wedding / 1000 inhabitants absorbs all the variation on the rest of the map. Here I propose to use binning to fix this issue. We also need to add a title, make a better color legend, add text and use a better color palette:

Here we are:


I would like to thanks Bob Rudis for this post and the team carto for providing the hexbin boundaries. I also used this awesome post as inspiration.




Leave a Reply

Notify of