This lesson notes how to use Kyle Walker’s tidycensus package to make maps using American Community Survey data via the Census Bureau’s API. See Reporting with Data in R for more information.

As of this writing, the RWDIR chapter is here.

library(tidyverse)
library(tidycensus)
library(scales)

Get broadband census data

Find the data in the portal

The first challenge is to find the variable we need: Broadband any type.

This is the table.

v19_subject <- load_variables(2019, "acs5/subject", cache = TRUE)

# View(v19_subject)

The variable we need: S2801_C02_014

We’ll check these numbers to make sure we have the right variables.

Get our data from tidycensus

Percetage of housholds with broadband

get_acs(
  year = 2019,
  variables = c(broadband = "S2801_C02_014"),
  geography = "us",
)
## Getting data from the 2015-2019 5-year ACS
## Using the ACS Subject Tables

Now that we’ve confirmed, get for counties in Texas.

tx_broadband <- get_acs(
  year = 2019,
  variables = "S2801_C02_014",
  geography = "county",
  state = "TX"
)
## Getting data from the 2015-2019 5-year ACS
## Using the ACS Subject Tables
tx_broadband

Counties with lowest percentage

tx_broadband %>% 
  arrange(estimate) %>% 
  head()

Counties with highest percentage

tx_broadband %>% 
  arrange(estimate %>% desc()) %>% 
  head()

Make the map

Add the geometry

The same get_acs call, but we are adding geometry.

The output us suppressed.

broadband_tx_geo <- get_acs(
  year = 2019,
  variables = "S2801_C02_014",
  geography = "county",
  state = "TX",
  geometry = TRUE
)

Plot the map

ggplot(broadband_tx_geo) +
  geom_sf(aes(fill = estimate), color = "white", size = .1) +
  theme_void() +
  scale_fill_distiller(
    palette = "Blues",
    direction = 1,
    name = "% Broadband"
  ) +
  labs(
    title = "Broadband connectivity",
    subtitle = "A look at percentage of households with broadband of any type in Texas counties.",
    caption = "Source: American Community Survey/2019 5-year survey"
  )