-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3dplot.txt
More file actions
63 lines (52 loc) · 1.48 KB
/
Copy path3dplot.txt
File metadata and controls
63 lines (52 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
### https://stat.ethz.ch/pipermail/r-help/2008-May/161202.html
#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!
### http://stackoverflow.com/questions/5221283/3d-scatterplot-with-colored-spheres-with-r-and-rgl
library(rgl)
m <- read.csv(file="mem0.csv", sep = ",", head=TRUE)
mcol = m$val
i = 1
mdim = dim(m)
while (i <= mdim[1] ){
if (mcol[i] == 1){
mcol[i] = "red"
}else {
mcol[i] = "blue"
}
i = i +1
}
plot3d(m$x, m$y, m$z, col = mcol, type='s', size=0.1)