| Package | Purpose |
|---|---|
| leaflet | Interactive maps |
| dplyr | Data wrangling |
| readr | CSV/TSV reading |
| sf | Spatial data |
| tigris | US shapefiles |
| scales | Number formatting |
From Local Landmarks to Public Health Insights
2026-05-04
leaflet, dplyr, readr, tigris, sf| Package | Purpose |
|---|---|
| leaflet | Interactive maps |
| dplyr | Data wrangling |
| readr | CSV/TSV reading |
| sf | Spatial data |
| tigris | US shapefiles |
| scales | Number formatting |
Leaflet is a JavaScript library for interactive maps wrapped for R via the leaflet package.
| Function | Role |
|---|---|
leaflet() |
Initialize the map |
addTiles() |
Add a base tile layer |
addProviderTiles() |
Add styled tiles |
addMarkers() |
Pin-drop markers |
addCircleMarkers() |
Sized circle markers |
addPolygons() |
Filled shapes/regions |
addLegend() |
Color legend |
A simple data frame of three local places with coordinates:
We use the GeoNames cities dataset (cities with 5,000+ population):
The raw file has no column names, we rename the ones we need:
cities <- cities_raw |>
transmute(
city = X2,
latitude = X5,
longitude = X6,
country_code = X9,
state = X11,
population = X15
) |>
filter(!is.na(population), population > 0)
# Keep only the biggest city per US state
biggest_city_each_state <- cities |>
filter(country_code == "US",
!is.na(state), state != "") |>
group_by(state) |>
slice_max(population, n = 1, with_ties = FALSE) |>
ungroup() |>
mutate(
popup_text = paste0(
"<b>", city, "</b><br>",
"State: ", state, "<br>",
"Population: ", comma(population)
)
)leaflet(biggest_city_each_state) |>
addProviderTiles(providers$CartoDB.Positron) |>
setView(lng = -98.5795, lat = 39.8283, zoom = 4) |>
addCircleMarkers(
lng = ~longitude,
lat = ~latitude,
radius = 7,
color = "darkblue",
fillColor = "skyblue",
fillOpacity = 0.85,
weight = 1,
popup = ~popup_text,
label = ~paste0(city, ", ", state)
)We use County Health Rankings data (2025) linked to county shapefiles:
The tigris package downloads US Census shapefiles directly into R as simple features (sf) objects:
tigris gives us polygon boundaries for every US county, state, zip code, and more.
sf (simple features) is the standard R format for vector spatial data — Leaflet speaks sf natively.
pal_obesity <- colorNumeric(
palette = "YlOrRd",
domain = wi_map$adult_obesity,
na.color = "gray90"
)
leaflet(wi_map) |>
addProviderTiles(providers$CartoDB.Positron) |>
addPolygons(
fillColor = ~pal_obesity(adult_obesity),
fillOpacity = 0.75,
color = "white",
weight = 1,
popup = ~paste0(
"<b>", NAME, " County</b><br>",
"Adult Obesity: ", percent(adult_obesity, accuracy = 0.1), "<br>",
"Life Expectancy: ", round(life_expectancy, 1), " years"
),
label = ~paste0(NAME, ": ", percent(adult_obesity, accuracy = 0.1))
) |>
addLegend(
position = "bottomright",
pal = pal_obesity,
values = ~adult_obesity,
title = "Adult Obesity Rate",
opacity = 0.8,
labFormat = labelFormat(
transform = function(x) 100 * x,
suffix = "%"
)
)pal_life <- colorNumeric(
palette = "YlGnBu",
domain = wi_map$life_expectancy,
na.color = "gray90"
)
leaflet(wi_map) |>
addProviderTiles(providers$CartoDB.Positron) |>
addPolygons(
fillColor = ~pal_life(life_expectancy),
fillOpacity = 0.75,
color = "white",
weight = 1,
popup = ~paste0(
"<b>", NAME, " County</b><br>",
"Life Expectancy: ", round(life_expectancy, 1), " years<br>",
"Adult Obesity: ", percent(adult_obesity, accuracy = 0.1)
),
label = ~paste0(NAME, ": ", round(life_expectancy, 1), " years")
) |>
addLegend(
position = "bottomright",
pal = pal_life,
values = ~life_expectancy,
title = "Life Expectancy (years)",
opacity = 0.8
)| Feature | Leaflet | geom_sf / ggplot2 |
|---|---|---|
| Interactivity | ✅ Zoom, pan, click | ❌ Static image |
| Popups & tooltips | ✅ Built-in | ❌ Not supported |
| Base map tiles | ✅ OpenStreetMap, CartoDB, etc. | ❌ None |
| Publication plots | ⚠️ Limited | ✅ Full ggplot2 control |
| Learning curve | 🟢 Easy | 🟡 Moderate |
| Best for | Presentations, web, exploration | Papers, reports, print |
All code and data available in r_package.qmd
Interactive Mapping with R & Leaflet