#Loading all needed packages
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(gganimate)
library(readxl)
library(RColorBrewer)
library(png)
library(magick)
## Linking to ImageMagick 6.9.9.14
## Enabled features: cairo, freetype, fftw, ghostscript, lcms, pango, rsvg, webp
## Disabled features: fontconfig, x11
library(knitr)
library(animation)
library(viridis)
## Loading required package: viridisLite
library(purrr)
library(viridisLite)
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
Month_labels <- c("Month 1", "Month 2", "Month 3")
names(Month_labels) <- c(1,2,3)
StaticGraph <- ggplot(AnimateData, aes(AnimateData$'Teachers Using App')) + geom_bar(stat = "count", width = 12, fill = viridis(32)) + labs(title = '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)) + facet_wrap(~Month, ncol = 2, labeller = labeller(Month = Month_labels))
StaticGraph
#Animating the Static Graph with the Use of Transition_Manual
AnimateData1 <- AnimateData %>%
split(.$ID) %>%
accumulate(~ bind_rows(.x, .y)) %>%
bind_rows(.id = "frame") %>%
mutate(frame = as.integer(frame))
ggplot(data = AnimateData1, aes(x = AnimateData1$`Teachers Using App`)) + geom_histogram(stat = "count", binwidth = 1, alpha = 0.7, fill = viridis(2445)) + 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")) + facet_wrap(~Month, ncol = 2, labeller = labeller(Month = Month_labels)) + transition_manual(frame)
#Another Use of Transition_Manual
ggplot(AnimateData, aes(AnimateData$`Teachers Using App`)) + geom_bar(stat = "count", width = 12, fill = viridis(32)) + ggtitle("Month:{current_frame}") + theme_minimal() + theme(plot.title = element_text(hjust = 0.5, face = "bold")) + scale_fill_manual(values = sample(color_vector, n)) + labs(x = 'Teachers', y = 'Count') + transition_manual(Month)
## nframes and fps adjusted to match transition
#Use of Transition_state
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 = 2, strip.position = "top", labeller = labeller(Month = Month_labels)) + transition_states(No) + shadow_mark()
#Tweaking the Dataset to Plot Animated Line Graph
Month1_values <- c(0, 100, 150, 200, 215, 230, 300, 350, 400, 430, 500)
Month1_count <- c(1, 5, 1, 10, 3, 1, 4, 2, 1, 6, 7)
Month1_df <- data.frame(Month1_values, Month1_count)
Month2_values <- c(50, 100, 115, 150, 200, 215, 250, 300, 400, 500, 600)
Month2_count <- c( 2, 5, 1, 4, 12, 6, 2, 5, 1, 2, 1)
Month2_df <- data.frame(Month2_values, Month2_count)
Month3_values <- c(0, 0, 50, 100, 115, 150, 200, 215, 250, 300, 400)
Month3_count <- c(0,7, 2, 5, 1, 4, 7, 8, 2, 4, 1)
Month3_df <- data.frame(Month3_values, Month3_count)
All_Month <- c("Month 1", "Month 2", "Month 3")
All_Data <- do.call("cbind", list(Month1_df, Month2_df, Month3_df))
#Use of Transition_reveal
Month1 <- ggplot(Month1_df, aes(x=Month1_values, y=Month1_count)) + geom_line(color = "slateblue4") + geom_point( color = "slateblue4") +theme_minimal() + ylab('Count') + xlab('Teachers') + ggtitle("Teachers Using App Month 1") + theme(plot.title = element_text(hjust = 0.5)) + expand_limits(x=0, y=0) + expand_limits(x=c(100,600), y=c(5, 13)) + transition_reveal(Month1_values)
gifMonth1 <- animate(Month1, width = 400, height = 300)
Month2 <- ggplot(Month2_df, aes(x=Month2_values, y=Month2_count)) + geom_line(color = "darkgreen") + geom_point( color = "darkgreen") +theme_minimal() + ylab('Count') + xlab('Teachers') + ggtitle("Teachers Using App Month 2") + theme(plot.title = element_text(hjust = 0.5)) + expand_limits(x=0, y=0) + expand_limits(x=c(100,600), y=c(5, 13)) + transition_reveal(Month2_values)
gifMonth2 <- animate(Month2, width = 400, height = 300)
Month3 <- ggplot(Month3_df, aes(x=Month3_values, y=Month3_count)) + geom_line(color = "yellow3") + geom_point( color = "yellow3") +theme_minimal() + ylab('Count') + xlab('Teachers') + ggtitle("Teachers Using App Month 3") + theme(plot.title = element_text(hjust = 0.5)) + expand_limits(x=0, y=0) + expand_limits(x=c(100,600), y=c(5, 13)) + transition_reveal(Month3_values)
gifMonth3 <- animate(Month3, width = 400, height = 300)
#Combining the Animated Plots
a_gif <- image_read(gifMonth1)
b_gif <- image_read(gifMonth2)
c_gif <- image_read(gifMonth3)
new_gif <- image_append(c(a_gif[1], b_gif[1], c_gif[1]))
for(i in 2:100){
combined <- image_append(c(a_gif[i], b_gif[i], c_gif[i]))
new_gif <- c(new_gif, combined)
}
new_gif