Created
September 9, 2021 23:58
-
-
Save arvi1000/31527228ab602a96417dd20cceba8e42 to your computer and use it in GitHub Desktop.
census: commute mode by state leg dist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidycensus) | |
library(tidyverse) | |
# find the variables we care about | |
acs5_2019 <- tidycensus::load_variables(year = 2019, dataset = 'acs5', cache = T) | |
# ok here they are | |
my_vars <- paste0("B08006_", sprintf('%03d', 1:17)) | |
# select just the most important top level responses | |
# (e.g. no public transportation break out) | |
my_vars_subset <- my_vars[c(1,3,4,8,14:17)] | |
my_var_details <- | |
acs5_2019 %>% | |
filter(name %in% my_vars_subset) | |
# pull data from census using my stored API key | |
result_dat <- | |
tidycensus::get_acs(geography = "state legislative district (lower chamber)", | |
year = 2019, | |
variables = my_vars_subset, | |
state = 'NY', | |
survey = 'acs5') | |
# subset to just dist 38 | |
dist38 <- result_dat %>% filter(grepl('District 38', NAME)) | |
# add nice category details | |
dist38 <- merge(dist38, | |
select(my_var_details, c(name, label)), | |
by.x='variable', by.y='name') | |
# calc proportion of total and clean up labels | |
dist38 <- dist38 %>% | |
mutate(pct = estimate / dist38$estimate[1], | |
clean_name = gsub('^.*(!!){1,2}', '', label)) | |
dist38$clean_name <- gsub(':', '', dist38$clean_name) | |
# chart | |
dist38[-1, ] %>% | |
arrange(pct) %>% | |
ggplot(aes(x=fct_inorder(clean_name), y=pct)) + | |
geom_col(fill='red') + | |
geom_text(aes(label=scales::percent(pct, accuracy = 1)), hjust = -0.5) + | |
theme_light() + | |
scale_y_continuous(labels = scales::percent, limits = c(0,1)) + | |
coord_flip() + | |
labs(title='Journey To Work, NY State Leg Distr 38', | |
subtitle = 'ACS 5 year 2019', | |
x='mode', y='Percent') |
Author
arvi1000
commented
Sep 9, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment