#Install packeges
#install.packages('animation')
#install.packages('ggplot2')
#install.packages('gganimate')

#Load libraries
library(animation)
library(ggplot2)
library(gganimate)
library(magick)
## Linking to ImageMagick 6.9.9.39
## Enabled features: cairo, fontconfig, freetype, lcms, pango, rsvg, webp
## Disabled features: fftw, ghostscript, x11
#Preview Data Set
data <- economics
head(data)
## # A tibble: 6 x 6
##   date         pce    pop psavert uempmed unemploy
##   <date>     <dbl>  <dbl>   <dbl>   <dbl>    <dbl>
## 1 1967-07-01  507. 198712    12.6     4.5     2944
## 2 1967-08-01  510. 198911    12.6     4.7     2945
## 3 1967-09-01  516. 199113    11.9     4.6     2958
## 4 1967-10-01  512. 199311    12.9     4.9     3143
## 5 1967-11-01  517. 199498    12.8     4.7     3066
## 6 1967-12-01  525. 199657    11.8     4.8     3018
#Review Data
head(data)
## # A tibble: 6 x 6
##   date         pce    pop psavert uempmed unemploy
##   <date>     <dbl>  <dbl>   <dbl>   <dbl>    <dbl>
## 1 1967-07-01  507. 198712    12.6     4.5     2944
## 2 1967-08-01  510. 198911    12.6     4.7     2945
## 3 1967-09-01  516. 199113    11.9     4.6     2958
## 4 1967-10-01  512. 199311    12.9     4.9     3143
## 5 1967-11-01  517. 199498    12.8     4.7     3066
## 6 1967-12-01  525. 199657    11.8     4.8     3018
#Create Plot
generatedPlot <- ggplot(data, aes(x = date, y = unemploy)) +
  
  #Plot Line
  geom_line(show.legend = FALSE, color = 'red') +
  
  #Transition
  transition_reveal(date) +
  
  #Labels
  labs(title = 'Economy Stats', x = 'Date', y = 'Unemployment') +
  
  #Themeing
  theme_minimal() +
  
  #Center title for aesthetic purposes
  theme(plot.title = element_text(hjust = 0.5))

#View animated plot in R
generatedPlot