How to make beautiful county maps of the US

Using the R package Tidycensus

First, you need to obtain an API key — it’s quick and easy. Go here, fill in the short form, and they’ll give you one immediately. You need to plug in your API key where you see XXXXXX below.

To find variables of interest, I recommend the Census website’s advanced search.

require(tidycensus)
require(tidyverse)
require(viridis)

# Plug in your API key where the X's are; then uncomment that line.

# census_api_key("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")

#################################
## Using the tidycensus package 
#################################

us.county.income <- get_acs(geography = "county", variables = "B19013_001", 
                            shift_geo = TRUE, geometry = TRUE)

ggplot(us.county.income) + 
  geom_sf(aes(fill = estimate), color = NA) + 
  coord_sf(datum = NA) + 
  theme_minimal() + 
  scale_fill_viridis_c() +
  labs(fill = "Estimated Avg.") 

For more advanced instructions, including some tips on how to search the Census variables from within R, see these slides by Kyle Walker.


Related

comments powered by Disqus