Mini Lesson 4 ### Creating animated graphs using gganimate()

Setting Up

#inorder for gganimate to work we have to download and install devtools for rstudio through github
devtools::install_github("hadley/devtools")
## Skipping install of 'devtools' from a github remote, the SHA1 (a0c8d733) has not changed since last install.
##   Use `force = TRUE` to force installation
#once "cowplot" and "animation" package has been installed, you can download and install gganimate package
#need to instal "dgrtwo/animate"
devtools::install_github("dgrtwo/gganimate")
## Skipping install of 'gganimate' from a github remote, the SHA1 (bf820020) has not changed since last install.
##   Use `force = TRUE` to force installation

Loading Packages, Programs and data

library(cowplot)
## Loading required package: ggplot2
## 
## Attaching package: 'cowplot'
## The following object is masked from 'package:ggplot2':
## 
##     ggsave
library(animation)
library(ggplot2)
library(gganimate)
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(installr) #need this to install ImageMagick 
## Loading required package: stringr
## 
## Welcome to installr version 0.19.0
## 
## More information is available on the installr project website:
## https://github.com/talgalili/installr/
## 
## Contact: <tal.galili@gmail.com>
## Suggestions and bug-reports can be submitted at: https://github.com/talgalili/installr/issues
## 
##          To suppress this message use:
##          suppressPackageStartupMessages(library(installr))
#install.ImageMagick() #dependency for gganimate
magickPath <- shortPathName("C:\\Program Files\\ImageMagick-7.0.7-Q16\\magick.exe") #gganimate needs a file path for program. 
load("D:/Downloads/ccm.rdata")

Selecting data

#Using ccm data to animate a graph
glimpse(ccm)
## Observations: 3,752
## Variables: 28
## $ color                     <chr> "Color", "Color", "Color", "Color", ...
## $ director_name             <chr> "James Cameron", "Gore Verbinski", "...
## $ num_critic_for_reviews    <int> 723, 302, 602, 813, 462, 392, 324, 6...
## $ duration                  <int> 178, 169, 148, 164, 132, 156, 100, 1...
## $ director_facebook_likes   <int> 0, 563, 0, 22000, 475, 0, 15, 0, 282...
## $ actor_3_facebook_likes    <int> 855, 1000, 161, 23000, 530, 4000, 28...
## $ actor_2_name              <chr> "Joel David Moore", "Orlando Bloom",...
## $ actor_1_facebook_likes    <int> 1000, 40000, 11000, 27000, 640, 2400...
## $ gross                     <int> 760505847, 309404152, 200074175, 448...
## $ genres                    <chr> "Action|Adventure|Fantasy|Sci-Fi", "...
## $ actor_1_name              <chr> "CCH Pounder", "Johnny Depp", "Chris...
## $ movie_title               <chr> "Avatar\xe5\xca", "Pirates of the Ca...
## $ num_voted_users           <int> 886204, 471220, 275868, 1144337, 212...
## $ cast_total_facebook_likes <int> 4834, 48350, 11700, 106759, 1873, 46...
## $ actor_3_name              <chr> "Wes Studi", "Jack Davenport", "Step...
## $ facenumber_in_poster      <int> 0, 0, 1, 0, 1, 0, 1, 4, 3, 0, 0, 1, ...
## $ plot_keywords             <chr> "avatar|future|marine|native|paraple...
## $ movie_imdb_link           <chr> "http://www.imdb.com/title/tt0499549...
## $ num_user_for_reviews      <int> 3054, 1238, 994, 2701, 738, 1902, 38...
## $ language                  <chr> "English", "English", "English", "En...
## $ country                   <chr> "USA", "USA", "UK", "USA", "USA", "U...
## $ content_rating            <chr> "PG-13", "PG-13", "PG-13", "PG-13", ...
## $ budget                    <int> 237000000, 300000000, 245000000, 250...
## $ title_year                <int> 2009, 2007, 2015, 2012, 2012, 2007, ...
## $ actor_2_facebook_likes    <int> 936, 5000, 393, 23000, 632, 11000, 5...
## $ imdb_score                <dbl> 7.9, 7.1, 6.8, 8.5, 6.6, 6.2, 7.8, 7...
## $ aspect_ratio              <dbl> 1.78, 2.35, 2.35, 2.35, 2.35, 2.35, ...
## $ movie_facebook_likes      <int> 33000, 0, 85000, 164000, 24000, 0, 2...
#using duration and ratio variables (and of course time)
ccm4 <- ccm %>%
  select(movie_title,
         duration,
         genres,
         gross, 
         budget,
         title_year
         ) %>%
  mutate(ratio = gross/budget)

Creating animation graph

#creating graph
g <- ggplot(ccm4, aes(x = budget, y = ratio, size = gross, frame = title_year))+
      geom_point(aes(colour = gross, alpha = .6)) +
      scale_x_log10() +
      scale_y_log10()

ani.options(convert = magickPath) #animation options to use magickPath
gganimate(g, filename = "ccm_test.gif")
## Executing: 
## "C:\PROGRA~1\IMAGEM~1.7-Q\magick.exe -loop 0 -delay 100
##     Rplot1.png Rplot2.png Rplot3.png Rplot4.png Rplot5.png
##     Rplot6.png Rplot7.png Rplot8.png Rplot9.png Rplot10.png
##     Rplot11.png Rplot12.png Rplot13.png Rplot14.png Rplot15.png
##     Rplot16.png Rplot17.png Rplot18.png Rplot19.png Rplot20.png
##     Rplot21.png Rplot22.png Rplot23.png Rplot24.png Rplot25.png
##     Rplot26.png Rplot27.png Rplot28.png Rplot29.png Rplot30.png
##     Rplot31.png Rplot32.png Rplot33.png Rplot34.png Rplot35.png
##     Rplot36.png Rplot37.png Rplot38.png Rplot39.png Rplot40.png
##     Rplot41.png Rplot42.png Rplot43.png Rplot44.png Rplot45.png
##     Rplot46.png Rplot47.png Rplot48.png Rplot49.png Rplot50.png
##     Rplot51.png Rplot52.png Rplot53.png Rplot54.png Rplot55.png
##     Rplot56.png Rplot57.png Rplot58.png Rplot59.png Rplot60.png
##     Rplot61.png Rplot62.png Rplot63.png Rplot64.png Rplot65.png
##     Rplot66.png Rplot67.png Rplot68.png Rplot69.png Rplot70.png
##     Rplot71.png Rplot72.png Rplot73.png Rplot74.png
##     "ccm_test.gif""
## Output at: ccm_test.gif

The chunk works correctly and can be played in rstudio as a gif but once knitted and published, there is no gif of the graphic being played. Still need to figure out how to solve this issule. For now, I uploaded it to gify website. https://media.giphy.com/media/xT9IgEPa8KfXqKz5xC/giphy.gif

Had some hard time figuring out why gganimate was unable to call ImageMagick correctly and resulting in error. Needed to change the file path for animations to ImageMagick. Stackoverflow link for solution : https://stackoverflow.com/questions/41129257/gganimate-unable-to-call-imagemagick-correctly-possible-path-error