This page shows how to make a choropleth map with ggplot2 from geoJSON data. To make a choropleth map you need two inputs:

  • A geospatial object that provides the boundaries of the geographic units that interest you. Here, we will consider the city districts of the south of France. Data are available at the geoJSON format. Data come from here, and this post explain in detail how to read and represent geoJSON format with R.
  • A numerical variable that we use to color each geographical unit. Here we will use the number of restaurant per city. The data has been found here. (Thanks @joelgombin).

 

Step 1: Read the geoJSON object


 

This step has been extensively describe in the chart #325. The geojsonio library allows to read this type of format in R. To plot it with ggplot2, we first need to transform it to a data frame using the tidy function of the broom library. Then, the geom_polygon() function allows to represent this type of object !

 

 

Step 2: Read the numerical variable


 

The number of restaurant per city district has been found on the internet and a clean version is stored on the gallery website. It is thus easy to read it with read.table. Before doing a choropleth map, it is a good practice to check the distribution of your variable.

 

Here, we have a ‘long tail’ distribution: a few cities have a lot of restaurant. Thus we will probably need to apply a log scale to our color palette. It will avoid that all the variation is absorbed by these high values.

 

 

 

Step 3: Merge them together


This is a key step in choropleth map: your 2 inputs must have a id in common to make the link between them!

 

Step 4: Make a basic choropleth


 

 

 

We can make a first basic choropleth map. We just need to add fill = our value in the aesthetic of our polygons.

 

 

 

 

Step 5: improve this choropleth


There is a bit of work to do to get a descent figure. We need to change the color palette, improve the legend, use a log scale transformation for the colorscale, change background and add titles and explanation. Here is the code to do that, and the final result!

 

Another great example made with ggplot2 from geoJSON data is this Swiss map made by Timo Grossenbacher.

 

Related



2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Holtzawkates Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
awkates
Guest
awkates

What is the command to add the outlines between polygons?