The R code below can replicate this chart. You can get help installing R here.
You can get help with the tidycensus data retreival package here.
You can get help with the gganimate package here.
# Load R packages
library(tidycensus)
library(tidyverse)
library(gganimate)
# Get Data
population <- get_estimates(geography = 'state',
product = 'characteristics',
breakdown = c('SEX', 'AGEGROUP', 'HISP', 'RACE'),
state = 'AL',
breakdown_labels = TRUE,
time_series = TRUE)
# Wrangle Data
p <- population %>%
mutate(
RACE7 = case_when( # Carefully set mutually exclusive race-ethnicity categories.
HISP == 'Hispanic' & RACE == 'All races' ~ 'Hispanic',
HISP == 'Non-Hispanic' & RACE == 'White alone' ~ 'White',
HISP == 'Non-Hispanic' & RACE == 'Black alone' ~ 'Black',
HISP == 'Non-Hispanic' & RACE == 'Asian alone' ~ 'Asian',
HISP == 'Non-Hispanic' & RACE == 'Two or more races' ~ 'Two or more races',
HISP == 'Non-Hispanic' & RACE == 'American Indian and Alaska Native alone' ~ 'American Indian and Alaska Native',
HISP == 'Non-Hispanic' & RACE == 'Native Hawaiian and Other Pacific Islander alone' ~ 'Native Hawaiian and Other Pacific Islander',
TRUE ~ 'Leftover'
),
year = DATE + 2007
) %>%
filter(
RACE7 != 'Leftover',
SEX == 'Both sexes',
str_detect(AGEGROUP, '^Age'),
HISP != 'Both Hispanic Origins',
DATE %in% c(3,10)
) %>% # Visualize Data
ggplot() +
geom_bar(aes(x = AGEGROUP, y = value), stat = 'identity', position = 'identity') +
facet_wrap(~RACE7, nrow = 2, labeller = label_wrap_gen(width = 15)) +
scale_y_continuous(labels = scales::comma) +
coord_flip() +
theme_bw() +
theme(axis.text.x = element_text(angle = -90, hjust = 0, vjust = .5)) +
labs(
title = 'Estimated Population by Age and Race/Ethnicity - Alabama',
subtitle = 'Year: {closest_state}',
y = '',
x = '',
caption = paste0('Source: U.S. Census Population Estimates, Data retreived on ', Sys.Date(), ', created by @StephenHolz')
) +
transition_states(year, transition_length = 1, state_length = 2) +
ease_aes('linear')
animation <- animate(p,
width = 800,
height = 600
)
anim_save(
file.path('2017-AL-population-estimates-by-race-ethnicity-age.gif'),
animation = animation
)