The Lab Notes

The main theme of our research is to understand how gene regulation and genome organization tie in with each other. The Lab Notes are the latest headlines from the lab, featuring a collection of random thoughts and useful code snippets.

3D animations with R

Every now and then I need to make a rotating animation of a 3D plot. The R package rgl turns out to have everything you need, but the grip is a little difficult. Below is an example that will walk you through the steps to make this animation.

First things first, you must make sure that rgl is installed. On Unbuntu, you may also have to install additional libraries. And by the end, you will need to use imagemagick, so at the shell command line you can issue

sudo apt-get install libglu1-mesa-dev
sudo apt-get install imagemagick

You can now start R. Since the package is on the CRAN, you can install it as usual.


For the purposes of this example, we create a random 3D cloud that consist of two Gaussian spheres next to each other.

# Distribute 1000 points at random among two spheres.
x <- matrix(rnorm(3000, mean=rep(c(0,2), each=1500)),
ncol=3, byrow=TRUE)
cols <- rep(c("dodgerblue4", "dodgerblue2"), each=500)

Now we plot the cloud in 3D and save the frames to different .png files. After the call to plot3d you can resize the window...