Introduction

The American Community Survey (ACS) is a yearly survey run by the U.S. Census Bureau. It covers topics the 10-year census doesn’t, like education, income, and health insurance. Because the ACS is based on a sample of households and not a full count, every number it reports is an estimate with a margin of error (MOE). The MOE shows how far off the estimate could be.

This report uses the get_acs() function from the tidycensus R package to request ACS data in two different ways:

  1. As a regular table. Each row is a place and its estimate, with no map shapes. This works well for ranking and comparing places with charts. This report uses it to compare the percent of adults with a graduate degree across Alaska’s boroughs and census areas.
  2. As spatial data. Adding geometry = TRUE to the same function also downloads the boundary of each place, so the data can be mapped. This report uses it to map the percent of children without health insurance across census tracts in the Fairbanks North Star Borough.

Both requests use Alaska data, which brings up the same challenge in each: many places in Alaska have small populations, and small populations lead to large margins of error. The code in this report follows Kyle Walker’s tidycensus workshop.

Data Preparation

Load packages

These packages are used throughout the report.

library(tidycensus)  # downloads ACS and census data into R
library(tidyverse)   # data wrangling (dplyr) and plotting (ggplot2)
library(scales)      # formats axis labels (like adding % signs)
library(plotly)      # turns ggplot charts into interactive charts
library(mapview)     # makes interactive maps

Request 1: A regular table (graduate degrees by borough)

The first request asks for variable DP02_0066P, the percent of people age 25 and over with a graduate or professional degree. The ACS only measures education for people 25 and older, since most younger people haven’t had time to finish school. The data is pulled for all 30 of Alaska’s boroughs and census areas, which the Census Bureau uses in place of counties. It comes from the 2017-2021 5-year ACS, because many Alaska boroughs are too small for the 1-year ACS, which only covers areas with 65,000 or more people.

# Request percent with a graduate degree for every Alaska borough/census area
ak_grad <- get_acs(
  geography = "county",      # county level (boroughs and census areas in Alaska)
  variables = "DP02_0066P",  # percent age 25+ with a graduate or professional degree
  state = "AK",              # Alaska only
  year = 2021                # 2017-2021 5-year ACS (the default survey)
)

The data comes back as a plain table with one row per borough. The estimate column is the percent with a graduate degree, and the moe column is the margin of error. The first 10 rows are shown below.

# Show the first 10 rows as a clean table
ak_grad %>%
  head(10) %>%
  knitr::kable()
GEOID NAME variable estimate moe
02013 Aleutians East Borough, Alaska DP02_0066P 5.4 1.9
02016 Aleutians West Census Area, Alaska DP02_0066P 2.4 1.1
02020 Anchorage Municipality, Alaska DP02_0066P 14.0 0.7
02050 Bethel Census Area, Alaska DP02_0066P 5.5 1.0
02060 Bristol Bay Borough, Alaska DP02_0066P 5.3 2.6
02063 Chugach Census Area, Alaska DP02_0066P 11.7 4.7
02066 Copper River Census Area, Alaska DP02_0066P 11.4 4.4
02068 Denali Borough, Alaska DP02_0066P 14.7 9.9
02070 Dillingham Census Area, Alaska DP02_0066P 9.4 4.9
02090 Fairbanks North Star Borough, Alaska DP02_0066P 13.7 1.2

Request 2: Spatial data (children’s health insurance by census tract)

Finding a variable

To find a variable for the second request, the load_variables() function loads the list of all ACS variables so they can be searched. Here, it’s filtered to show only the health insurance variables for children under 19 in the Data Profile tables.

# Load the list of all 2021 5-year ACS Data Profile variables
vars <- load_variables(2021, "acs5/profile")

# Search for health insurance variables for children under 19
vars %>%
  filter(str_detect(label, "under 19")) %>%
  knitr::kable()
