Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created April 27, 2026 20:08
Show Gist options
  • Select an option

  • Save walkerke/30a0d3dc7bfb57c78bfc6f0eb2a746c4 to your computer and use it in GitHub Desktop.

Select an option

Save walkerke/30a0d3dc7bfb57c78bfc6f0eb2a746c4 to your computer and use it in GitHub Desktop.
library(mapgl) # Get the new features: pak::pak("walkerke/mapgl")
library(tidycensus)
library(sf)
library(tidyverse)
sf_use_s2(FALSE)
tx_bgs <- get_acs(
geography = "block group",
variables = "B01001_001",
state = "TX",
geometry = TRUE,
year = 2024
) |>
st_transform(4326)
m <- mapboxgl(bounds = filter(tx_bgs, str_starts(GEOID, "48439"))) |>
add_fill_layer(
id = "Block Groups",
source = tx_bgs,
fill_color = interpolate(
column = "estimate",
na_color = "lightgrey",
stops = viridisLite::viridis(6),
values = seq(0, 8000, length.out = 6)
),
fill_opacity = 0.7,
visibility = "none"
) |>
add_layers_control(position = "top-right") |>
add_draw_control(
radius = TRUE,
attributes = list(
site_id = draw_attribute(type = "text", label = "Site ID"),
notes = draw_attribute(type = "textarea", label = "Site notes")
),
show_measurements = TRUE
)
m
sites <- get_drawn_features(m)
site_populations <- st_join(
sites,
tx_bgs,
left = FALSE
) |>
st_drop_geometry() |>
summarize(pop_est = sum(estimate, na.rm = TRUE), .by = site_id)
site_populations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment