Mapping U.S. Rents by County in R with tidycensus, sf and ggplot2

Wait 5 sec.

[This article was first published on R Programming – DataScience+, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.The U.S. Census Bureau publishes the rent that a typical household pays in every county in the country, updated every year, and gives it away through a free API. With the tidycensus package you can pull that data — numbers and the map polygons to draw it — in a single function call, then turn it into a national map in about twenty lines. Here we map median gross rent by county to see where renting is expensive and where it is cheap.Getting the dataThe data comes from the American Community Survey (ACS), the Census Bureau’s rolling survey of about 3.5 million addresses a year. We use the 5-year estimates (here 2019–2023), which pool five years of responses so that even small, rural counties get a reliable number. The variable we want is B25064_001: median gross rent in dollars — contract rent plus utilities — for renter-occupied housing.tidycensus talks to the ACS API, and the API needs a free key. Request one at api.census.gov/data/key_signup.html; it arrives by email in a minute. Activate it, then install it into R once — after that tidycensus finds it automatically in every session, so you never put it in a script you share:library(tidycensus)census_api_key("YOUR_KEY_HERE", install = TRUE) # run once; writes to ~/.RenvironWith the key set, load the packages we need:library(tidycensus)library(tigris) # shift_geometry(): repositions Alaska & Hawaiilibrary(dplyr)library(sf)library(ggplot2)library(scales) # dollar labelslibrary(patchwork) # combine the small-multiple mapsoptions(tigris_use_cache = TRUE) # cache boundary files after first downloadNow one call does everything. We ask for the rent variable at geography = "county", and — this is what makes tidycensus special — set geometry = TRUE so it also downloads the county boundaries and returns them joined to the data as an sf object. Leaving state unset gives us every county in the country; shift_geometry() then tucks Alaska and Hawaii under the lower 48 so the whole nation fits one frame.us transmute(County = NAME, `Median rent` = scales::dollar(estimate)) |> head(8) |> knitr::kable()CountyMedian rentSan Mateo County, California$2,893Santa Clara County, California$2,814Marin County, California$2,584San Francisco County, California$2,419Orange County, California$2,352Contra Costa County, California$2,322Alameda County, California$2,318Loudoun County, Virginia$2,317Zooming into the statesA national map flattens what happens inside each state. The state = argument fixes that — it accepts a vector, so one call pulls several states at once. Here we grab the four most populous, then draw each on its own colour scale to bring out where rent concentrates within each one.states