Black Lives Matter. Please consider donating to Black Girls Code today.
https://www.blackgirlscode.com/

Mapbox Choropleth Maps in R

How to make a Mapbox Choropleth Map of US Counties in R with Plotly.


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

A Choropleth Map is a map composed of colored polygons. It is used to represent spatial variations of a quantity. This page documents how to build tile-map choropleth maps, but you can also build outline choropleth maps using our non-Mapbox trace types.

Below we show how to create Choropleth Maps using Plotly Choroplethmapbox graph object.

Mapbox Access Token and Base Map Configuration

To plot on Mapbox maps with Plotly you may need a Mapbox account and a public Mapbox Access Token. See our Mapbox Map Layers documentation for more information. If you're using a Chart Studio Enterprise server, please see additional instructions here.

Introduction: main parameters for choropleth tile maps

Making choropleth Mapbox maps requires two main types of input:

  1. GeoJSON-formatted geometry information where each feature has either an id field or some identifying value in properties.
  2. A list of values indexed by feature identifier.

The GeoJSON data is passed to the geojson argument, and the data is passed into the z argument of plot_geo.

Note the geojson attribute can also be the URL to a GeoJSON file, which can speed up map rendering in certain cases.

GeoJSON with feature.id

Here we load a GeoJSON file containing the geometry information for US counties, where feature.id is a FIPS code.

library(rjson)
url = 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
json_file <- rjson::fromJSON(file=url)
json_file$features[[1]]$id
## [1] "01001"

Data indexed by id

Here we load unemployment data by county, also indexed by FIPS code.

df = read.csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", header = T, colClasses = c("fips"="character"))
head(df)
##    fips unemp
## 1 01001   5.3
## 2 01003   5.4
## 3 01005   8.6
## 4 01007   6.6
## 5 01009   5.5
## 6 01011   7.2

Mapbox Choropleth Map Using GeoJSON

With choroplethmapbox, each row of the DataFrame is represented as a region of the choropleth.

library(rjson)
library(plotly)

url <- 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
counties <- rjson::fromJSON(file=url)
url2<- "https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv"
df <- read.csv(url2, colClasses=c(fips="character"))
fig <- plot_ly() 
fig <- fig %>% add_trace(
    type="choroplethmapbox",
    geojson=counties,
    locations=df$fips,
    z=df$unemp,
    colorscale="Viridis",
    zmin=0,
    zmax=12,
    marker=list(line=list(
      width=0),
      opacity=0.5
    )
  )
fig <- fig %>% layout(
    mapbox=list(
      style="carto-positron",
      zoom =2,
      center=list(lon= -95.71, lat=37.09))
  )
fig