Dr. Kyle Walker TidyCensus Exercises, Without Spatial

load in packages

library(tidycensus)
library(tidyverse)
library(stringr)
library(scales)
library(plotly)

Census API key running here, but hidden

Get the percent of the population with graduate degrees

graduate_degree <- get_acs(
  geography = "county",
  variables = "DP02_0066P",
  year = 2021
)

Counties only in Michigan, arrange by largest percent of degree holders

graduate_degree_mi_largest <- graduate_degree %>%
  #using the str_detect function from stringr here, instead of splitting the name column like Dr. Walker
  filter(str_detect(GEOID, "^26")) %>%
  arrange(desc(estimate))

#head of counties with largest percent of graduate degree holders
head(graduate_degree_mi_largest)
## # A tibble: 6 × 5
##   GEOID NAME                       variable   estimate   moe
##   <chr> <chr>                      <chr>         <dbl> <dbl>
## 1 26161 Washtenaw County, Michigan DP02_0066P     30.3   0.7
## 2 26125 Oakland County, Michigan   DP02_0066P     21.3   0.3
## 3 26089 Leelanau County, Michigan  DP02_0066P     19.6   1.6
## 4 26065 Ingham County, Michigan    DP02_0066P     18.4   0.6
## 5 26083 Keweenaw County, Michigan  DP02_0066P     16.1   3.3
## 6 26077 Kalamazoo County, Michigan DP02_0066P     15.8   0.6

Counties only in Michigan, arrange by smallest percent of degree holders

graduate_degree_mi_smallest <- graduate_degree %>%
  #using the str_detect function from stringr here, instead of splitting the name column like Dr. Walker
  filter(str_detect(GEOID, "^26")) %>%
  arrange(estimate)

#head of counties with smallest percent of graduate degree holders
head(graduate_degree_mi_smallest)
## # A tibble: 6 × 5
##   GEOID NAME                       variable   estimate   moe
##   <chr> <chr>                      <chr>         <dbl> <dbl>
## 1 26085 Lake County, Michigan      DP02_0066P      3.7   0.7
## 2 26117 Montcalm County, Michigan  DP02_0066P      4.3   0.6
## 3 26067 Ionia County, Michigan     DP02_0066P      4.4   0.6
## 4 26113 Missaukee County, Michigan DP02_0066P      4.4   0.8
## 5 26131 Ontonagon County, Michigan DP02_0066P      4.4   0.7
## 6 26157 Tuscola County, Michigan   DP02_0066P      4.4   0.6

Select just the 25 counties with the largest share of degree holders

graduate_degree_mi_largest_slice <- graduate_degree_mi_largest %>%
  arrange(desc(estimate)) %>%
  #choosing top 25 counties with highest percentatge of graduate degree holders
  slice(1:25)

Make a plot of the counties with the largest share of degree holders, with errorbars for the MOE

md_plot_errorbar <- ggplot(graduate_degree_mi_largest_slice, 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 = "darkred", size = 2) + 
  #using the paste0 function here because the label_percent was not rounding correctly 
  scale_x_continuous(labels = function(x) paste0(x, "%")) + 
  scale_y_discrete(labels = function(x) str_remove(x, " County, Michigan|, Michigan")) + 
  labs(title = "Graduate Degree Share, 2017-2021 ACS",
       subtitle = "Counties in Michigan",
       caption = "Data acquired with R and tidycensus. Error bars represent margin of error around estimates.",
       x = "ACS estimate",
       y = "") + 
  theme_minimal(base_size = 12)

View errorbar plot

md_plot_errorbar
Fig 1. Michigan counties with the highest percentage of graduate degree holders (top 25).

Fig 1. Michigan counties with the highest percentage of graduate degree holders (top 25).

Set up subtitle and captions for plotly_chart (plotly doesn’t directly support subtitle and caption elements as in ggplot2)

plotly_chart <- ggplotly(md_plot_errorbar, tooltip = "x")

plotly_chart <- plotly_chart %>%
  layout(
    annotations = list(
      list(
        text = "Counties in Michigan",
        x = 0,
        y = 1.05,
        xref = "paper",
        yref = "paper",
        showarrow = FALSE,
        font = list(size = 16)
      ),
      list(
        #use \n to drop text to the next line
        text = "Data acquired with R and tidycensus. Error bars \n represent margin of error around estimates.",
        x = 1,
        y = -0.01,
        xref = "paper",
        yref = "paper",
        showarrow = FALSE,
        font = list(size = 10)
      )
    )
  )

Make interactive errorbar plot with plotly package

plotly_chart

Fig 2. Michigan counties with the highest percentage of graduate degree holders (top 25), as an interactive.

The counties with the highest percentage of graduate degree holders are mostly counties that have higher education institutions. Washtenaw is home to UofM, Oakland is home to Oakland University, Ingham is home to Michigan State, and Kalamazoo is home to Western Michigan University.