name label concept
DP03_0100 Estimate!!HEALTH INSURANCE COVERAGE!!Civilian noninstitutionalized population under 19 years SELECTED ECONOMIC CHARACTERISTICS
DP03_0100P Percent!!HEALTH INSURANCE COVERAGE!!Civilian noninstitutionalized population under 19 years SELECTED ECONOMIC CHARACTERISTICS
DP03_0101 Estimate!!HEALTH INSURANCE COVERAGE!!Civilian noninstitutionalized population under 19 years!!No health insurance coverage SELECTED ECONOMIC CHARACTERISTICS
DP03_0101P Percent!!HEALTH INSURANCE COVERAGE!!Civilian noninstitutionalized population under 19 years!!No health insurance coverage SELECTED ECONOMIC CHARACTERISTICS

The variable used is DP03_0101P, the percent of children under 19 with no health insurance coverage.

Requesting the data with boundaries

The second request uses the same get_acs() function, with two differences. It asks for census tracts instead of boroughs, and it adds geometry = TRUE. Census tracts are small areas that usually hold between 1,200 and 8,000 people, so they show differences within the borough. geometry = TRUE downloads the boundary of each tract along with the data.

# Request percent of children under 19 with no health insurance, by census tract
fnsb_kids <- get_acs(
  geography = "tract",                 # census tracts
  variables = "DP03_0101P",            # percent of children under 19 with no health insurance
  state = "AK",                        # Alaska
  county = "Fairbanks North Star",     # Fairbanks North Star Borough only
  year = 2021,                         # 2017-2021 5-year ACS
  geometry = TRUE                      # include tract boundaries for mapping
)

The result has the same columns as the first request, plus a geometry column that holds the shape of each tract. That extra column is what makes the data mappable. It’s left out of the table below because it’s very long.

# Show the first 6 rows (without the long geometry column)
fnsb_kids %>%
  sf::st_drop_geometry() %>%
  head() %>%
  knitr::kable()
GEOID NAME variable estimate moe
02090001700 Census Tract 17, Fairbanks North Star Borough, Alaska DP03_0101P 8.2 14.8
02090980000 Census Tract 9800, Fairbanks North Star Borough, Alaska DP03_0101P 0.1 0.6
02090001902 Census Tract 19.02, Fairbanks North Star Borough, Alaska DP03_0101P 0.0 1.8
02090000400 Census Tract 4, Fairbanks North Star Borough, Alaska DP03_0101P 4.8 5.5
02090000300 Census Tract 3, Fairbanks North Star Borough, Alaska DP03_0101P 5.4 6.4
02090001300 Census Tract 13, Fairbanks North Star Borough, Alaska DP03_0101P 1.0 2.3

Analysis

Comparing places with a regular table

Highest and lowest percentages

With a regular table, the boroughs can be sorted and compared directly. The table below shows the five with the highest percentages of graduate degree holders.

# Sort from highest to lowest and keep the top 5
ak_grad %>%
  arrange(desc(estimate)) %>%
  head(5) %>%
  select(NAME, estimate, moe) %>%
  knitr::kable(col.names = c("Borough / Census Area", "Estimate (%)", "MOE"))
Borough / Census Area Estimate (%) MOE
Denali Borough, Alaska 14.7 9.9
Juneau City and Borough, Alaska 14.1 1.9
Anchorage Municipality, Alaska 14.0 0.7
Haines Borough, Alaska 13.9 6.5
Fairbanks North Star Borough, Alaska 13.7 1.2

This table shows the five lowest.

# Sort from lowest to highest and keep the bottom 5
ak_grad %>%
  arrange(estimate) %>%
  head(5) %>%
  select(NAME, estimate, moe) %>%
  knitr::kable(col.names = c("Borough / Census Area", "Estimate (%)", "MOE"))
Borough / Census Area Estimate (%) MOE
Kusilvak Census Area, Alaska 0.6 1.3
Yakutat City and Borough, Alaska 1.8 1.3
Aleutians West Census Area, Alaska 2.4 1.1
Wrangell City and Borough, Alaska 4.0 1.6
Yukon-Koyukuk Census Area, Alaska 4.5 1.4

