I have a bar plot created with ggplot which I would like to animate; the basic plot is ...
I have researched this on the web and created this code:-
Here is some Reprex data ...
Data <- as.data.frame(rbind(c("11 Mar'", "Male", "20-30"),
c("11 Mar'", "Male", "20-30"),
c("11 Mar'", "Female", "20-30"),
c("12 Mar'", "Female", "50-60"),
c("12 Mar'", "Female", "10-20"),
c("12 Mar'", "Male", "60-70"),
c("13 Mar'", "Female", "20-30"),
c("13 Mar'", "Female", "60-70"),
c("13 Mar'", "Male", "60-70"),
c("13 Mar'", "Male", "60-70"),
c("13 Mar'", "Female", "20-30"),
c("14 Mar'", "Female", "70-80"),
c("14 Mar'", "Female", "70-80"),
c("14 Mar'", "Male", "40-50")))
colnames(Data) <- c("Date", "Sex", "AgeGroup")
And this is my call to ggplot ...
ggplot(Data, aes(x = AgeGroup, fill = Sex, frame = Date, Cumulative = TRUE)) +
geom_bar(position = position_dodge2(preserve = "single")) +
scale_fill_manual(values = c("lightblue", "darkblue")) +
xlab("\nAge Group") + ylab("\nIndividuals") +
ggtitle("\nMarch - April 2020") +
scale_y_discrete(limits= c(2,4,6,8)) + theme_pc() +
transition_states(Date, transition_length = 4, state_length = 1) +
labs(title = 'Date: {closest_state}',
subtitle = "Age and Gender distribution",
caption = "data as of 0945 10 Apr 2020")
Unfortunately I am not getting the the desired output - the data is not being shown cumulatively instead each frame of the plot refreshes to show only a single days result. I expected that geom_bar would always accumulate the data across days but it doesn't seem to - I even have "Cumulative = TRUE" in the ggplot call but still the result looks like this ...
Can anyone point me in the right direction ??