Skip to content

Instantly share code, notes, and snippets.

@jkr216
Created July 23, 2022 18:53
Show Gist options
  • Save jkr216/53f9b7217134e04271f1b11e21398bd1 to your computer and use it in GitHub Desktop.
Save jkr216/53f9b7217134e04271f1b11e21398bd1 to your computer and use it in GitHub Desktop.
Living arrangements 25 - 34
library(tidyverse)
library(timetk)
library(tidyquant)
library(readxl)
library(janitor)
url <- "https://www2.census.gov/programs-surveys/demo/tables/families/time-series/adults/ad3-25-34.xls"
destfile <- "ad3_25_34.xls"
curl::curl_download(url, destfile)
read_excel(destfile, skip = 7) %>%
slice(1:56) %>%
select(1, 2, 3, `Child of householder2`, `...11`, `Living with other relatives`, `...15`,
`Living with spouse1`, `...9`) %>%
row_to_names(1) %>%
rename(year = Year, men_total = 2, women_total = 3,
men_percent_parent = 4, women_percent_parent = 5,
men_percent_relative = 6, women_percent_relative= 7,
men_percent_spouse = 8, women_percent_spouse= 9) %>%
mutate(across(contains("men"), ~as.numeric(.)),
across(contains("percent"), ~./100),
year = str_remove(year, "s"),
year = str_glue("{year}-01-01") %>% ymd(),
total_parent = ((men_total * men_percent_parent) + (women_total * women_percent_parent))/(men_total + women_total),
total_other_relative = ((men_total * men_percent_relative) + (women_total * women_percent_relative))/(men_total + women_total),
total_spouse = ((men_total * men_percent_spouse) + (women_total * women_percent_spouse))/(men_total + women_total),
total_parent_other_relative = total_parent + total_other_relative) %>%
select(year, total_other_relative, total_parent,total_parent_other_relative, total_spouse) %>%
pivot_longer(-year) %>%
mutate(
name = str_replace_all(name, "_", " ") %>% str_replace("total", "") %>% str_replace("parent other", "parent or other") %>%
str_to_title()
) %>%
ggplot(aes(x = year, y = value, color = name)) +
geom_line() +
theme_minimal() +
scale_y_continuous(labels = scales::percent,
breaks = scales::pretty_breaks(10)) +
scale_x_date(
date_labels = "%Y",
date_breaks = "4 years"
) +
labs(x = "", y = "",
title = "25-34 Years Old Percent Living With",
color = "",
caption = "Source: U.S. Census Bureau America’s Families and Living Arrangements 2021") +
theme(legend.position = "top")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment