PUBH 6199: Visualizing Data with R, Summer 2025
2025-06-17
tmap
leaflet
tigris
and tidycensus
library(tidyverse)
library(spData)
library(tmap)
data(urban_agglomerations)
urb_2030 <- urban_agglomerations |> filter(year == 2030)
tm_shape(World) +
tm_polygons() +
tm_shape(urb_2030) +
tm_symbols(fill = "black", col = "white", size = "population_millions",
size.legend = tm_legend(title = "Urban Population in 2030\n(millions)"))
leaflet.extras
library(leaflet)
library(leaflet.extras)
thefts_coords <- read_csv("data/thefts_coords.csv")
thefts_sf <- st_as_sf(thefts_coords, coords = c("lon", "lat"), crs = 4326)
leaflet(thefts_sf) %>%
addProviderTiles("OpenStreetMap") %>%
addHeatmap(blur = 15, max = 0.05, radius = 10) %>%
setView(lng = -87.6298, lat = 41.8781, zoom = 11)
library(tidyverse)
library(spData)
library(tmap)
data(urban_agglomerations)
urb_1970_2030 <- urban_agglomerations |> filter(year %in% c(1970, 1990, 2010, 2030))
tm_shape(World) +
tm_polygons() +
tm_shape(urb_1970_2030) +
tm_symbols(fill = "black", col = "white", size = "population_millions",
size.legend = tm_legend(title = "Urban Population\n(millions)")) +
tm_facets(by = "year", ncol = 2)
tmap
leaflet
tigris
and tidycensus
Source: Which color scale to use when visualizing data, by Lisa Charlottte Muth.
Source: Analyzing US Census Data, by Kyle Walker
Source: Analyzing US Census Data, by Kyle Walker
Source: 2020 U.S. Election Mapped, by Vivid Maps
Use Case | Recommended Projection | EPSG Code |
---|---|---|
Equal-area choropleths | Albers Equal Area | 5070 |
Interactive maps | Web Mercator | 3857 |
Global perspective | Robinson or Winkel Tripel | 54030 / 54042 |
Local detail (U.S.) | NAD83 / State Plane | varies |
Use st_transform()
to convert:
In tmap
:
tmap
leaflet
tigris
and tidycensus
sf
, sp
tmap
, ggplot2
, cartography
leaflet
, mapview
, ggmap
rgdal
, rmapshaper
tigris
, tidycensus
{sf}
: simple featuresThe {sf}
package is the standard way to work with vector spatial data in R. It replaces older tools like {sp}
with a simple, tidy-friendly interface.
Key Features of {sf}
data.frame
-like objectdplyr
, ggplot2
, tmap
sfc
column to store spatial information (e.g., points, polygons)tmap
leaflet
tigris
and tidycensus
tmap
ggplot2
, based on “the grammar of graphics”{tmap}
works?
{tmap}
adopts an intuitive approach to map-making: the addition operator + adds a new layer, followed by tm_*():
tm_fill()
: shaded areas for (multi)polygonstm_borders()
: border outlines for (multi)polygonstm_polygons()
: both, shaded areas and border outlines for (multi)polygonstm_lines()
: lines for (multi)linestringstm_symbols()
: symbols for (multi)points, (multi)linestrings, and (multi)polygonstm_raster()
: colored cells of raster data (there is also tm_rgb() for rasters with three layers)tm_text()
: text information for (multi)points, (multi)linestrings, and (multi)polygons{tmap}
tm_polygons()
: for choropleth mapstm_symbols()
: for point data, size and color can represent different variablesScales control how the values are represented on the map and in the legend, and can have a major impact on how spatial variability is portrayed
tm_shape(nz) + tm_polygons(fill = "Median_income")
tm_shape(nz) + tm_polygons(fill = "Median_income",
fill.scale = tm_scale(breaks = c(0, 30000, 40000, 50000)))
tm_shape(nz) + tm_polygons(fill = "Median_income",
fill.scale = tm_scale(n = 10))
tm_shape(nz) + tm_polygons(fill = "Median_income",
fill.scale = tm_scale(values = "BuGn"))
tm_scale_intervals(style = "pretty")
:
{tmap}
A unique feature of
{tmap}
is its ability to create static and interactive maps using the same code. Maps can be viewed interactively at any point by switching to view mode, using the commandtmap_mode("view")
tmap
leaflet
tigris
and tidycensus
{leaflet}
in R{leaflet}
is the most widely used interactive mapping package in R.leaflet
Mappal <- colorNumeric("RdYlBu", domain = cycle_hire$nbikes)
# cycle_hire is an `sf` object with columns: name, nbikes, geometry, built-in data from spData
leaflet(cycle_hire) |>
addProviderTiles(providers$CartoDB.Positron) |>
addCircles(col = ~pal(nbikes), opacity = 0.9) |>
# lnd is a London boroughs shapefile
addPolygons(data = lnd, fill = FALSE) |>
addLegend(pal = pal, values = ~nbikes) |>
setView(lng = -0.1, lat = 51.5, zoom = 12) |>
addMiniMap()
tmap
leaflet
tigris
and tidycensus
tigris
The
{tigris}
package provides access to U.S. Census Bureau geographic data. Shapefiles downloaded using {tigris} will be loaded as a simple features (sf) object with geometries.
A shapefile is a vector data file format commonly used for geospatial analysis.
Shapefiles contain information for spatially describing features (e.g. points, lines, polygons), as well as any associated attribute information.
You can find / download shapefiles online (e.g. from the US Census Bureau), or depending on the tools available, access them via packages (like we’re doing today).
Entire US
library(tigris)
library(sf)
counties <- counties(state = NULL, cb = TRUE, progress_bar = FALSE)
# Use `cb = TRUE` for simplified geometries
glimpse(counties)
Rows: 3,235
Columns: 13
$ STATEFP <chr> "01", "01", "01", "10", "01", "01", "04", "05", "05", "05",…
$ COUNTYFP <chr> "069", "023", "113", "005", "071", "089", "015", "017", "12…
$ COUNTYNS <chr> "00161560", "00161537", "00161583", "00217269", "00161561",…
$ GEOIDFQ <chr> "0500000US01069", "0500000US01023", "0500000US01113", "0500…
$ GEOID <chr> "01069", "01023", "01113", "10005", "01071", "01089", "0401…
$ NAME <chr> "Houston", "Choctaw", "Russell", "Sussex", "Jackson", "Madi…
$ NAMELSAD <chr> "Houston County", "Choctaw County", "Russell County", "Suss…
$ STUSPS <chr> "AL", "AL", "AL", "DE", "AL", "AL", "AZ", "AR", "AR", "AR",…
$ STATE_NAME <chr> "Alabama", "Alabama", "Alabama", "Delaware", "Alabama", "Al…
$ LSAD <chr> "06", "06", "06", "06", "06", "06", "06", "06", "06", "06",…
$ ALAND <dbl> 1501742250, 2365900084, 1660653961, 2424590442, 2792044612,…
$ AWATER <dbl> 4795418, 19114321, 15562947, 674129051, 126334711, 28756353…
$ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((-85.71209 3..., MULTIPOLYGON (…
One state
library(tigris)
library(sf)
counties_md <- counties(state = "Maryland", cb = TRUE, progress_bar = FALSE)
# Use `cb = TRUE` for simplified geometries
glimpse(counties_md)
Rows: 24
Columns: 13
$ STATEFP <chr> "24", "24", "24", "24", "24", "24", "24", "24", "24", "24",…
$ COUNTYFP <chr> "005", "019", "017", "015", "041", "037", "039", "011", "03…
$ COUNTYNS <chr> "01695314", "00596495", "01676992", "00596115", "00592947",…
$ GEOIDFQ <chr> "0500000US24005", "0500000US24019", "0500000US24017", "0500…
$ GEOID <chr> "24005", "24019", "24017", "24015", "24041", "24037", "2403…
$ NAME <chr> "Baltimore", "Dorchester", "Charles", "Cecil", "Talbot", "S…
$ NAMELSAD <chr> "Baltimore County", "Dorchester County", "Charles County", …
$ STUSPS <chr> "MD", "MD", "MD", "MD", "MD", "MD", "MD", "MD", "MD", "MD",…
$ STATE_NAME <chr> "Maryland", "Maryland", "Maryland", "Maryland", "Maryland",…
$ LSAD <chr> "06", "06", "06", "06", "06", "06", "06", "06", "06", "06",…
$ ALAND <dbl> 1549740649, 1400573750, 1185745253, 896912542, 695556093, 9…
$ AWATER <dbl> 215957832, 1145353067, 479451413, 185281256, 539369000, 105…
$ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((-76.3257 39..., MULTIPOLYGON (…
tidycensus
Lab 5 has a tutorial on how to get started with {tidycensus}
packages. Follow it carefully. Remember to add your .Renviron
to .gitignore
so that you do not share your API keys.
Your turn in HW 5:
Choose a U.S. state
Download county shapefiles with tigris
or tidycensus
Plot a choropleth using tmap
Add labels and legends
15:00
tmap
for quick static/interactive mapsleaflet
for rich interactivitytigris
and tidycensus
Fill out the end-of-class survey
~ This is the end of Lecture 5 ~
10:00
PUBH 6199: Visualizing Data with R