Assignment 6

Author

Theresa Anderson

Open the assign06.qmd file and complete the exercises.

This is a very open-ended assignment. There are three musts:

  1. You must use the tidycensus package to get either decennial or ACS data from the US Census Bureau.

  2. You must get data for two different variables and they can’t be population or median home values.

  3. You must show all the code you used to get the data and create the table or chart.

You can then either create a cool table or chart comparing the two variables. They can be from any region and for any geography…it doesn’t necessarily need to be Maine.

Note: you will receive deductions for not using tidyverse syntax in this assignment. That includes the use of filter, mutate, and the up-to-date pipe operator |>.

The Grading Rubric is available at the end of this document.

We’ll preload the following potentially useful packages

library(tidyverse)
library(tidycensus)
library(gapminder)
library(gt)
library(gtExtras)
library(scales)

This is your work area. Add as many code cells as you need.

acs_vars <- load_variables(2022, "acs5", cache = TRUE)

B20004vars <- acs_vars |>
  filter(str_starts(name, "B20004_")) |>
  select(name, label)
           
B20004vars
# A tibble: 18 × 2
   name       label                                                             
   <chr>      <chr>                                                             
 1 B20004_001 Estimate!!Total:                                                  
 2 B20004_002 Estimate!!Total:!!Less than high school graduate                  
 3 B20004_003 Estimate!!Total:!!High school graduate (includes equivalency)     
 4 B20004_004 Estimate!!Total:!!Some college or associate's degree              
 5 B20004_005 Estimate!!Total:!!Bachelor's degree                               
 6 B20004_006 Estimate!!Total:!!Graduate or professional degree                 
 7 B20004_007 Estimate!!Total:!!Male:                                           
 8 B20004_008 Estimate!!Total:!!Male:!!Less than high school graduate           
 9 B20004_009 Estimate!!Total:!!Male:!!High school graduate (includes equivalen…
10 B20004_010 Estimate!!Total:!!Male:!!Some college or associate's degree       
11 B20004_011 Estimate!!Total:!!Male:!!Bachelor's degree                        
12 B20004_012 Estimate!!Total:!!Male:!!Graduate or professional degree          
13 B20004_013 Estimate!!Total:!!Female:                                         
14 B20004_014 Estimate!!Total:!!Female:!!Less than high school graduate         
15 B20004_015 Estimate!!Total:!!Female:!!High school graduate (includes equival…
16 B20004_016 Estimate!!Total:!!Female:!!Some college or associate's degree     
17 B20004_017 Estimate!!Total:!!Female:!!Bachelor's degree                      
18 B20004_018 Estimate!!Total:!!Female:!!Graduate or professional degree        
income_by_education <- get_acs(
  geography = "county",
  state = "ME",
  county = "Cumberland",
  table = "B20004",
  survey = "acs5",
  year = 2022)
Getting data from the 2018-2022 5-year ACS
income_by_education
# A tibble: 18 × 5
   GEOID NAME                     variable   estimate   moe
   <chr> <chr>                    <chr>         <dbl> <dbl>
 1 23005 Cumberland County, Maine B20004_001    53078  1410
 2 23005 Cumberland County, Maine B20004_002    30226  5456
 3 23005 Cumberland County, Maine B20004_003    39336  1606
 4 23005 Cumberland County, Maine B20004_004    46458  1093
 5 23005 Cumberland County, Maine B20004_005    58206  1480
 6 23005 Cumberland County, Maine B20004_006    76244  2549
 7 23005 Cumberland County, Maine B20004_007    59009  1345
 8 23005 Cumberland County, Maine B20004_008    38777  3955
 9 23005 Cumberland County, Maine B20004_009    45519  1976
10 23005 Cumberland County, Maine B20004_010    51513  2685
11 23005 Cumberland County, Maine B20004_011    69329  2747
12 23005 Cumberland County, Maine B20004_012    90839  5739
13 23005 Cumberland County, Maine B20004_013    46829   786
14 23005 Cumberland County, Maine B20004_014    23945  2452
15 23005 Cumberland County, Maine B20004_015    33890  1367
16 23005 Cumberland County, Maine B20004_016    42184  2148
17 23005 Cumberland County, Maine B20004_017    50372  2213
18 23005 Cumberland County, Maine B20004_018    68569  1854
income_by_education <- income_by_education |>
  left_join(B20004vars, by = c("variable" = "name"))

income_by_education
# A tibble: 18 × 6
   GEOID NAME                     variable   estimate   moe label               
   <chr> <chr>                    <chr>         <dbl> <dbl> <chr>               
 1 23005 Cumberland County, Maine B20004_001    53078  1410 Estimate!!Total:    
 2 23005 Cumberland County, Maine B20004_002    30226  5456 Estimate!!Total:!!L…
 3 23005 Cumberland County, Maine B20004_003    39336  1606 Estimate!!Total:!!H…
 4 23005 Cumberland County, Maine B20004_004    46458  1093 Estimate!!Total:!!S…
 5 23005 Cumberland County, Maine B20004_005    58206  1480 Estimate!!Total:!!B…
 6 23005 Cumberland County, Maine B20004_006    76244  2549 Estimate!!Total:!!G…
 7 23005 Cumberland County, Maine B20004_007    59009  1345 Estimate!!Total:!!M…
 8 23005 Cumberland County, Maine B20004_008    38777  3955 Estimate!!Total:!!M…
 9 23005 Cumberland County, Maine B20004_009    45519  1976 Estimate!!Total:!!M…
10 23005 Cumberland County, Maine B20004_010    51513  2685 Estimate!!Total:!!M…
11 23005 Cumberland County, Maine B20004_011    69329  2747 Estimate!!Total:!!M…
12 23005 Cumberland County, Maine B20004_012    90839  5739 Estimate!!Total:!!M…
13 23005 Cumberland County, Maine B20004_013    46829   786 Estimate!!Total:!!F…
14 23005 Cumberland County, Maine B20004_014    23945  2452 Estimate!!Total:!!F…
15 23005 Cumberland County, Maine B20004_015    33890  1367 Estimate!!Total:!!F…
16 23005 Cumberland County, Maine B20004_016    42184  2148 Estimate!!Total:!!F…
17 23005 Cumberland County, Maine B20004_017    50372  2213 Estimate!!Total:!!F…
18 23005 Cumberland County, Maine B20004_018    68569  1854 Estimate!!Total:!!F…
income_by_education <- income_by_education |>
  filter(!variable %in% c("B20004_001", "B20004_002", "B20004_003", "B20004_004", "B20004_005", "B20004_006", "B20004_007", "B20004_013")) |>
  mutate(
    gender = case_when(
      str_detect(label, "Male") ~ "Male",
      str_detect(label, "Female") ~ "Female",
      TRUE ~ NA_character_),
    education = case_when(
      str_detect(label, "Less than high school graduate") ~ "Did not graduate",
      str_detect(label, "High school graduate (includes equivalency)") ~ "High school graduate",
      str_detect(label, "Some college or associate's degree") ~ "Some college",
      str_detect(label, "Bachelor's degree") ~ "Bachelor's degree",
      str_detect(label, "Graduate or professional degree") ~ "Graduate / Professional",
      TRUE ~ NA_character_),
     education = factor(education, levels = c(
      "Did not graduate",
      "High school graduate",
      "Some college",
      "Bachelor's degree",
      "Graduate / Professional"))) |>
  filter(!is.na(education)) |>
  select(gender, education, estimate) |>
  arrange(education, gender) |>
  rename(median_income = estimate) |>
  mutate(median_income = dollar(median_income))

income_by_education
# A tibble: 8 × 3
  gender education               median_income
  <chr>  <fct>                   <chr>        
1 Female Did not graduate        $23,945      
2 Male   Did not graduate        $38,777      
3 Female Some college            $42,184      
4 Male   Some college            $51,513      
5 Female Bachelor's degree       $50,372      
6 Male   Bachelor's degree       $69,329      
7 Female Graduate / Professional $68,569      
8 Male   Graduate / Professional $90,839      
income_by_education_table <- income_by_education |>
  pivot_wider(
    names_from = gender,
    values_from = median_income) |>
  gt() |>
  cols_label(
    education = fontawesome::fa("school"),
    Female = md("**Female**"),
    Male = md("**Male**")) |>
  tab_header(title = md("**Median Annual Income by Education & Gender**"),
             subtitle = md("**Cumberland County, Maine**")) |>
  tab_options(
    heading.align = "left",
    table.align = "left") |>
  cols_align(
    align = "left",
    columns = c(education)) |>
  tab_source_note(md("[American Community Survey 2022](https://www.census.gov/programs-surveys/acs/data.html)")) |>
  opt_stylize(1) |>
  gt_theme_nytimes()

income_by_education_table
Median Annual Income by Education & Gender
Cumberland County, Maine
Female Male
Did not graduate $23,945 $38,777
Some college $42,184 $51,513
Bachelor's degree $50,372 $69,329
Graduate / Professional $68,569 $90,839
American Community Survey 2022
income_by_education_chart <- income_by_education |>
  ggplot(aes(x = education, y = median_income, fill = gender)) +
  geom_col(position = "dodge") +
  labs(
    title = "Median Annual Income by Education & Gender",
    subtitle = "Cumberland County, Maine",
    x = "Education Level",
    y = "Median Income",
    caption = "American Community Survey (2022)\nhttps://www.census.gov/programs-surveys/acs/data.html") +
  theme_minimal()

income_by_education_chart

Submission

To submit your assignment:

  • Change the author name to your name in the YAML portion at the top of this document
  • Render your document to html and publish it to RPubs.
  • Submit the link to your Rpubs document in the Brightspace comments section for this assignment.
  • Click on the “Add a File” button and upload your .qmd file for this assignment to Brightspace.

Grading Rubric

Item
(percent overall)
100% - flawless 67% - minor issues 33% - moderate issues 0% - major issues or not attempted
Chart or table accuracy.
(45%)
No errors, good labels, everything is clearly visible in the rendered document.
At least two valid variables used from US census data (can be census or ACS)
(40%)
Messages and/or errors suppressed from rendered document and all code is shown.
(7%)
Submitted properly to Brightspace
(8%)
NA NA You must submit according to instructions to receive any credit for this portion.