Spatial data visualization with `tidycensus`

Location! location! location! Where people live in space tells us a lot about the space itself and people of who live there.

Today’s demo is about spatial data visualization with `tidycensus` package using population and race are two example variables I am curious about. First we will get the big picture with Virginia state level population & race distribution, then will zoom in on northern Virginia in Washington DC metro area (this is where I live).

This notebook serves several purposes:

  • demonstrate how to access Census Bureau’s data;
  • map population and race distribution;
  • an exercise of ‘tidycensus’ R package for mapping census data

Tools

We do not have to start from scratch. As always the case, someone has already done the hard work for us so we can stand on their shoulders.

Kyle Walker in this case has developed an rstat package called tidycensus. This package allows for easy access, analysis and visualization of Census Bureau data on hundreds of variables.

Not that in Python you can not do spatial analysis/visualization of census data, but certainly not as easily as in R because of some excellent rstats packages available and tailored for this purpose.

To be able to use tidycensus you’ll need your own Census API Key. If you do not have one, get one.

The only other library you’ll need is tidyverse; and this is it! If you like interactive visualization of maps (i.e. zoom in zoom out etc.) you will need additional libraries and codes.


Okay, so here we go …
# import libraries
library(tidycensus)
library(tidyverse)
options(tigris_use_cache = TRUE)

# get your Census Bureau API key
census_api_key("YOUR API KEY")
# getting VA population data of counties
vapop <- get_acs(state = "VA", geography = "county", 
                  variables = "B19013_001", geometry = TRUE)
# plot VA population
vapop %&gt;%
  ggplot(aes(fill = estimate)) + 
  geom_sf(color = NA) + 
  coord_sf(crs = "+init=epsg:4326") + # original 26911 
  scale_fill_viridis_c(option = "viridis", direction=-1) 
# specify races
races <- c(White = "P005003", 
              Black = "P005004", 
              Asian = "P005006", 
              Hispanic = "P004003")

# get decennial data on races
varace <- get_decennial(geography = "county", variables = races, 
                  state = "VA", geometry = TRUE,
                  summary_var = "P001001") 

# plot race variables as a percent of total population
varace %&gt;%
  mutate(percent = 100 * (value / summary_value)) %&gt;% # create a calculated column of percent value
  ggplot(aes(fill = percent)) +
  facet_wrap(~variable) +
  geom_sf(color = NA) +
  coord_sf(crs = "+init=epsg:4326") + 
  scale_fill_viridis_c(option = "magma", direction=-1)

Zooming in on Northern Virginia

# specify counties that constitute Northern Virginia
NOVA = c("Fairfax County", "Fairfax City", "Manassas Park City", "Arlington County", "Loudoun County", "Alexandria City", "Falls Church City", "Prince William County", "Manassas City")

# get population data of NOVA cunties
novapop <- get_acs(state = "VA", county = NOVA, geography = "tract", 
                  variables = "B19013_001", geometry = TRUE)

# plot NOVA population

novapop %&gt;%
  ggplot(aes(fill = estimate)) + 
  geom_sf(color = NA) + 
  coord_sf(crs = "+init=epsg:4326") + 
  scale_fill_viridis_c(option = "viridis", direction=-1)
# get decennial data on races
novarace <- get_decennial(geography = "tract", variables = races, 
                  state = "VA", county = NOVA, geometry = TRUE,
                  summary_var = "P001001") 

# plot race variables as a percent of total population
novarace %&gt;%
  mutate(percent = 100 * (value / summary_value)) %&gt;% # create a calculated column of percent value
  ggplot(aes(fill = percent)) +
  facet_wrap(~variable) +
  geom_sf(color = NA) +
  coord_sf(crs = "+init=epsg:4326") + 
  scale_fill_viridis_c(option = "magma", direction=-1)

One thought on “Spatial data visualization with `tidycensus`

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s