Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created November 3, 2022 15:52
Show Gist options
  • Save walkerke/46dd7f5bf977c2390ccaa5903f76a57e to your computer and use it in GitHub Desktop.
Save walkerke/46dd7f5bf977c2390ccaa5903f76a57e to your computer and use it in GitHub Desktop.
library(tigris)
library(tidycensus)
library(tidyverse)
library(gganimate)
library(scales)
options(tigris_use_cache = TRUE)
us_county_pop <- get_decennial(
geography = "county",
variables = "P1_001N",
year = 2020,
geometry = TRUE,
resolution = "20m"
) %>%
shift_geometry() %>%
mutate(rank = factor(round(min_rank(desc(value)), -2)))
anim <- ggplot(us_county_pop, aes(fill = value)) +
geom_sf(linewidth = 0.1) +
theme_void(base_family = "Roboto", base_size = 12) +
scale_fill_viridis_c(option = "cividis", trans = log10_trans(), labels = scales::label_number_si()) +
labs(fill = "Value",
title = "US County Populations, 2020",
subtitle = "@kyle_e_walker | tidycensus / gganimate R packages",
caption = "") +
theme(legend.position = "bottom", legend.direction = "horizontal",
legend.key.width = unit(1.5, "cm")) +
transition_states(rank, state_length = 1) +
shadow_mark()
animate(anim, height = 550, width = 800, end_pause = 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment