# R: Animating 2D and 3D plots

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**Analysis with Programming**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

One great package in R is the animation made by Yihui Xie. And just for fun, we are going to explore that. Our aim is to create simple animated 2D and 3D plots. Here is the first one, 2D of course

The code,

It’s a piece of cake right? The function we used for wrapping the plot is

What about 3-dimensional? Speechless,

The function of the above plot is, $z = sin(x\times y)$, and was generated using the codes below,

Another wave, $z = sin(x) + cos(y)$, looped both through the ranges of $x$ and $y$, and degrees of $\theta$,

You should try it.

The code,

It’s a piece of cake right? The function we used for wrapping the plot is

**saveGIF**, this function basically collects all the plots made and use these as frames of the GIF file. In other words, the above plot was generated/looped 100 times through the**curve**function, and in every iteration we increased the limits of the x axis; hence rolling all the generated plots, animates x-axis towards positive values.What about 3-dimensional? Speechless,

The function of the above plot is, $z = sin(x\times y)$, and was generated using the codes below,

Another wave, $z = sin(x) + cos(y)$, looped both through the ranges of $x$ and $y$, and degrees of $\theta$,

You should try it.

To

**leave a comment**for the author, please follow the link and comment on their blog:**Analysis with Programming**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.