Skip to content

Instantly share code, notes, and snippets.

@pdparker
Created June 21, 2020 09:40
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 pdparker/75806082eee207cd91c9f0520b394cad to your computer and use it in GitHub Desktop.
Save pdparker/75806082eee207cd91c9f0520b394cad to your computer and use it in GitHub Desktop.
library(ggmap)
library(maps)
library(mapdata)
library(glue)
library(here)
library(progress)
library(extrafont)
font_import()
tuesdata <- tidytuesdayR::tt_load(2020, week = 25)
#Extract Data
tuesdata$census %>%
filter(region == "USA Total") %>%
mutate(prop = black/total,
prop_large = total/max(total)) -> Proportion
# Plot USA
usa <- map_data("usa")
# Get dimensions of USA
usa_dimensions <- tribble(
~ center.lat, ~ center.lon, ~ max.lat, ~ min.lat,~ max.lon, ~ min.lon,
mean(usa$lat), mean(usa$long), max(usa$lat), min(usa$lat),max(usa$long), min(usa$long)
)
# Create Proportion Changes for Population Growth Total and Proportion Black Change
Proportion <- Proportion %>%
mutate(map_lat_min = usa_dimensions$center.lat - ((usa_dimensions$center.lat - usa_dimensions$min.lat)*prop),
map_lat_max = usa_dimensions$center.lat + ((usa_dimensions$max.lat - usa_dimensions$center.lat)*prop),
map_lon_min = usa_dimensions$center.lon - ((usa_dimensions$center.lon - usa_dimensions$min.lon)*prop),
map_lon_max = usa_dimensions$center.lon + ((usa_dimensions$max.lon - usa_dimensions$center.lon)*prop),
ymax = usa_dimensions$max.lat + usa_dimensions$max.lat*(1-prop_large),
ymin = usa_dimensions$min.lat - usa_dimensions$min.lat*(1-prop_large),
xmax = usa_dimensions$max.lon - usa_dimensions$max.lon*(1-prop_large),
xmin = usa_dimensions$min.lon + usa_dimensions$min.lon*(1-prop_large))
# Create Progress Bar
pb <- progress_bar$new(
format = " Working: [:bar] :percent eta: :eta",
total = 9, clear = FALSE, width= 60)
# Loop to create images
for (i in seq(1790, 1870, 10)){
# Progress bar
pb$tick()
# Extrat data for census year
proportion_tmp <- Proportion %>% filter(year == i)
prp <- paste0(ceiling(proportion_tmp$prop*100),"%")
pop <- format(proportion_tmp$total,big.mark=",",scientific=FALSE)
# Plot total USA
ggplot() + geom_polygon(data = usa, aes(x=long, y = lat, group = group),
fill=NA, color = "black") +
annotate(geom = "text",
x = usa_dimensions$center.lon-20,
y = usa_dimensions$center.lat,
label = prp) +
theme_void() -> p1
# Plot USA map proportional to total population
ggplot() + geom_polygon(data = usa, aes(x=long, y = lat, group = group),
fill="black", color = "black") +
theme_void() -> p2
# Combine Plots and scale via coord_fixed
p3 <- p1 +
annotation_custom(grob = ggplotGrob(p2),
xmin = proportion_tmp$map_lon_min,
xmax = proportion_tmp$map_lon_max,
ymin = proportion_tmp$map_lat_min,
ymax = proportion_tmp$map_lat_max) +
coord_cartesian(xlim = c(proportion_tmp$xmin,proportion_tmp$xmax),
ylim = c(proportion_tmp$ymin,proportion_tmp$ymax))+
#scale_x_continuous(proportion_tmp$xmin,proportion_tmp$xmax) +
#scale_y_continuous(proportion_tmp$ymin,proportion_tmp$ymax)+
#coord_fixed(ratio = 1) +
labs(
title = glue("PROPORTION OF BLACK AMERICANS IN THE TOTAL POPULATION OF THE UNITED STATES: {i}"),
subtitle = "Based on W.E.B. DU Bois's Data Portraits: Plate 42.",
caption = glue("Population of the US: {pop}")
) +
theme_pomological(base_family = "BAYARD", base_size=20)+
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
#text=element_text(family="Impact", size=14),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())
ggsave(filename = here::here("Downloads", glue("plot{i}.png")),plot = p3,
height = 8, width = 10)
}
#Get files
for_gif <- list.files(path = here::here("Downloads"), pattern = "^plot[0-9]+.png",full.names = TRUE)
# Create Gif
gifski::gifski(for_gif, gif_file = here::here("Downloads", "animation.gif"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment