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.

## Comments

## Re: Three dimensional plots using rgl package

Good topic Tanvir. Thanks for posting.

If you add some reproducible data that would benefit average users like me. For example, in you code what is

`X, Y, Z`

? I assume that they are vectors, right?In short, an article is most useful if it is self contained. An example with a real example helps understanding things better.

Another thing is, if you have a main figure for the article, don't show it again in the body of the article. This will save some bandwidth for people in Bangladesh. Instead, you can just refer to the figure above and give the code near the bottom.

Keep up the good work.

## Re: Three dimensional plots using rgl package

Thank you sir. I have tried to fix the pitfalls. The data has 133 rows, so I put just a few of them, is it OK?

Good joke sir ("....average users like me...."). It sounds funny when a very much capable and experienced R user (and statistician) like you call himself "average user".

Again, thank you sir for your very much encouraging and suggestive words.

## Re: Three dimensional plots using rgl package

Its better now.

## Re: Three dimensional plots using rgl package

Here is one particular example of 3d plots using not-so-fancy packages (

basicandlattice):A brief description of the problem:

Click on the hyperlinked code to see the output:Warning: Running this piece of code will generate 4 graphs in your R working directory (see

`getwd`

).## Re: Three dimensional plots using rgl package

Thanks Ehsan vaia, you have given something great. I am so honored and glad that you have written my post and also commented on it. But the function will really take sometime for me to digest (I am a very much beginner).

I have given the codes for rotating the plot automatically, your valuable suggestion,comment and more such functions are desired....

Thank you again vaia.

## Re: Three dimensional plots using rgl package

Don't read too much into it, ignore the data generating procedure (i just gave something extra, if someone wants to get the whole perspective of the idea): just see the format of

`powerMatrix`

(print it in the console), and then see the use of`persp`

(basic) and`wireframe`

(lattice) :)## Re: Three dimensional plots using rgl package

Thanks again vaia, now I have started to gain some courage to try the function.... In my last comment I made a mistake; I wrote "you have written..." it would be "you have read my post and also commented on it" instead.

Hope we all will be able to learn many other amazing things from you.

Good wishes vaia.

## Re: Three dimensional plots using rgl package

I just upgraded to R version 2.12.1 and rgl version 0.92.798. Unlike the previous versions, rgl.snapshot() no longer works. In other words, except for a screen save, there's no way that I can find to save the image/movie.

Do you know of a work-around?

Is there a way to inform those that are maintaining the package?

George

## Re: Three dimensional plots using rgl package

It is unfortunate that the most recent version of rgl package does not support rgl.snapshot command.But you can use rgl.postscript command to save the 3d plot in ps,eps or pdf format. The command is-

Use gimp or photoshop(in windows) to convert the pdf(or ps/eps) plot to your favourite format(like png).

Hope this will help you.

## Re: Three dimensional plots using rgl package

There are now several of us in the same boat. I just got another panic e-mail.

It appears to be rgl version that is the problem. And, the postscript route may or may not work. I have several runs where it displays the right image, but the saved .eps file gives the wrong shading and colors.

I haven't tested it yet, but one guy is getting huge files. This is a big problem.

## Re: Three dimensional plots using rgl package

Yes the saved eps file is not of good quality. Probably it would be better if you first save the plot in pdf format and then convert it into png(or any other format). I am not happy with the saved pdf image also, but it is certainly better than the eps. Yes, this is a big problem. Hope the next version will support snapshot again.

## Re: Three dimensional plots using rgl package

Good day!

I've been surfing the net for long hours to look for R command in plotting 2D and 3Dimensional solution space... Fortunately, i found this page..

I just want to ask if there is R command to plot the data points and make use of symbols as group indicator.. to come up with a conclusion that a points of the same symbol belongs to the same group.. something like that.. it find me hard to do so..

Any suggestions please.. any help will do and very much appreciated.. THANKS!

## Re: Three dimensional plots using rgl package

Let, we want to plot 2 vectors,x and y, on a two dimensional space,and we want different symbols and colors for each of the situations-

(1) x is less than y

(2) x=y

(3) x>y

Let the vectors are-

x<-c(1,2,1,5,4,6,7,3,4,2,1)

y<-c(0,2,3,5,7,2,0,1,2,3,4)

Now, try the codes-

And finally-

`plot(x,y,pch=p(x,y),col=p(x,y))`

Is this the thing you are trying to do?

## writeWebGL

Have you seen the new funciton writeWebGL() that creates a html/javascript bundle to make an interactive snapshot of your plot?

see e.g.

http://geolabs.wordpress.com/2012/04/24/r-interactive-3d-webgl-plot-of-time-space-cube-with-rgl/

## Add new comment