JG.

Animation in R

Today I learned that R allows you to build animated images or gifs. Today we are going to explore my experience trying to understand this function. I started off by viewing this blog by Byran. I chose this over the post by the Yihui Xie's who created the namiation package, because it Bryan's blog had a example code that i could quickly reference and try for myself. I tried the example in R but got an error code. I tried to quickly trouble shoot this to try and get a result and see how the funntion worked. I had to define lastmean and prevlastmean and was able to fix the code. From there I was able to understand how the saveGIF function worked and tried the other example code give to us.

saveGIF({

    for (i in 1:10) plot(runif(10), ylim = 0:1)

})

Basically within the function we define the data we want to show. You can add some options such as the interval or speed of the animation by adding ani.options(interval = 1) the higher the number the slower or longer the interval. Something like .5 or even .1 would have a much faster animation than 1. This is done in sections I Ibelieve. at the end of the function you can add something like

}, movie.name = "mtcars_animation.gif", ani.width = 800, ani.height = 600)

this allows you to set the name of the gif and add the size of the animation.

I used data from the MTCars dataset to build my own animated graph that loops through each horsepower number and places it on the graph showing mpg and weight. It displays the horsepower in the top right corner. You will see that there are cars with similar horsepower so there will be multiple dots on the graph.

mtcars_animation.gif

With this MTCars data you can see that as the weight goes up the horse power goes up but the miles per gallon also goes down.

This is a very interesting and useful animation. I will have to look for a way to integrate this into my final project.