Skip to content

Instantly share code, notes, and snippets.

@szimmer
Created October 6, 2021 14:35
Show Gist options
  • Save szimmer/70bdfdb582fb2df9420aca36d8393292 to your computer and use it in GitHub Desktop.
Save szimmer/70bdfdb582fb2df9420aca36d8393292 to your computer and use it in GitHub Desktop.
Comparing ACS and Decennial Estimates over time
library(tidycensus)
library(tidyverse)
library(geofacet)
lv19 <- load_variables(2019, "acs1")
d20 <- get_decennial(
"state",
variables=c("HU_Tot"="H1_001N",
"HU_Occ"="H1_002N",
"Pop_Tot"="P1_001N",
"Pop_GQ"="P5_001N"),
year=2020) %>%
mutate(
Year=2020,
Type="Decennial")
d10 <- get_decennial(
"state",
variables=c("HU_Tot"="H003001",
"HU_Occ"="H003002",
"Pop_Tot"="H010001",
"Pop_GQ"="P042001"),
year=2010) %>%
mutate(
Year=2010,
Type="Decennial")
get_acs_spec <- function(yr){
get_acs(
"state",
variables=c("HU_Tot"="B25002_001",
"HU_Occ"="B25002_002",
"Pop_Tot"="B01001_001",
"Pop_GQ"="B26001_001"),
survey="acs1",
year=yr
) %>%
mutate(
Year=yr,
Type="ACS") %>%
rename(value=estimate)
}
dacs <- 2010:2019 %>% map_df(get_acs_spec)
d_all <- bind_rows(dacs, d10, d20) %>%
filter(GEOID!="72") %>%
mutate(
StAbb=cdlTools::fips(GEOID, to="abbreviation")
) %>%
arrange(StAbb, variable, desc(Type), Year) %>%
group_by(StAbb, variable) %>%
mutate(
Ratio=value/value[1]
) %>%
ungroup()
d_all %>%
filter(GEOID=="37") %>%
filter(variable=="HU_Tot") %>%
ggplot(aes(x=Year, y=value, group=Type, colour=Type)) +
geom_line() +
scale_x_continuous(breaks=c(2010, 2015, 2020), labels = function(x) paste0("'", substr(x, 3, 4)))
d_all %>%
filter(variable=="HU_Tot") %>%
ggplot(aes(x=Year, y=Ratio, group=Type, colour=Type)) +
geom_line() +
facet_geo(~StAbb, grid="us_state_grid2") +
scale_x_continuous(breaks=c(2010, 2015, 2020), labels = function(x) paste0("'", substr(x, 3, 4))) +
ggtitle("Ratio of total housing units to 2010 Decennial") +
theme(legend.position = "bottom")
d_all %>%
filter(variable=="HU_Occ") %>%
ggplot(aes(x=Year, y=Ratio, group=Type, colour=Type)) +
geom_line() +
facet_geo(~StAbb, grid="us_state_grid2") +
scale_x_continuous(breaks=c(2010, 2015, 2020), labels = function(x) paste0("'", substr(x, 3, 4))) +
ggtitle("Ratio of occupied housing units to 2010 Decennial") +
theme(legend.position = "bottom")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment