Assignment 6

Author

Anna StPierre

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.

knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
# census_api_key("YOUR_API_KEY_HERE", install = TRUE)
census_data <- get_acs(
  geography = "county",
  variables = c(income = "B19013_001", rent = "B25064_001"),
  state = "ME",
  survey = "acs5",
  year = 2021
)
census_wide <- census_data |>
  select(GEOID, NAME, variable, estimate) |>
  pivot_wider(names_from = variable, values_from = estimate) |>
  rename(Median_Household_Income = income,
         Median_Gross_Rent = rent) |>
  # For example, calculate a rent-to-income ratio
  mutate(rent_income_ratio = Median_Gross_Rent / Median_Household_Income) |>
  # Filter out any rows with missing values
  filter(!is.na(rent_income_ratio))
census_table <- census_wide |>
  gt() |>
  tab_header(
    title = "Median Household Income and Median Gross Rent by County in Maine"
  ) |>
  fmt_currency(
    columns = vars(Median_Household_Income),
    currency = "USD"
  ) |>
  fmt_currency(
    columns = vars(Median_Gross_Rent),
    currency = "USD"
  )
census_table
Median Household Income and Median Gross Rent by County in Maine
GEOID NAME Median_Household_Income Median_Gross_Rent rent_income_ratio
23001 Androscoggin County, Maine $59,287.00 $812.00 0.01369609
23003 Aroostook County, Maine $47,278.00 $627.00 0.01326198
23005 Cumberland County, Maine $80,679.00 $1,273.00 0.01577858
23007 Franklin County, Maine $53,607.00 $701.00 0.01307665
23009 Hancock County, Maine $60,354.00 $865.00 0.01433211
23011 Kennebec County, Maine $58,097.00 $813.00 0.01399384
23013 Knox County, Maine $63,399.00 $927.00 0.01462168
23015 Lincoln County, Maine $62,121.00 $855.00 0.01376346
23017 Oxford County, Maine $51,384.00 $751.00 0.01461544
23019 Penobscot County, Maine $55,125.00 $870.00 0.01578231
23021 Piscataquis County, Maine $46,948.00 $741.00 0.01578342
23023 Sagadahoc County, Maine $73,343.00 $948.00 0.01292557
23025 Somerset County, Maine $47,488.00 $767.00 0.01615145
23027 Waldo County, Maine $59,880.00 $837.00 0.01397796
23029 Washington County, Maine $46,689.00 $605.00 0.01295808
23031 York County, Maine $73,856.00 $1,090.00 0.01475845
ggplot(census_wide, aes(x = Median_Household_Income, y = Median_Gross_Rent, label = NAME)) +
  geom_point() +
  geom_text(nudge_y = 200, check_overlap = TRUE) +
  labs(
    title = "Comparison of Median Household Income and Median Gross Rent",
    x = "Median Household Income (USD)",
    y = "Median Gross Rent (USD)"
  ) +
  theme_minimal()

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.