Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created April 23, 2018 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkerke/bea65396caeaa608a5b024b7562a3505 to your computer and use it in GitHub Desktop.
Save walkerke/bea65396caeaa608a5b024b7562a3505 to your computer and use it in GitHub Desktop.
library(tidycensus)
library(tigris)
library(tidyverse)
library(sf)
library(extrafont)
options(tigris_use_cache = TRUE, tigris_class = "sf")
########################################################################
########################################################################
# Chart 1
########################################################################
########################################################################
# Get the data
austin_metro <- map(c(2010, 2016), function(x) {
get_acs(geography = "tract",
state = "TX",
county = c("Bastrop", "Caldwell", "Hays", "Travis", "Williamson"),
variables = c(totalpop = "B15002_001"),
geometry = TRUE,
year = x) %>%
mutate(endyear = x)
}) %>%
reduce(rbind)
austin_hall <- c(-97.747300, 30.265682) %>%
st_point() %>%
st_sfc(crs = st_crs(austin_metro)) %>%
st_transform(26914)
austin_dist <- austin_metro %>%
st_transform(26914) %>%
mutate(dist = as.numeric(
st_distance(
st_centroid(.), austin_hall
)
)) %>%
mutate(distcat = case_when(
dist < 10000 ~ "0-10km",
dist < 20000 ~ "10-20km",
dist < 30000 ~ "20-30km",
dist < 40000 ~ "30-40km",
TRUE ~ "40km+"
)) %>%
as.data.frame() %>%
group_by(distcat, endyear) %>%
summarize(pop = sum(estimate, na.rm = TRUE)) %>%
ungroup() %>%
spread(key = endyear, value = pop) %>%
rename(pop2010 = `2010`, pop2016 = `2016`) %>%
mutate(pctchange = 100 * ((pop2016 - pop2010) / pop2010))
ggplot(austin_dist) +
theme_minimal(base_family = "Verdana", base_size = 12) +
geom_linerange(aes(ymin = -2, x = reorder(distcat, desc(distcat)),
ymax = pctchange),
color = "blue", size = 1) +
geom_point(aes(y = pctchange, x = reorder(distcat, desc(distcat))),
color = "blue", size = 4) +
coord_flip() +
labs(y = "Percent growth between 2006-10 and 2012-16",
x = "",
title = "Population growth age 25+ by distance band",
subtitle = "Austin, TX metropolitan area",
caption = "Data acquired with the R tidycensus package, ACS table B15002.\nDistance measured from Census tract centroids to Austin City Hall.\nChart by @kyle_e_walker.") +
scale_y_continuous(labels = function(g) {paste0(g, "%")}) +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
plot.title = element_text(face = "bold"),
plot.caption = element_text(size = 7),
plot.margin = unit(c(0.2, 0.5, 0.2, 0.2), "in"))
ggsave("general.png", dpi = 300, height = 5, width = 8)
########################################################################
########################################################################
# Chart 2
########################################################################
########################################################################
# By whether you do - or don't - have a college degree
vars <- paste0("B15002_0", c(15:18, 32:35))
austin_metro <- map(c(2010, 2016), function(x) {
get_acs(geography = "tract",
state = "TX",
county = c("Bastrop", "Caldwell", "Hays", "Travis", "Williamson"),
variables = vars,
summary_var = "B15002_001",
year = x,
geometry = TRUE) %>%
mutate(endyear = x) %>%
group_by(GEOID) %>%
mutate(ba_or_higher = sum(estimate, na.rm = TRUE)) %>%
ungroup() %>%
distinct(GEOID, .keep_all = TRUE) %>%
mutate(less_than_ba = summary_est - ba_or_higher)
}) %>%
reduce(rbind) %>%
select(GEOID, NAME, endyear, ba_or_higher, less_than_ba, over25 = summary_est)
austin_hall <- c(-97.747300, 30.265682) %>%
st_point() %>%
st_sfc(crs = st_crs(austin_metro)) %>%
st_transform(26914)
austin_dist <- austin_metro %>%
st_transform(26914) %>%
mutate(dist = as.numeric(
st_distance(
st_centroid(.), austin_hall
)
)) %>%
mutate(distcat = case_when(
dist < 10000 ~ "0-10km",
dist < 20000 ~ "10-20km",
dist < 30000 ~ "20-30km",
dist < 40000 ~ "30-40km",
TRUE ~ "40km+"
)) %>%
as.data.frame() %>%
group_by(distcat, endyear) %>%
summarize(ba_or_higher_grouped = sum(ba_or_higher, na.rm = TRUE),
less_than_ba_grouped = sum(less_than_ba, na.rm = TRUE)) %>%
ungroup() %>%
gather(key = level, value = pop, -distcat, -endyear) %>%
spread(key = endyear, value = pop) %>%
rename(pop2010 = `2010`, pop2016 = `2016`) %>%
mutate(pctchange = 100 * ((pop2016 - pop2010) / pop2010),
level = case_when(
level == "ba_or_higher_grouped" ~ "Bachelor's degree or higher",
level == "less_than_ba_grouped" ~ "Less than bachelor's degree"
))
ggplot(austin_dist) +
theme_minimal(base_family = "Verdana") +
geom_linerange(aes(color = level, ymin = -0.2, x = reorder(distcat, desc(distcat)),
ymax = pctchange),
size = 1, position = position_dodge(width = 0.5)) +
geom_point(aes(color = level, y = pctchange, x = reorder(distcat, desc(distcat))),
size = 4, position = position_dodge(width = 0.5)) +
coord_flip() +
scale_color_manual(values = c("navy", "lightblue")) +
labs(y = "Percent change between 2006-10 and 2012-16",
x = "",
title = "Population change age 25+ by distance band",
subtitle = "Austin, TX metropolitan area",
caption = "Data acquired with the R tidycensus package, ACS table B15002.\nDistance measured from Census tract centroids to Austin City Hall.\nChart by @kyle_e_walker.",
color = "") +
scale_y_continuous(labels = function(g) {paste0(g, "%")}) +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
plot.title = element_text(face = "bold"),
plot.caption = element_text(size = 7)) +
guides(color = guide_legend(reverse = TRUE))
ggsave("bycollege.png", dpi = 300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment