I am trying to animate a stacked bar chart over time. In particular, I would like each stack to first build up in a given year and then transition to the next year. Also, I would like to make sure the past bars are visible. The following code plots each bar at once and transitions to the next year while making the previous years invisible. Is there a way to solve this issue?
library(tidyr)
library(ggplot2)
library(gganimate)
library(dplyr)
df <- data.frame(stringsAsFactors=FALSE,
Year = c("2010", "2011", "2012"),
LabelOne = c(1000, 1500, 2000),
LabelTwo = c(50, 100, 150),
LabelThree = c(20, 30, 40)
)
df_long <- gather(df, lbs, Value, LabelOne:LabelThree, -Year)
head(df_long)
pp <- ggplot(df_long, aes(Year, Value)) +
geom_bar(stat = "identity", aes(fill = lbs)) +
transition_states(Year,
transition_length = 4, state_length = 2) +
ease_aes('cubic-in-out')
animate(pp, nframes = 300, fps = 50, width = 400, height = 550)