# Re: [R] Plotting separate populations in scatterplot3d

From: Yasir Kaheil <kaheil_at_gmail.com>
Date: Tue, 06 May 2008 11:02:13 -0700 (PDT)

#Install library rgl
#here is the function which you need to run first: 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)

thanks
y

Eleca Dunham wrote:
>
> Hi,
>
> I'm having trouble plotting populations as separate colors and points in
> the
> 3d scatterplot package. I have a column with 4 different population names
> and 3 columns with my data. I want to plot each population with a
> different
> color and pch. In addition, I want to use the type="h" in my plotting so
> that I can see the points clearly in my graph( for publication purposes).
> I've also attached an example dataset.
>
> I simply want to show that each population clusters separately in space.
>
> I've attached a visual representation of what I want my data to look like.
> The scripts for this graph is as follows:
>
> data(trees)
> s3d <- scatterplot3d(trees, type = "h", color = "blue",
> angle = 55, scale.y = 0.7, pch = 16, main = "Adding elements")
>
> For some reason, R will not read my header, so I had to use the data.frame
> function to get R to read my data. This is a real pain because I have tons
> of data. I've tried the following to get R to recognize each population
> type
> separately:
>
> PopulationType.LK1<-PopulationType[LK1]
> PopulationType.LK2<-PopulationType[LK2]....
> points((PopulationType.LK1, col="red", pch=19), (PopulationType.LK2,
> col="blue", pch=22), .....)
>
> When I tweak the example tree code from above, R will plot all of my data
> no
> problem (with the rgl 3d graphs, not scatterplot3d), but only with one
> color
> and plotting character. It seems pretty straightforward, but I've spent 4
>
>
>
> Any help would be greatly appreciated.
>
> Regards,
> EJ
>
>
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

Yasir H. Kaheil, Ph.D.
Catchment Research Facility
The University of Western Ontario
```--
View this message in context: http://www.nabble.com/Plotting-separate-populations-in-scatterplot3d-tp17088785p17088937.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help