Created
July 23, 2026 00:31
-
-
Save walkerke/c6876d0b7769120f7ac919a45dcf98f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(tidycensus) | |
| library(mapgl) | |
| library(tidyverse) | |
| library(sf) | |
| options(tigris_use_cache = TRUE) | |
| adult_total_vars <- paste0("B06001_", sprintf("%03d", 4:12)) | |
| adult_born_vars <- paste0("B06001_", sprintf("%03d", 16:24)) | |
| puma_birth <- get_acs( | |
| geography = "public use microdata area", | |
| variables = c(adult_total_vars, adult_born_vars), | |
| survey = "acs1", | |
| year = 2024, | |
| geometry = TRUE, | |
| output = "wide" | |
| ) |> | |
| filter(!str_starts(GEOID, "72")) |> | |
| mutate( | |
| adults = rowSums(across(all_of(paste0(adult_total_vars, "E")))), | |
| born = rowSums(across(all_of(paste0(adult_born_vars, "E")))), | |
| pct = 100 * born / adults, | |
| bin = case_when( | |
| is.na(pct) ~ "No data", | |
| pct < 25 ~ "Under 25%", | |
| pct < 40 ~ "25% - 40%", | |
| pct < 55 ~ "40% - 55%", | |
| pct < 65 ~ "55% - 65%", | |
| pct < 75 ~ "65% - 75%", | |
| .default = "Over 75%" | |
| ), | |
| pct_fmt = if_else(is.na(pct), "No data", sprintf("%.1f%%", pct)), | |
| adults_fmt = if_else(is.na(pct), "No data", scales::comma(adults)), | |
| state_label = str_extract(NAME, "(?<=; ).+$"), | |
| puma_label = NAME |> | |
| str_remove("; .+$") |> | |
| str_remove(" PUMA$") |> | |
| str_replace_all("--", " — ") | |
| ) |> | |
| select( | |
| GEOID, | |
| puma_label, | |
| state_label, | |
| pct, | |
| pct_fmt, | |
| adults_fmt, | |
| bin, | |
| geometry | |
| ) | |
| bins <- c( | |
| "Over 75%", | |
| "65% - 75%", | |
| "55% - 65%", | |
| "40% - 55%", | |
| "25% - 40%", | |
| "Under 25%", | |
| "No data" | |
| ) | |
| bin_colors <- c( | |
| "#7f1d3a", # dark red | |
| "#a63d4f", # deep red | |
| "#d96b4f", # red-orange | |
| "#ec9f52", # orange | |
| "#f4c47c", # gold | |
| "#faeec4", # cream | |
| "#d9d9d9" # no data | |
| ) | |
| birth_popup <- paste0( | |
| '<span style="font-weight:700;font-size:14px">{puma_label}</span><br>', | |
| '<span style="color:#6b7280;font-size:12px">{state_label}</span>', | |
| '<div style="height:1px;background:#edeff2;margin:8px -14px"></div>', | |
| '<div style="display:flex;justify-content:space-between;gap:18px">', | |
| '<span style="color:#6b7280">Born in state of residence</span>', | |
| '<span style="font-weight:600">{pct_fmt}</span>', | |
| "</div>", | |
| '<div style="display:flex;justify-content:space-between;gap:18px">', | |
| '<span style="color:#6b7280">Adults (18+)</span>', | |
| '<span style="font-weight:600">{adults_fmt}</span>', | |
| "</div>" | |
| ) | |
| maplibre( | |
| style = openfreemap_style("positron"), | |
| center = c(-98.58, 39.83), | |
| zoom = 3.6 | |
| ) |> | |
| add_fill_layer( | |
| id = "puma-birth", | |
| source = puma_birth, | |
| fill_color = match_expr( | |
| column = "bin", | |
| values = bins, | |
| stops = bin_colors, | |
| default = "#d9d9d9" | |
| ), | |
| fill_opacity = 0.85, | |
| fill_outline_color = "#ffffff40", | |
| before_id = "waterway_line_label", | |
| popup = birth_popup, | |
| popup_style = popup_style( | |
| background_color = "#ffffff", | |
| text_color = "#1f2937", | |
| border_radius = 10, | |
| padding = 14, | |
| font_family = "system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif", | |
| font_size = 13, | |
| shadow = TRUE | |
| ), | |
| hover_options = list( | |
| fill_color = "cyan" | |
| ) | |
| ) |> | |
| add_categorical_legend( | |
| legend_title = "Adults living in the state<br>where they were born<br>2024 1-year ACS by PUMA", | |
| values = bins, | |
| colors = bin_colors, | |
| position = "bottom-left", | |
| layer_id = "puma-birth", | |
| interactive = TRUE, | |
| filter_column = "bin", | |
| style = legend_style( | |
| background_color = "white", | |
| background_opacity = 0.94, | |
| border_color = "#d1d5db", | |
| border_width = 1, | |
| shadow = TRUE | |
| ) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment