This document will show how to create a choropleth map with pop-ups using RStudio’s Leaflet package for R. The map will display GDP per capita in thousands of pesos for Mexican states in 2008. Spatial data were originally downloaded from Natural Earth, and the demographic data come from INEGI, indicador 3116001002. The data are in the form of a zipped shapefile that can be read in to R with the rgdal package.

To get started, let’s load the required packages, download the data, and take a look at it.

library(rgdal)
library(leaflet)

tmp <- tempdir()

url <- "http://personal.tcu.edu/kylewalker/data/mexico.zip"

file <- basename(url)

download.file(url, file)

unzip(file, exdir = tmp)

mexico <- readOGR(dsn = tmp, layer = "mexico", encoding = "UTF-8")
## OGR data source with driver: ESRI Shapefile 
## Source: "C:\Users\KYLEWA~1\AppData\Local\Temp\RtmpMXziLj", layer: "mexico"
## with 32 features
## It has 9 fields
head(mexico@data)
##   id                name               state mus09 pri10 sec10 ter10
## 0  1      Aguascalientes      Aguascalientes  17.1  6.34 28.74 64.28
## 1  2     Baja California     Baja California  15.4  5.44 31.64 61.68
## 2  3 Baja California Sur Baja California Sur   3.0  9.18 17.48 72.37
## 3  4            Campeche            Campeche   7.0 19.24 21.20 57.11
## 4  5             Chiapas             Chiapas  13.9 42.76 13.51 42.90
## 5  6           Chihuahua           Chihuahua  15.2 10.22 34.16 53.92
##    gdp08 other10
## 0  84.70    0.64
## 1  83.07    1.24
## 2  94.64    0.97
## 3 395.55    2.45
## 4  34.44    0.83
## 5  85.10    1.70

The dataset contains a number of different variables; if you are curious, mus09 is the out-migration rate to the United States per 1000 residents for the period 2004-2009 (Indicador 3104004001), and pri10, sec10, ter10, and other10 (Indicadores 3103004001, 3103004002, 3103004003) reflect the distribution of the state’s workforce across its primary, secondary, tertiary, and other sectors, respectively. The variable we are interested in mapping is gdp08, which is per capita gross domestic product in thousands of pesos for 2008 (in 2003 pesos).

With the Leaflet package, creating a functional interactive map takes just a few lines of R code. Notice that I first define a ColorBrewer color ramp and means of classifying my data using the built-in colorQuantile function. I then define the parameters of my html pop-up. I then call the leaflet function, pull in tiles from CartoDB, and then style the polygons within the addPolygons function.

pal <- colorQuantile("YlGn", NULL, n = 5)

state_popup <- paste0("<strong>Estado: </strong>", 
                      mexico$name, 
                      "<br><strong>PIB per c?pita, miles de pesos, 2008: </strong>", 
                      mexico$gdp08)

leaflet(data = mexico) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(fillColor = ~pal(gdp08), 
              fillOpacity = 0.8, 
              color = "#BDBDC3", 
              weight = 1, 
              popup = state_popup)