library(tidyverse)
library(tidycensus)
library(gapminder)
library(gt)
library(gtExtras)
library(scales)Assignment 6
Go to the shared posit.cloud workspace for this class and open the assign06 project. Open the assign06.qmd file and complete the exercises.
This is a very open-ended assignment. There are three musts:
You must use the
tidycensuspackage 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
This is your work area. Add as many code cells as you need.
census_api_key("ef4c6f65b724d1a826956c4c4459608d9aac0992", install = TRUE, overwrite = 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] "ef4c6f65b724d1a826956c4c4459608d9aac0992"
update.packages("tidycensus")
decennial_vars_2010 <- load_variables(2010, "sf1", cache = TRUE)
head(decennial_vars_2010)# A tibble: 6 × 3
name label concept
<chr> <chr> <chr>
1 H001001 Total HOUSING UNITS
2 H002001 Total URBAN AND RURAL
3 H002002 Total!!Urban URBAN AND RURAL
4 H002003 Total!!Urban!!Inside urbanized areas URBAN AND RURAL
5 H002004 Total!!Urban!!Inside urban clusters URBAN AND RURAL
6 H002005 Total!!Rural URBAN AND RURAL
housing_vars_2010 <- decennial_vars_2010 %>% filter(grepl("housing", label, ignore.case = TRUE))
age_vars_2010 <- decennial_vars_2010 %>%
filter(grepl("age", label, ignore.case = TRUE))
head(housing_vars_2010)# A tibble: 6 × 3
name label concept
<chr> <chr> <chr>
1 H011001 Total population in occupied housing units TOTAL …
2 H011002 Total population in occupied housing units!!Owned with a mor… TOTAL …
3 H011003 Total population in occupied housing units!!Owned free and c… TOTAL …
4 H011004 Total population in occupied housing units!!Renter occupied TOTAL …
5 H011A001 Population in occupied housing units TOTAL …
6 H011A002 Population in occupied housing units!!Owned with a mortgage … TOTAL …
head(age_vars_2010)# A tibble: 6 × 3
name label concept
<chr> <chr> <chr>
1 H004002 Total!!Owned with a mortgage or a loan TENURE
2 H011002 Total population in occupied housing units!!Owned with a mor… TOTAL …
3 H011A002 Population in occupied housing units!!Owned with a mortgage … TOTAL …
4 H011B002 Population in occupied housing units!!Owned with a mortgage … TOTAL …
5 H011C002 Population in occupied housing units!!Owned with a mortgage … TOTAL …
6 H011D002 Population in occupied housing units!!Owned with a mortgage … TOTAL …
census_data_2010 <- get_decennial(
geography = "state",
variables = c("H001001", "P013001"),
year = 2010,
dataset = "sf1"
)Getting data from the 2010 decennial Census
Using Census Summary File 1
head(census_data_2010)# A tibble: 6 × 4
GEOID NAME variable value
<chr> <chr> <chr> <dbl>
1 01 Alabama H001001 2171853
2 02 Alaska H001001 306967
3 04 Arizona H001001 2844526
4 05 Arkansas H001001 1316299
5 06 California H001001 13680081
6 22 Louisiana H001001 1964981
census_data_2010 <- get_decennial(
geography = "state",
variables = c("H001001", "P013001"),
year = 2010,
dataset = "sf1"
)Getting data from the 2010 decennial Census
Using Census Summary File 1
census_data_long <- census_data_2010 %>%
select(NAME, variable, value) %>%
pivot_wider(names_from = variable, values_from = value) %>%
rename(State = NAME, Total_Housing_Units = H001001, Median_Age = P013001) %>%
pivot_longer(cols = c(Total_Housing_Units, Median_Age), names_to = "Variable", values_to = "Value")
ggplot(census_data_long, aes(x = reorder(State, -Value), y = Value, fill = Variable)) +
geom_bar(stat = "identity") +
facet_wrap(~Variable, scales = "free_y") +
labs(
title = "Total Housing Units and Median Age by State (2010)",
x = "State",
y = "Value"
) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 8),
strip.text = element_text(size = 12)
)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. |