2

I have a bar plot created with ggplot which I would like to animate; the basic plot is ...enter image description here

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 ...

enter image description here

Can anyone point me in the right direction ??

Peter
  • 118
  • 9
  • please update the individual's count to the sample data and also, add the gganimate code. BTW, you can try creating a new variable with the cumulative sum of each combinations of age group and sex and plotting with that. – Mohanasundaram Apr 19 '20 at 00:54
  • Sorry I don't understand what you mean by "update the individuals count to the sample data" ... also the gganimate code is attached to the end of the golly call - starting at "transition_states(Date ....." – Peter Apr 19 '20 at 04:54
  • Sorry, I misunderstood your dataset as if it is supposed to have the counts in each cross section. No, I realized that the frequency ranges between 0 and 2. – Mohanasundaram Apr 19 '20 at 05:09

1 Answers1

2

I have managed to get it. I have reproduced the data set by duplicating the previous date's rows and adding to current date and so. So that the frequencies will be cumulative. I couldn't get it done in R. However, the data will be looking like this:

Data <- structure(list(Date = c("11 Mar'", "11 Mar'", "11 Mar'", "12 Mar'", 
"12 Mar'", "12 Mar'", "12 Mar'", "12 Mar'", "12 Mar'", "13 Mar'", 
"13 Mar'", "13 Mar'", "13 Mar'", "13 Mar'", "13 Mar'", "13 Mar'", 
"13 Mar'", "13 Mar'", "13 Mar'", "13 Mar'", "14 Mar'", "14 Mar'", 
"14 Mar'", "14 Mar'", "14 Mar'", "14 Mar'", "14 Mar'", "14 Mar'", 
"14 Mar'", "14 Mar'", "14 Mar'", "14 Mar'", "14 Mar'", "14 Mar'"
), Sex = c("Female", "Male", "Male", "Female", "Female", "Female", 
"Male", "Male", "Female", "Female", "Female", "Female", "Female", 
"Female", "Female", "Male", "Male", "Male", "Male", "Male", "Female", 
"Female", "Female", "Female", "Female", "Female", "Female", "Female", 
"Male", "Male", "Female", "Male", "Male", "Male"), AgeGroup = c("20-30", 
"20-30", "20-30", "10-20", "20-30", "50-60", "20-30", "20-30", 
"60-70", "10-20", "20-30", "20-30", "20-30", "50-60", "60-70", 
"20-30", "20-30", "60-70", "60-70", "60-70", "10-20", "20-30", 
"20-30", "20-30", "50-60", "60-70", "70-80", "70-80", "20-30", 
"20-30", "40-50", "60-70", "60-70", "60-70")), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -34L), spec = structure(list(
    cols = list(Date = structure(list(), class = c("collector_character", 
    "collector")), Sex = structure(list(), class = c("collector_character", 
    "collector")), AgeGroup = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))

Then, enter_grow() and exit_fade() with alpha value needs to be added. Ensure that the bars are appearing in the same order of the category. In general, the bar for the the value is available at first is getting plotted irrespective of the color. The makes the shifting of the bar color while transiting.

ggplot(Data, aes(x = AgeGroup, fill = Sex, frame = Date)) + 
  geom_bar(position = position_dodge(preserve = "single")) +
  scale_fill_manual(values = c("lightblue", "darkblue"), drop = TRUE) +
  xlab("\nAge Group") + ylab("\nIndividuals") +
  ggtitle("\nMarch - April 2020") + 
  scale_y_discrete(limits= c(2,4,6,8)) +
  transition_states(Date, transition_length = 4, state_length = 1) + shadow_mark() +
  enter_grow() + 
  exit_fade(alpha = 1)+
  labs(title = 'Date: {closest_state}',  
       subtitle  =  "Age and Gender distribution",
       caption  = "data as of 0945 10 Apr 2020")

enter image description here

Mohanasundaram
  • 2,889
  • 1
  • 8
  • 18