Appendix C: Interactive Choroploth Maps

Table of content for chapter 04

Chapter section list

In this section I am trying to generate interactive maps with the help of the {plotly} package. I will follow the article Choropleth Maps in R from the plotly website.

C.1 Main parameters

Making choropleth maps with {plotly} requires two main types of input:

  1. Geometry information: This can either be a supplied GeoJSON file where each feature has either an id field or some identifying value in properties; or one of the built-in geometries within plot_ly: US states and world countries (see below)
  2. A list of values indexed by feature identifier.

Code Collection C.1 : Load input data for choropleth map

R Code C.1 : Load geometry information

Code
URL_geoJSON = "https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json"
counties <- rjson::fromJSON(file = URL_geoJSON)

counties$features[[1]]
#> $type
#> [1] "Feature"
#> 
#> $properties
#> $properties$GEO_ID
#> [1] "0500000US01001"
#> 
#> $properties$STATE
#> [1] "01"
#> 
#> $properties$COUNTY
#> [1] "001"
#> 
#> $properties$NAME
#> [1] "Autauga"
#> 
#> $properties$LSAD
#> [1] "County"
#> 
#> $properties$CENSUSAREA
#> [1] 594.436
#> 
#> 
#> $geometry
#> $geometry$type
#> [1] "Polygon"
#> 
#> $geometry$coordinates
#> $geometry$coordinates[[1]]
#> $geometry$coordinates[[1]][[1]]
#> [1] -86.49677  32.34444
#> 
#> $geometry$coordinates[[1]][[2]]
#> [1] -86.71790  32.40281
#> 
#> $geometry$coordinates[[1]][[3]]
#> [1] -86.81491  32.34080
#> 
#> $geometry$coordinates[[1]][[4]]
#> [1] -86.89058  32.50297
#> 
#> $geometry$coordinates[[1]][[5]]
#> [1] -86.91760  32.66417
#> 
#> $geometry$coordinates[[1]][[6]]
#> [1] -86.71339  32.66173
#> 
#> $geometry$coordinates[[1]][[7]]
#> [1] -86.71422  32.70569
#> 
#> $geometry$coordinates[[1]][[8]]
#> [1] -86.41312  32.70739
#> 
#> $geometry$coordinates[[1]][[9]]
#> [1] -86.41117  32.40994
#> 
#> $geometry$coordinates[[1]][[10]]
#> [1] -86.49677  32.34444
#> 
#> 
#> 
#> 
#> $id
#> [1] "01001"

The GeoJSON file contains the geometry information for US counties, and has as feature ID the FIPS code in the column feature.id.

R Code C.2 : Load unemployment data by county, also indexed by FIPS code.

Code
URL_data = "https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv"
df <- readr::read_csv(URL_data,
                      show_col_types = FALSE)

utils::head(df)
#> # A tibble: 6 × 2
#>   fips  unemp
#>   <chr> <dbl>
#> 1 01001   5.3
#> 2 01003   5.4
#> 3 01005   8.6
#> 4 01007   6.6
#> 5 01009   5.5
#> 6 01011   7.2

C.2 Choropleth map using GeoJSON

R Code C.3 : Choropleth map using GeoJSON

Code
g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = plotly::toRGB('white')
)

fig <- plotly::plot_ly() |> 
    plotly::add_trace(
        type = "choropleth",
        geojson = counties,
        locations = df$fips,
        z = df$unemp,
        colorscale = "Viridis",
        zmin = 0,
        zmax = 12,
        marker = list(line = list(width = 0))
        ) |> 
    plotly::colorbar(title = "Unemployment Rate (%)") |> 
    plotly::layout(
        title = "2016 US Unemployment by County",
        geo = g
    )

fig
Graph C.1: Example for a choropleth map using a GeoJSON file

In this example we set layout.geo.scope to usa to automatically configure the map to display USA-centric data in an appropriate projection.