Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created February 20, 2018 21:02
Show Gist options
  • Save walkerke/e7cb26810b7f58f7450d6ba5c247c941 to your computer and use it in GitHub Desktop.
Save walkerke/e7cb26810b7f58f7450d6ba5c247c941 to your computer and use it in GitHub Desktop.
library(tidycensus)
library(tmap)
library(tmaptools)
library(sf)
library(tigris)
library(magick)
library(tidyverse)
options(tigris_use_cache = TRUE)
ctys <- c("Dallas", "Tarrant", "Collin County", "Denton",
"Parker", "Wise", "Johnson", "Ellis", "Rockwall",
"Kaufman")
p90 <- get_decennial(geography = "tract", variables = "P0010001",
state = "TX", county = ctys, geometry = TRUE,
year = 1990) %>%
mutate(year = "1990")
p00 <- get_decennial(geography = "tract", variables = "P001001",
state = "TX", county = ctys, geometry = TRUE,
year = 2000) %>%
mutate(year = "2000")
p10 <- get_decennial(geography = "tract", variables = "P0010001",
state = "TX", county = ctys, geometry = TRUE,
year = 2010) %>%
mutate(year = "2010")
p16 <- get_acs(geography = "tract", variables = "B01003_001",
state = "TX", county = ctys, geometry = TRUE) %>%
rename(value = estimate) %>%
mutate(year = "2012-2016")
dfs <- list(p90, p00, p10, p16)
url <- "path to your chosen tileset - I'm using Mapbox"
osm <- read_osm(bb(p90), type = url)
walk(dfs, function(x) {
dots <- x %>%
st_transform(26914) %>%
mutate(value = as.integer(value / 200)) %>%
st_sample(., .$value) %>%
st_sf()
b1 <- tm_shape(osm) +
tm_raster() +
tm_shape(dots) +
tm_dots(alpha = 0.4) +
tm_layout(title = unique(x$year)) +
tm_credits("1 dot = 200 people. Data source: Census/ACS via the R tidycensus package")
save_tmap(b1, paste0("img/", unique(x$year), ".jpg"))
})
i90 <- image_read("img/1990.jpg")
i00 <- image_read("img/2000.jpg")
i10 <- image_read("img/2010.jpg")
i16 <- image_read("img/2012-2016.jpg")
image_animate(c(i90, i00, i10, i16), fps = 0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment