Making interactive maps with {rdeck}

mapping
interactivity

Run the following code in your course or project repository to download the Making interactive maps with {rdeck} starter to your current working directory:

usethis::use_github_file("bldgspatialdata/starters", "rdeck-interactive-map.qmd")

Setup

Using this template requires the {rdeck} package which can be installed from GitHub:

pak::install_github("qfes/rdeck@*release")
Sign up for a Mapbox account and get a token

A Mapbox account and mapbox access token is required for Mapbox basemaps, with or without the Mapbox data service. See the {rdeck} documentation on mapbox_access_token for more information.

The template also uses the {tidycensus} and {sf} packages.

library(rdeck)
library(sf)
library(tidycensus)

options(tigris_use_cache = TRUE)

To customize this starter template, try:

  • Changing the state or geography
  • Changing the variables
  • Changing the values per dot for the dot density map

Map

This template includes code cells downloading and preparing the data, a code cell for creating a rdeck map instance, and two example map code cell you can adapt. The initial code cell also uses the output: false to hide messages from the get_acs() function.

Set parameters for data and maps
state <- "MD"

population_table <-  "B01003"

race_variables <- c(
    "White" = "B03002_003",
    "Black" = "B03002_004",
    "Latino" = "B03002_012"
  )

values_per_dot <- 100
Download ACS data and prepare data for mapping
# Download data on population by county
population <- get_acs("county",
  state = state,
  table = population_table,
  geometry = TRUE
)

# Transform the data into a geodetic CRS (required by all rdeck layers)
population <- st_transform(population, 4326)

# Rename the geometry column (required by add_polygon_layer())
population <- st_set_geometry(population, "polygon")

# Download data on race/ethnic group population by county
race <- get_acs("county",
  state = state,
  variables = race_variables,
  geometry = TRUE
)

# Convert data into a dot density format
race_dot_density <- race |>
  as_dot_density(
    value = "estimate",
    values_per_dot = values_per_dot,
    group = "variable"
  )

# Transform the data into a geodetic CRS and rename the geometry column (required by all rdeck layers)
race_dot_density <- st_transform(race_dot_density, 4326)

# Rename the geometry column (required by add_scatterplot_layer())
race_dot_density <- st_set_geometry(race_dot_density, "position")
Create light and dark rdeck map instances
# Create a rdeck map instance using the Mapbox light and Mapbox dark themes
rdeck_light <- rdeck(
  map_style = mapbox_light(),
  theme = "kepler",
  initial_bounds = st_bbox(population),
  height = 600
)

rdeck_dark <- rdeck(
  map_style = mapbox_dark(),
  theme = "kepler",
  initial_bounds = st_bbox(population),
  height = 600
)

Both map code cells use the column: screen-inset-shaded option to make the map full width in an HTML output format. Learn more about how to modify the article layout in a Quarto document.

This first map uses the rdeck::add_polygon_layer() and rdeck::scale_color_linear(). Setting pickable = TRUE allows you to click on a polygon and see the fields supplied to the tooltip parameter.

Create a polygon layer map
rdeck_light |>
  add_polygon_layer(
    name = "Residents by county",
    data = population,
    opacity = 0.6,
    get_fill_color = scale_color_linear(
      estimate,
      col_label = "Population"
      ),
    tooltip = c(NAME, GEOID, estimate),
    pickable = TRUE
  )

This second map uses the rdeck::add_scatterplot_layer() and rdeck::scale_color_category(). It also uses a custom palette for qualitative data created with the scales::brewer_pal() function from the {scales} package.

Create a scatterplot layer map
rdeck_dark |>
  add_scatterplot_layer(
    name = "Population by race/ethnicity (100 per dot)",
    data = race_dot_density,
    radius_min_pixels = 1,
    radius_max_pixels = 5,
    radius_scale = 5,
    opacity = 0.6,
    get_fill_color = scale_color_category(
      variable,
      palette = scales::brewer_pal("qual"),
      col_label = "Race/ethnicity"
      )
  )