• analytics-link

Creating animated plots in R


R is renowned for it's amazing data visualisation capabilities. Here we show you it's actually quite simple to create an animated plot, and save it as a GIF!

We'll be using the saveGIF function from the animation package, and using the persp function within a loop to create 200 plots which saveGIF stitches together into GIF format.

Here is the code, have a tinker with it and let us know any amazing creations you come up with!

if(!require(animation)) { install.packages("animation"); require(animation)}

saveGIF({ par(bg = rgb(0.003921569,0.09411765,0.2117647,1)) for(i in 1:200){ x <- seq(0 + (i * 0.05), 1 + (i * 0.05), length = 25) y <- x f <- function(x, y) { cos(x * y) } z <- outer(x, y, f) persp(x, y, z, theta = 45, phi = 30, expand = 0.5, col = rgb(0,0.8823529,0.8980392,1), box = F, r = 0.1, main = paste(i, " / 200", sep = ""), col.main = "white") } }, interval = 0.1, ani.width = 550, ani.height = 550, movie.name = "myplot.gif" )

As always, we hope you enjoyed this post - please do share using the social buttons below!

#R #datavisualisation #animation

396 views

© 2020 - Analytics Link