Basic choropleth map with the Cartography package



The cartography package is a great alternative to make maps with R. This post explains how to build a basic choropleth map using it, with explanation and reproducible code.

Choropleth section About Maps

Data provided in the library


The cartography package is a very good alternative to the other tools presented in the gallery for building maps.

It notably allows to:

  • display a legend with embedded circles
  • add data sources on the bottom of the chart
  • show north / south

# Use the cartography library to do the choropleth map
library(cartography)

# Load data
data(nuts2006)

# Build a choropleth
choroLayer(spdf = nuts2.spdf, df = nuts2.df, var = "pop2008" , legend.pos = "right")
title("Population in 2008")

Data provided in a shape file


Warning! The rgdal package is deprecated. You should use the sf package instead.

The cartography package is a very good alternative to the other tools presented in the gallery for building maps.

It notably allows to:

  • display a legend with embedded circles
  • add data sources on the bottom of the chart
  • show north / south

# Download the shape file from the web and unzip it:
# download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip" , destfile="world_shape_file.zip")
# system("unzip world_shape_file.zip")

# Load it as a geospatial object in R
library(rgdal)
my_spdf <- readOGR( dsn= "/Users/yan.holtz/Desktop/R-graph-gallery/world_shape_file/" , layer="TM_WORLD_BORDERS_SIMPL-0.3", verbose=FALSE) 
africa <- my_spdf[my_spdf@data$REGION==2 , ]

africa@data$POP2005 <- as.numeric(africa@data$POP2005)

# Use the cartography library to do the choropleth map
library(cartography)
choroLayer(spdf = africa, df = africa@data, var = "POP2005")
title("Number of people living in Africa in 2005")

Related chart types


Map
Choropleth
Hexbin map
Cartogram
Connection
Bubble map



❤️ 10 best R tricks ❤️

👋 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! 🔥