#clearing the directory and loading all needed packages

library(dplyr)
library(ggplot2)
library(gganimate)
library(readxl)
library(RColorBrewer)
library(png)
library(gifski)
library(knitr)
library(animation)
library(viridis)
AnimateData <- read_excel("C:\\Users\\CcHUB_1\\Downloads\\R Training\\Basic Research and Analysis Training March Session 1 followup - Copy.xlsx")

#generating colors

n=25 #no specific reason for choosing 21. it is purely ramdom. This is the number of palettes you would want in the palette. Minimum is usually 3
color_palette <- brewer.pal.info[brewer.pal.info$category=='qual',] #choosing the type of color pallete (name)
color_vector = unlist(mapply(brewer.pal, color_palette$maxcolors, rownames(color_palette))) #creatingthe colors vector

#Creating a Static Bargraph

StaticGraph <- ggplot(AnimateData, aes(AnimateData$'Teachers Using App')) + geom_bar(stat = "count", width = 12, fill = viridis(15))  + labs(title = 'Animated Graph of Teachers Using App X', x = 'Teachers', y = 'Count') + theme_minimal() + theme(plot.title = element_text(hjust = 0.5, face = "bold")) + scale_fill_manual(values = sample(color_vector, n))


StaticGraph
## Warning: Use of `AnimateData$"Teachers Using App"` is discouraged. Use `Teachers
## Using App` instead.
## Warning: Removed 3 rows containing non-finite values (stat_count).

Combining Facet and Gganimate (Transition_state)

Heading_Name <- list("1" = "Month 1", "2" = "Month 2", "3" = "Month 3")  

Heading_Label <- function(variable, value){return(Heading_Name[value])}


StaticGraph <- ggplot(AnimateData, aes(AnimateData$'Teachers Using App')) + geom_bar(width = 12, fill = viridis(32)) + labs(title = 'Animated Graph of Teachers Using App X', x = 'Teachers', y = 'Count') + theme_minimal() + theme(plot.title = element_text(hjust = 0.5, face = "bold")) 

StaticGraph + facet_wrap(~Month, ncol = 1, strip.position = "top", labeller = Heading_Label) + transition_states(ID) + shadow_mark()
## Warning: The labeller API has been updated. Labellers taking `variable` and
## `value` arguments are now deprecated. See labellers documentation.
## Warning: Use of `AnimateData$"Teachers Using App"` is discouraged. Use `Teachers
## Using App` instead.
## Warning: Removed 3 rows containing non-finite values (stat_count).