How can i add title to each state in an animated barplot using gganimate ?

Hello everyone , i create an animated barplot with 2 states. i want to add a title to each state . How can i do this ? this is my code. please help me

a <- data.frame(group= c("Doctorat","Mastère", "Licence ou équivalent","Niveau secondaire ou équivalent","Niveau collège","Niveau primaire","Autre"), values=c(82, 29, 12,100,48,70,33), frame=rep('a',7))
   b <- data.frame(group=c("Doctorat","Mastère", "Licence ou équivalent","Niveau secondaire ou équivalent","Niveau collège","Niveau primaire","Autre"), values=c(145, 55, 44,70,80,39,65), frame=rep('b',7))
   data <- rbind(a,b)  
   a$group=factor(a$group,levels =  unique(a[["group"]]))
  
  ggplot(a, aes(x=group, y=values,fill=group)) + 
     geom_bar(stat='identity')+
     theme(axis.text.x = element_text(angle = 45, hjust=1)) +
     scale_fill_manual("Niveau d'études", values = c("Doctorat"="lightsalmon","Mastère"="lightsalmon","Licence ou équivalent"="lightsalmon","Niveau secondaire ou équivalent"="mediumaquamarine","Niveau collège"="darkolivegreen3","Niveau primaire"="mediumorchid","Autre"="deepskyblue3"))
  data$group=factor(data$group,levels = unique(a[["group"]]))
  g= ggplot(data, aes(x=group, y=values, fill=group)) + 
     theme(axis.text.x = element_text(angle = 45, hjust=1)) +
     scale_fill_manual("", values = c("Doctorat"="lightsalmon","Mastère"="lightsalmon","Licence ou équivalent"="lightsalmon","Niveau secondaire ou équivalent"="mediumaquamarine","Niveau collège"="darkolivegreen3","Niveau primaire"="mediumorchid","Autre"="deepskyblue3"))+
     
     geom_bar(stat='identity')+
     geom_text(aes(label=values), position=position_dodge(width=0.9), vjust=-0.25) +
     theme(axis.text.x = element_text(angle = 45,face="bold"),axis.title.x=element_blank(),axis.title.y=element_blank()) +
     
     # gganimate specific bits:
     transition_states(
       frame,
       transition_length = 2,
       state_length = 1, wrap = TRUE
     ) +
     ease_aes('sine-in-out')
   animate(g, renderer=gifski_renderer())
  anim_save("outfile.gif")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.