This report completes the practice exercises inspired by a three-day workshop taught by Dr. Kyle Walker on working with the 2021 American Community Survey (ACS) in R. Part A looks at 5-year ACS data for the percent of the population with a graduate degree by county in California. Part B uses 5-year ACS data of median rent by census tract in San Francisco County to create an interactive choropleth map.
# query ACS data on percent graduate degrees by county in California
CA_Grad <- get_acs(
geography = "county",
state = "CA",
variables = c(percent_graduate = "DP02_0066P"),
year = 2021)
# use tidyverse to arrange data from highest to lowest
arrange(CA_Grad, desc(estimate))
## # A tibble: 58 × 5
## GEOID NAME variable estimate moe
## <chr> <chr> <chr> <dbl> <dbl>
## 1 06085 Santa Clara County, California percent_graduate 26.6 0.3
## 2 06041 Marin County, California percent_graduate 26.3 0.9
## 3 06075 San Francisco County, California percent_graduate 24.1 0.5
## 4 06081 San Mateo County, California percent_graduate 23.1 0.5
## 5 06001 Alameda County, California percent_graduate 21.3 0.3
## 6 06113 Yolo County, California percent_graduate 21 0.9
## 7 06003 Alpine County, California percent_graduate 18.7 8.5
## 8 06087 Santa Cruz County, California percent_graduate 17.8 0.8
## 9 06013 Contra Costa County, California percent_graduate 16.9 0.4
## 10 06073 San Diego County, California percent_graduate 15.7 0.3
## # ℹ 48 more rows
Results: Santa Clara, Marin, San Francisco, San Mateo, and Alameda Counties have the highest percentage of graduate degree holders in California.
# use tidyverse to to arrange data form lowest to highest
arrange(CA_Grad, estimate)
## # A tibble: 58 × 5
## GEOID NAME variable estimate moe
## <chr> <chr> <chr> <dbl> <dbl>
## 1 06021 Glenn County, California percent_graduate 2.7 0.8
## 2 06035 Lassen County, California percent_graduate 3.1 0.6
## 3 06011 Colusa County, California percent_graduate 3.5 1
## 4 06025 Imperial County, California percent_graduate 4.1 0.5
## 5 06047 Merced County, California percent_graduate 4.4 0.4
## 6 06103 Tehama County, California percent_graduate 4.5 0.9
## 7 06031 Kings County, California percent_graduate 4.6 0.6
## 8 06039 Madera County, California percent_graduate 4.8 0.6
## 9 06107 Tulare County, California percent_graduate 5 0.4
## 10 06069 San Benito County, California percent_graduate 5.2 0.8
## # ℹ 48 more rows
Results: Glenn, Lassen, Colusa, Imperial, and Merced Counties have the lowest percentage of graduate degree holders in California.
# use ggplot to make a plot with error bars using ACS graduate degree data for counties in California
CA_plot_errorbar <- ggplot(CA_Grad, aes(x = estimate,
y = reorder(NAME, estimate))) +
geom_errorbar(aes(xmin = estimate - moe, xmax = estimate + moe),
width = 0.5, linewidth = 0.5) +
geom_point(color = "lightblue", size = 2) +
scale_x_continuous(labels = label_percent(scale = 1)) +
scale_y_discrete(labels = function(x) str_remove(x, " County, California|, California")) +
labs(title = "Percent of Population with a Graduate Degree, 2017-2021 ACS",
subtitle = "Counties in California",
caption = "Data acquired with R and tidycensus. Error bars represent margin of error around estimates.",
x = "ACS estimate",
y = "") +
theme_classic(base_size = 12)
# view plot
CA_plot_errorbar
Figure 2. County-level estimates of percentage of population with a graduate degree, California.
This method works okay for my state, but there are a lot of counties, so the plot needs to be large and some counties have a wide margin of error.
ggplotly(CA_plot_errorbar, tooltip = "x")
Figure 2. County-level estimates of percentage of population with a graduate degree, California. Hover over each point to see estimate value.
# load and view Data Profiles to select a state and demographic of interest
pers <- load_variables(2021, "acs5/profile")
head(pers)
## # A tibble: 6 × 3
## name label concept
## <chr> <chr> <chr>
## 1 DP02PR_0001 Estimate!!HOUSEHOLDS BY TYPE!!Total households SELECT…
## 2 DP02PR_0001P Percent!!HOUSEHOLDS BY TYPE!!Total households SELECT…
## 3 DP02PR_0002 Estimate!!HOUSEHOLDS BY TYPE!!Total households!!Married-… SELECT…
## 4 DP02PR_0002P Percent!!HOUSEHOLDS BY TYPE!!Total households!!Married-c… SELECT…
## 5 DP02PR_0003 Estimate!!HOUSEHOLDS BY TYPE!!Total households!!Married-… SELECT…
## 6 DP02PR_0003P Percent!!HOUSEHOLDS BY TYPE!!Total households!!Married-c… SELECT…
I’ve decided to use the estimate for median rent (US Dollars) in San Francisco County, California
# query 5-year ACS Census data for median rent by census tract in San Francisco County, California, include county Shapefiles using "geometry = TRUE"
SF_Rent <- get_acs(
geography = "tract",
variables = c(median_rent = "DP04_0134"),
state= "CA",
county = "San Francisco",
geometry = TRUE)
# explore data with the mapview package
mapview(SF_Rent, zcol = "estimate")
Results: It looks like estimates range from 336 to 3,500, but the lowest values are in a tract that is a large outdoor recreation area that doesn’t contain houses, so I will remove that GEOID. There is also an island far off the coast that I want to remove
# filter data with tidyr
SF_Rent_Clean <- SF_Rent %>% filter(GEOID != "06075980501") %>%
filter(GEOID != "06075980401")
# check with mapview again
mapview(SF_Rent_Clean, zcol = "estimate")
Results: Now, values range from ~530-3,500.
# First, I want to show my estimate column in US dollars
SF_Rent_Clean$Median_Rent <- dollar(SF_Rent_Clean$estimate)
# Next, I want to make a custom tooltip
SF_Rent_Clean <- SF_Rent_Clean %>% mutate(Median_Rent = dollar(estimate),
Rent_MOE = dollar(moe),
tooltip_text = paste0(Median_Rent, " +/-", Rent_MOE))
# I will use ggplot to create a choropleth map since my estimates are derived values.
ggsf <- ggplot(SF_Rent_Clean, aes(fill = estimate, data_id = GEOID,
tooltip = tooltip_text)) +
geom_sf_interactive() + #<<
scale_fill_viridis_c(option = "mako", direction = -1) +
theme_classic() +
labs(fill = "Median rent\n2021 ACS",
title = "San Francisco County Census Tracts")
# I will use giraph to show interactive popups.
girafe(ggobj = ggsf) %>% #<<
girafe_options(opts_hover(css = "fill:cyan;"))
Figure 2. San Francisco County Census data from the 5-year ACS showing median rent per tract. Hover over each county to view dollar estimate and margin of error
Results: Rent in San Francisco County is generally very high(above $3,000 in most tracts), but some areas, especially downtown, appear to have lower median rent prices.
SSDAΝ. (2023, March 1). 2023 webinar 1 – Working with the 2021 American Community Survey with R and Tidycensus. [Video]. YouTube. https://www.youtube.com/watch?v=1WKAeM8yR-0
SSDAΝ. (2023, March 2). 2023 webinar 2 – Mapping and spatial analysis with ACS data in R [Video]. YouTube. https://www.youtube.com/watch?v=ahPquLT6gxA