::use_github_file("bldgspatialdata/starters", "rdeck-interactive-map.qmd") usethis
Making interactive maps with {rdeck}
Run the following code in your course or project repository to download the Making interactive maps with {rdeck}
starter to your current working directory:
Setup
Using this template requires the {rdeck}
package which can be installed from GitHub:
::install_github("qfes/rdeck@*release") pak
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
<- "MD"
state
<- "B01003"
population_table
<- c(
race_variables "White" = "B03002_003",
"Black" = "B03002_004",
"Latino" = "B03002_012"
)
<- 100 values_per_dot
Download ACS data and prepare data for mapping
# Download data on population by county
<- get_acs("county",
population state = state,
table = population_table,
geometry = TRUE
)
# Transform the data into a geodetic CRS (required by all rdeck layers)
<- st_transform(population, 4326)
population
# Rename the geometry column (required by add_polygon_layer())
<- st_set_geometry(population, "polygon")
population
# Download data on race/ethnic group population by county
<- get_acs("county",
race state = state,
variables = race_variables,
geometry = TRUE
)
# Convert data into a dot density format
<- race |>
race_dot_density 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)
<- st_transform(race_dot_density, 4326)
race_dot_density
# Rename the geometry column (required by add_scatterplot_layer())
<- st_set_geometry(race_dot_density, "position") race_dot_density
Create light and dark rdeck map instances
# Create a rdeck map instance using the Mapbox light and Mapbox dark themes
<- rdeck(
rdeck_light map_style = mapbox_light(),
theme = "kepler",
initial_bounds = st_bbox(population),
height = 600
)
<- rdeck(
rdeck_dark 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"
) )