[R] interactive rotatable 3d scatterplot

Yasir Kaheil kaheil at gmail.com
Sat May 3 02:26:41 CEST 2008


#Install library rgl
#here is the is the function:
rgl.plot3d<-function(z, x, y, cols="red",axes=T,new=T)
{xr<-range(x)
x01<-(x-xr[1])/(xr[2]-xr[1])
yr<-range(y)
y01<-(y-yr[1])/(yr[2]-yr[1])
zr<-range(z)
z01<-(z-zr[1])/(zr[2]-zr[1])

if(new) rgl.clear()
if(axes)
	{xlab<-pretty(x)
	ylab<-pretty(y)
	zlab<-pretty(z)
	xat<-(xlab-xr[1])/(xr[2]-xr[1])
	yat<-(ylab-yr[1])/(yr[2]-yr[1])
	zat<-(zlab-zr[1])/(zr[2]-zr[1])
	rgl.lines(c(0,1.1),0,0)
	rgl.lines(0,c(0,1.1),0)
	rgl.lines(0,0,c(0,1.1))	
	rgl.texts(xat,-.05,-.05,xlab)
	rgl.texts(-.05,yat,-.05,ylab)
	rgl.texts(-.05,-.05,zat,zlab)
	rgl.texts(c(0.5,-.15,-.15),c(-.15,.5,-.15),c(-.15,-.15,.5),
		c(deparse(substitute(x)),deparse(substitute(y)),deparse(substitute(z))))
	}

rgl.spheres(x01,y01,z01,.01,color=cols)
}

#and here is how you call it
library(rgl)
data(iris)

iris.pc<-prcomp(iris[,1:4],scale=T)
rgl.plot3d(iris.pc$x[,1],iris.pc$x[,2],iris.pc$x[,3])
# different colors
rgl.plot3d(iris.pc$x[,1],iris.pc$x[,2],iris.pc$x[,3],col=unclass(iris[,5])+1)

#you could also change the labels and everything else. Enjoy!



Mark W Kimpel wrote:
> 
> I would like to create a 3d scatterplot that is interactive in the sense
> that I can spin it on its axes to better visualize some PCA results I
> have.
> What are the options in R? I've looked at RGL and perhaps it will suffice
> but it wasn't apparent from the documentation I found.
> 
> Any demo scripts available for a package that will work?
> 
> Mark
> 
> -- 
> Mark W. Kimpel MD ** Neuroinformatics ** Dept. of Psychiatry
> Indiana University School of Medicine
> 
> 15032 Hunter Court, Westfield, IN 46074
> 
> (317) 490-5129 Work, & Mobile & VoiceMail
> (317) 663-0513 Home (no voice mail please)
> 
> ******************************************************************
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: http://www.nabble.com/interactive-rotatable-3d-scatterplot-tp17030023p17030164.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list