library(leaflet)
## Warning: package 'leaflet' was built under R version 4.5.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.5.3
## Warning: package 'ggplot2' was built under R version 4.5.3
## Warning: package 'dplyr' was built under R version 4.5.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.1.6
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.2 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
## Warning: package 'sf' was built under R version 4.5.3
## Linking to GEOS 3.14.1, GDAL 3.12.1, PROJ 9.7.1; sf_use_s2() is TRUE
library(tigris)
## Warning: package 'tigris' was built under R version 4.5.3
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
options(tigris_use_cache = TRUE)
# Get Delaware school districts
districts <- school_districts(state = "DE", cb = TRUE, year = 2023)
# FIX projection (THIS removes warning + ensures Leaflet compatibility)
districts <- st_transform(districts, 4326)
# Create index variable (example metric)
set.seed(123)
districts$student_index <- runif(nrow(districts), 60, 100)
# Color palette
pal <- colorNumeric(
palette = "YlGnBu",
domain = districts$student_index
)
# Map
leaflet(districts) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(
fillColor = ~pal(student_index),
weight = 1,
color = "white",
fillOpacity = 0.8,
popup = ~paste0(
"<strong>District:</strong> ", NAME, "<br>",
"<strong>Index:</strong> ", round(student_index, 1)
)
) %>%
addLegend(
pal = pal,
values = ~student_index,
title = "District Education Index",
opacity = 1
)