Denali Borough, Juneau, Anchorage, Haines, and Fairbanks North Star have the highest percentages of graduate degree holders, all around 14%. Kusilvak, Yakutat, Aleutians West, Wrangell, and Yukon-Koyukuk have the lowest, ranging from less than 1% to about 4.5%.

The MOE column matters here, though. Denali ranks first, but its MOE is ±9.9, so its real value could be anywhere from about 5% to 25%. Kusilvak’s MOE (1.3) is larger than its estimate (0.6).

Margin of error plot

To see how reliable each estimate is, the plot below shows every borough’s estimate as a dot, with a line showing its margin of error. Longer lines mean less certainty. This plot follows the margin of error example from the workshop.

ak_plot <- ggplot(ak_grad, aes(x = estimate,
                               y = reorder(NAME, estimate))) +   # sort boroughs by estimate
  # Error bars from (estimate - MOE) to (estimate + MOE)
  geom_errorbar(aes(xmin = estimate - moe, xmax = estimate + moe),
                width = 0.5, linewidth = 0.5, color = "white") +
  # Dot for each estimate
  geom_point(color = "tomato", size = 2) +
  # Show the x-axis as percents
  scale_x_continuous(labels = label_percent(scale = 1)) +
  # Remove ", Alaska" from the borough names
  scale_y_discrete(labels = function(x) str_remove(x, ", Alaska")) +
  labs(title = "Graduate degrees in Alaska",
       subtitle = "Percent of population age 25+ by borough and census area, 2017-2021 ACS",
       caption = "Data acquired with R and tidycensus. Error bars represent margin of error around estimates.",
       x = "ACS estimate",
       y = "") +
  theme_minimal(base_size = 12) +
  # Dark background and white text to match the dark page
  theme(text = element_text(color = "white"),
        axis.text = element_text(color = "white"),
        panel.grid = element_line(color = "gray30"),
        plot.background = element_rect(fill = "#222222", color = NA),
        panel.background = element_rect(fill = "#222222", color = NA),
        plot.title.position = "plot",     # line titles up with the left edge so they have room
        plot.caption.position = "plot")   # same for the caption

ak_plot
Figure 1. Percent of people age 25 and over with a graduate or professional degree in each Alaska borough and census area, from the 2017-2021 ACS. Dots show the estimate and lines show the margin of error. Boroughs with small populations have much wider margins of error.

Figure 1. Percent of people age 25 and over with a graduate or professional degree in each Alaska borough and census area, from the 2017-2021 ACS. Dots show the estimate and lines show the margin of error. Boroughs with small populations have much wider margins of error.

Does this method work well for Alaska?

This method works well for Alaska in that all 30 boroughs and census areas fit on one readable plot. It also shows how unreliable many of the estimates are. Boroughs with larger populations, like Anchorage, Fairbanks North Star, and Mat-Su, have small margins of error, but small rural areas like Denali, Haines, and Lake and Peninsula have very large ones. Denali ranks highest, but its error bar overlaps almost every other borough, so it can’t confidently be called the highest. Kusilvak’s error bar even goes below 0%. The ranking is only reliable for the larger boroughs.

Interactive version

The same plot is shown below as an interactive chart, made with ggplotly() like in the workshop. Hover over a dot to see its exact estimate.

# Convert the ggplot chart to an interactive chart (same as the workshop)
# height = 800 gives the 30 boroughs enough room
ggplotly(ak_plot, tooltip = "x", height = 800)

Figure 2. Interactive version of Figure 1, showing the percent of people age 25 and over with a graduate degree in each Alaska borough and census area (2017-2021 ACS). Hover over each dot to see its estimate.

Mapping places with spatial data

A regular table is good for ranking places, but it can’t show where they are. Because the second request includes tract boundaries, that data can be mapped to show patterns across the borough.

Interactive map

The map below shows the percent of children under 19 without health insurance in each census tract. Click a tract to see its estimate and margin of error, and zoom in on Fairbanks to see the smaller tracts in town. This uses mapview(), like in the workshop.

# Put mapview's legend in the bottom right so the title can go at the top
mapviewOptions(legend.pos = "bottomright")

