I wasn't able to find a previously posted question that sufficiently answered this question. In previous posts, accepted answers have used shadow_mark to keep previously rendered layers persistent.
How to keep previous layers of data while doing animation in R gganimate?
This is an okay workaround when displaying the output in a scatterplot, but it is not a cumulative measurement and it fails when trying to do, for example, a stacked bar graph.
Consider the following data. I want to build up a cumulative stacked bar graph, using a transition state in my df.
df <- data.frame(t = c(2000, 2000, 2001, 2001, 2002, 2002),
f = c("y", "n", "y", "n", "y", "n"),
x = c("a", "a", "b", "c", "a", "c"),
y = c(2,3,5,1,4,8))
> df
t f x y
1 2000 y a 2
2 2000 n a 3
3 2001 y b 5
4 2001 n c 1
5 2002 y a 4
6 2002 n c 8
I want to display the data from 2000, and in the next layer I want to add the data from 2001 as cumulative with the previous layer. And again, for the next layer, I want to add the data from 2002 as cumulative with 2000 and 2001.
This shows why shadow_mark is not a solution to cumulative data:
ggplot(df, aes(x=x, y=y, fill=f)) +
geom_col() + labs(x=NULL, y=NULL, fill=NULL, title="{closest_state}") +
transition_states(t, transition_length = 2, state_length = 1) +
shadow_mark() + enter_fade() + exit_shrink() + ease_aes('sine-in-out') + theme_bw()
Adding a call to shadow_mark will not achieve the desired results of a cumulative plot. "a" should have a cumulative total of 9.
It could be possible to subset the data into 3 different df's for c(2000)
, c(2000,2001)
, and c(2000,2001,2002)
, and then rbind after creating a new states column, but that seems like a very hacky approach.
Is there a cleaner way to display cumulative data with the tools built into gganimate?