Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created February 20, 2018 22:59
Show Gist options
  • Save walkerke/0d346067883039c3f2156b3429597dd4 to your computer and use it in GitHub Desktop.
Save walkerke/0d346067883039c3f2156b3429597dd4 to your computer and use it in GitHub Desktop.
library(tidycensus)
library(tidyverse)
library(extrafont)
library(plotly)
place00 <- get_decennial(geography = "place", state = "TX",
variables = "P001001", year = 2000) %>%
mutate(NAME = str_replace(NAME, " city.*", "")) %>%
select(NAME, pop00 = value)
place16 <- get_acs(geography = "place", state = "TX",
variables = "B01003_001", survey = "acs1") %>%
mutate(NAME = str_replace(NAME, " city.*", "")) %>%
select(NAME, pop16 = estimate) %>%
inner_join(place00, by = "NAME")
dfwp <- c("Allen", "Arlington", "Carrollton", "Dallas", "Denton",
"Fort Worth", "Frisco", "Garland", "Grand Prairie",
"Irving", "Lewisville", "McKinney", "Mansfield",
"Mesquite", "Plano", "Richardson")
dfw <- filter(place16, NAME %in% dfwp)
p <- ggplot(dfw) +
geom_segment(aes(x = pop00, xend = pop16,
y = reorder(NAME, pop16),
yend = reorder(NAME, pop16))) +
geom_point(aes(x = pop16, y = reorder(NAME, pop16), text = paste0("2016: ", pop16)),
color = "navy", size = 3) +
geom_point(aes(x = pop00, y = reorder(NAME, pop16), text = paste0("2000: ", pop00)),
color = "#90b4d2", size = 3) +
geom_text(data = data.frame(), aes(x = 1188580, y = "Dallas", label = "2000"),
color = "#90b4d2", hjust = 1, size = 3, fontface = "bold",
nudge_x = -80000) +
geom_text(data = data.frame(), aes(x = 1317942, y = "Dallas", label = "2016"),
color = "navy", hjust = 0, size = 3, fontface = "bold",
nudge_x = 80000) +
scale_x_continuous(labels = scales::comma, expand = c(0.1, 0.1)) +
theme_minimal(base_family = "Verdana", base_size = 12) +
labs(x = "Population size",
y = "",
title = "Population change in Dallas-Fort Worth") +
theme(panel.grid.major.y=element_blank())
ggplotly(p, tooltip = "text")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment