library(tidyverse)
library(tidycensus)Warning: package 'tidycensus' was built under R version 4.4.3
library(gapminder)
library(gt)
library(gtExtras)Warning: package 'gtExtras' was built under R version 4.4.3
library(scales)Open the assign06.qmd file and complete the exercises.
This is a very open-ended assignment. There are three musts:
You must use the tidycensus package to get either decennial or ACS data from the US Census Bureau.
You must get data for two different variables and they can’t be population or median home values.
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)Warning: package 'tidycensus' was built under R version 4.4.3
library(gapminder)
library(gt)
library(gtExtras)Warning: package 'gtExtras' was built under R version 4.4.3
library(scales)This is your work area. Add as many code cells as you need.
library(tidyverse)
library(tidycensus)
library(gt)
census_api_key("e45e1356249a28afaab9fe16fe72bf82e342a8f9", overwrite = TRUE, install = TRUE)Your original .Renviron will be backed up and stored in your R HOME directory if needed.
Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY").
To use now, restart R or run `readRenviron("~/.Renviron")`
[1] "e45e1356249a28afaab9fe16fe72bf82e342a8f9"
# set up Census API key
readRenviron("~/.Renviron")#Load ACS variable metadata for 2020
acs_vars_2020 <- load_variables(2020, "acs5", cache = TRUE)
acs_vars_2020 |>
filter(str_detect(label, "Educational Attainment")) |>
select(name, label) |>
head()# A tibble: 0 × 2
# ℹ 2 variables: name <chr>, label <chr>
#Get higher education by gender from ACS 2020 for Suffolk County, MA
edu_gender_2020 <- get_acs(
geography = "county",
table = "B15002",
state = "MA",
county = "Suffolk",
year = 2020,
survey = "acs5")Getting data from the 2016-2020 5-year ACS
#Summarize Bachelor’s, Master’s and Doctorate degrees by gender
edu_summary <- edu_gender_2020 |>
select(NAME, variable, estimate) |>
pivot_wider(names_from = variable, values_from = estimate) |>
mutate(
male_bachelor = B15002_015,
male_master = B15002_016,
male_doctorate = B15002_017,
female_bachelor = B15002_032,
female_master = B15002_033,
female_doctorate = B15002_034
) |>
transmute(
County = NAME,
`Males with Bachelor's` = male_bachelor,
`Males with Master's` = male_master,
`Males with Doctorate` = male_doctorate,
`Females with Bachelor's` = female_bachelor,
`Females with Master's` = female_master,
`Females with Doctorate` = female_doctorate)#create a summary table
library(gtExtras)
edu_summary |>
gt() |>
gtExtras::gt_highlight_rows(rows = 1, font_weight = "bold") |>
fmt_number(
columns = where(is.numeric),
decimals = 0
) |>
tab_header(
title = md("**Bachelor's, Master's, and Doctorate Degrees by Gender**"),
subtitle = "Suffolk County, MA – ACS 2020 5-Year Estimates"
)| Bachelor’s, Master’s, and Doctorate Degrees by Gender | ||||||
|---|---|---|---|---|---|---|
| Suffolk County, MA – ACS 2020 5-Year Estimates | ||||||
| County | Males with Bachelor's | Males with Master's | Males with Doctorate | Females with Bachelor's | Females with Master's | Females with Doctorate |
| Suffolk County, Massachusetts | 70,838 | 32,958 | 11,840 | 76,038 | 44,190 | 13,268 |
#Create a bar chart comparing degrees by gender:
edu_long <- edu_summary |>
pivot_longer(
cols = -County,
names_to = c("Gender", "Degree"),
names_pattern = "(Males|Females) with (.*)",
values_to = "Count"
)
ggplot(edu_long, aes(x = Degree, y = Count, fill = Gender)) +
geom_bar(stat = "identity", position = "dodge") +
labs(
title = "Higher Education Attainment by Gender",
subtitle = "Suffolk County, MA – ACS 2020",
x = "Degree Type",
y = "Number of People"
) +
scale_y_continuous(labels = scales::comma) +
theme_minimal()Note: Females are higher in all categories of education!
To submit your assignment:
| 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. |