# Interactive map of percent of uninsured children by census tract
kids_map <- mapview(
  fnsb_kids,
  zcol = "estimate",                        # color tracts by the estimate
  layer.name = "Uninsured children (%)",    # legend title
  alpha.regions = 0.5,                      # make tracts see-through so the basemap shows
  homebutton = FALSE                        # remove the "zoom to layer" button
)

kids_map@map %>%
  # Esri's dark gray basemap (CartoDB's dark basemap now needs an API key)
  leaflet::addTiles(
    urlTemplate = "https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Dark_Gray_Base/MapServer/tile/{z}/{y}/{x}",
    attribution = "Tiles &copy; Esri"
  ) %>%
  # Esri's matching labels layer (town and road names)
  leaflet::addTiles(
    urlTemplate = "https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Dark_Gray_Reference/MapServer/tile/{z}/{y}/{x}"
  ) %>%
  # Add a title box to the top of the map (mapview doesn't add one on its own)
  leaflet::addControl(
    html = "<b>Children without health insurance</b><br>Fairbanks North Star Borough census tracts, 2017-2021 ACS",
    position = "topright",
    className = "map-title"
  )

Figure 3. Interactive map of the percent of children under 19 without health insurance by census tract in the Fairbanks North Star Borough (2017-2021 ACS). Click a tract to see its estimate and margin of error.

Static map

Because this data is a percent, it’s shown as a choropleth map, where each tract is shaded by its value. Choropleth maps work well for percents and rates because they compare areas fairly no matter how many people live in each one. This follows the choropleth example from the workshop.

ggplot(fnsb_kids, aes(fill = estimate)) +
  geom_sf(color = "gray30") +                         # draw tracts with gray outlines
  theme_void() +                                      # remove axes and background grid
  scale_fill_viridis_c(option = "plasma",             # color scale from the workshop's viridis family
                       na.value = "gray50") +         # gray for any tracts with no estimate
  labs(title = "Children without health insurance",
       subtitle = "Fairbanks North Star Borough census tracts, 2017-2021 ACS",
       fill = "% uninsured",
       caption = "Data acquired with R and tidycensus.") +
  # Dark background and white text to match the dark page
  theme(text = element_text(color = "white"),
        plot.background = element_rect(fill = "#222222", color = NA))
Figure 4. Percent of children under 19 without health insurance by census tract in the Fairbanks North Star Borough, from the 2017-2021 ACS. Lighter colors show higher percentages.

Figure 4. Percent of children under 19 without health insurance by census tract in the Fairbanks North Star Borough, from the 2017-2021 ACS. Lighter colors show higher percentages.

At first glance, the map is dominated by a few very large tracts, like the large tract on the west side and the one along the southeast edge, which show moderate to high percentages of uninsured children. These tracts cover most of the borough’s land, but most of that land is empty, and few children live there. Most of the borough’s children live in the small tracts around Fairbanks, North Pole, and College, which are hard to see at this scale. Those small tracts also show the widest range, including the highest values on the map. This is a common problem with choropleth maps: large areas draw the eye even when few people live in them. Zooming in on Fairbanks in the interactive map (Figure 3) gives a better picture of where most children actually live.

These estimates should also be read carefully. Children are a small share of each tract’s population, so the margins of error are large, and the differences between tracts may not be real. Clicking tracts on the interactive map shows how big the margins of error are.

Conclusion

The two requests show two ways to use the same get_acs() function. A regular table is best for ranking places and showing how reliable each estimate is, like the margin of error plot in Figure 1. Spatial data, requested with geometry = TRUE, is best for showing where patterns happen, like the tract maps in Figures 3 and 4.

Both parts also point to the same lesson about Alaska’s ACS data. Small populations, whether a rural borough or a small group like children in one census tract, lead to large margins of error. In both cases, the estimates are most reliable for larger places, and smaller places need to be read with caution. The maps also show a second problem with small populations: large rural tracts with few people take up most of the map, while the small tracts where most people live are hard to see.

Data Sources