Building a rotating 3D graph and exporting it as a GIF

When dealing with complex datasets, having the possibility to show your data in a 3D environment can be really enhancing.

This recipe will show you how to create such a plot, animate it, and export your animation as a GIF.

Getting ready

This recipe will leverage the rgl package specifically developed for 3D visualizations in R:

install.packages("rgl")
library(rgl)

We will also need to install ImageMagick in order to perform the export into the GIF format.

You can find instructions for software installation at http://www.imagemagick.org/script/binary-releases.php.

As an explicative dataset, we will use the iris dataset, which is a built-in dataset available with all base R installations.

The iris

Get RStudio for R Statistical Computing Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.