# Three dimensional plots using rgl package

September 15, 2010
By

(This article was first published on stattler.com - R, and kindly contributed to R-bloggers)

We all know that R can do amazing things including 3 dimensional plots. scatterplot3d is probably the most popular package for doing this. But a few days ago I got introduced with rgl package which can do 3 dimensional graphs with some added advantages like we can rotate the plot using mouse, zoom in or out using the mouse scroll wheel and even can play beautiful animations.
The data I have used for all the examples here is-

    X    Y       Z
2276 1902   156.6
2925 2138  -274.4
2209 1777    57.7
2712 1603   269.4
3128 1545   391.4
.... ....   .....
13526 8575  2178.8
12268 8063  3291.4
12006 7747  4402.9
10763 8752  2533.3
11927 9932   156.6


Here, X,Y and Z are monthly import,export and first differenced net foreign asset of Bangladesh from July 1998 to July 2010 respectively. That means there are a total of 133 observations in each vector. Here is the command for doing a scatter plot in rgl package-

plot3d(x=X, y=Y, z=Z, type="p", col="red", xlab="X", ylab="Y", zlab="Z",
size=5, lwd=15, box=F)

And the plot is (we can rotate the plot as our will, here just two different views are given)-

We can save the screen shot of the plot as png file by the command-

rgl.snapshot(filename="mei11.png",fmt="png")

The plot can be saved as other format like pdf and eps by rgl.postscript command.
In either cases the saved file will no longer be rotatable.
Now, we can make the plot a bit more beautiful by putting type="s" in plot3d function. That means the points will be represented by spheres. The code is-

plot3d(x=X, y=Y, z=Z, type="s", col="red", xlab="X", ylab="Y", zlab="Z",
size=5, radius=200, box=F)

If we leave the radius argument as default and put col=rainbow(n) the plot becomes very interesting to see. The command will be-

plot3d(x=X, y=Y, z=Z, type="s", col=rainbow(n=10), xlab="X", ylab="Y",
zlab="Z", size=5, box=F)

And the output of this command is the main figure of this article, have a look again.
This plot gives us no added advantage, in fact it is very difficult to understand, but I can not resist myself (because the plot is so beautiful now!).

Now, let’s do some fancy animations. Let’s rotate the plot automatically so that we can relax and see every aspect of our three dimensional plot.
Here are the commands-

M <- par3d("userMatrix")
play3d( par3dinterp( userMatrix=list(M,rotate3d(M, angle=pi,x=1 , y=0,z= 0) ) ), duration=10)

This command will rotate the plot for 10 seconds in the X axis at 180 degrees.
If you want to rotate it in Y axis, just put x=0 and y=1. And by changing the values of angle, x, y and z we can make the plot rotate in every possible way.

Now let’s do something more interesting- let’s write a simple function that would make the plot rotate automatically in several ways for a given number of times, so that we can have very well and details look of it.

The function is-

plot3d(x=X,y=Y,z=Z,type="s",col=rainbow(n=10),xlab="X",ylab="Y", zlab="Z",size=5,lwd=15,box=F)
M <- par3d("userMatrix")
st<-function(n,t)  {
for (i in 1:n) {
play3d( par3dinterp( userMatrix=list(M,rotate3d(M, pi, 1, 0, 0) ) ), duration=t )
play3d( par3dinterp( userMatrix=list(M,rotate3d(M, pi, 0, 1, 1) ) ), duration=t)
play3d( par3dinterp( userMatrix=list(M,rotate3d(M, pi, 0, 1, 0) ) ), duration=t)
play3d( par3dinterp( userMatrix=list(M,rotate3d(M, pi, 1, 0, 1) ) ), duration=t)
}
}

Now write-

st(20,2)

Watch and enjoy.

There are so many other interesting things in rgl package. Those who are interested in statistical programming (especially in R) please try it and share your knowledge and experience and help me to enhance mine.

Category:
Tag: