This post explains how to build a choropleth map with R. It shows how to load geospatial data, bind it to a numeric value and draw the map.
Two inputs are needed to build a choropleth map:
Note: this step is described in detail here. Read it if you are not familiar with geospatial data management in R.
The region boundaries required to make maps are usually stored in geospatial objects. Those objects can come from shapefiles, geojson files or provided in a R package. See the map section for possibilities.
Let’s get a geospatial object from a shape file available here.
# Download the shapefile. (note that I store it in a folder called DATA. You have to change that if needed.)
download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip", destfile = "DATA/world_shape_file.zip")
# You now have it in your current working directory, have a look!
# Unzip this file. You can do it with R (as below), or clicking on the object you downloaded.
system("unzip DATA/world_shape_file.zip")
# -- > You now have 4 files. One of these files is a .shp file! (TM_WORLD_BORDERS_SIMPL-0.3.shp)
And let’s load it in R
# Read this shape file with the sf library.
library(sf)
<- read_sf(
my_sf paste0(getwd(), "/DATA/world_shape_file/TM_WORLD_BORDERS_SIMPL-0.3.shp")
)
# Select Africa only
<- my_sf[my_sf$REGION == 2, ]
africa
# -- > Now you have a sf object (simple feature data frame). You can start doing maps!
The hardest part is to build the color palette that will be used
on the map. The RColorBrewer
package makes it a
breeze to get a nice set of colors thanks to its
brewer.pal()
function.
Once the palette is created, POP2005
is cut in
several bins using the cut()
function, and the
appropriate color is attributed to each bin.
Finally, build the map with plot()
and give the color
vector to the col
argument to get the choropleth.
# Plot
plot(st_geometry(africa), xlim = c(-20, 60), ylim = c(-40, 40))
In a choropleth map, each region has a color that represents the value of a numeric variable.
In this example, this variable is already available in the
africa
object. You can have a look to it typing
africa
in the terminal.
It is thus possible to visualize the distribution of
POP2005
with a classic histogram made in
ggplot2
# library
library(dplyr)
library(ggplot2)
# Make sure the variable you are studying is numeric
$POP2005 <- as.numeric(africa$POP2005)
africa
# Distribution of the population per country?
%>%
africa ggplot(aes(x = POP2005)) +
geom_histogram(bins = 20, fill = "#69b3a2", color = "white")
The hardest part is to build the color palette that will be used
on the map. The RColorBrewer
package makes it a
breeze to get a nice set of colors thanks to its
brewer.pal()
function.
Once the palette is created, POP2005
is cut in
several bins using the cut()
function, and the
appropriate color is attributed to each bin.
Finally, build the map with plot()
and give the color
vector to the col
argument to get the choropleth.
# Palette of 30 colors
library(RColorBrewer)
<- brewer.pal(9, "Reds")
my_colors <- colorRampPalette(my_colors)(30)
my_colors
# Attribute the appropriate color to each country
<- cut(africa$POP2005, 30)
class_of_country <- my_colors[as.numeric(class_of_country)]
my_colors
# Make the plot
plot(st_geometry(africa),
xlim = c(-20, 60), ylim = c(-40, 40), col = my_colors,
bg = "#A6CAE0"
)
👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