Data

Week 28 of tidy Tuesday is a dataset containing the number of age-eligible voters versus total ballots/votes counted by state and year.

Animation

Using the gganimate library, I created an animation over time of the number of eligible voters versus the number of votes.

Code

library(tidyverse)
library(gganimate)
library(scales)

 # Load data
voter_data <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2018-10-09/voter_turnout.csv")

 # Remove unwanted entries
state_data <- voter_data %>% 
  filter(!state %in% c("United States", "United States (Excl. Louisiana)"))

 # Create base plot for animation
votes_vs_eligible <- ggplot(state_data, aes(votes, eligible_voters, colour = state)) +
  geom_point(show.legend = FALSE) + theme_light() +
  scale_y_continuous(labels = comma_format()) +
  scale_x_continuous(labels = comma_format()) +
  labs(title = 'Eligible voters vs Number of votes', 
       subtitle = 'Year: {frame_time}',
       y = 'Eligible voters', x = 'Votes') +
  # extra lines needed for animation are below
  transition_time(year) +
  ease_aes('quartic-in-out')  # determines how points will move

animate(votes_vs_eligible, fps = 7.5)  # create the animation




Twitter