plot3d, color points by group

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

plot3d, color points by group

Kari-4
Hello

I've created a 3d scatterplot, and had no problems labeling the points.
However, I've been really struggling to change the color of the points based
on a factor (see 'group' below).  Is such a thing possible?

My data look like this:

food group      x      y      z
apple  fruit  0.216 -0.112 -0.893
orange fruit  0.814  0.097  0.460
broccoli veg -0.239  0.240 -0.425
banana  fruit  0.222  0.968 -0.050
parsnip  veg  0.139  0.897  0.378
garlic     veg -0.104  0.510 -0.400

pca <-  read.table(...
p3d<- plot3d(pca$x, pca$y, pca$z, xlab="Component 1", ylab="Component 2",
zlab="Component 3", col="blue",box=FALSE, size=5)
food.v<-as.vector(pca$food)
text3d(pca$x, pca$y, pca$z, food.v, cex=0.9, adj = 1)

Many thanks,
Leigh

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: plot3d, color points by group

Dieter Menne
Kari-4 wrote
I've created a 3d scatterplot, and had no problems labeling the points.
However, I've been really struggling to change the color of the points based
on a factor (see 'group' below).  Is such a thing possible?
Its always polite to make the example self-running by adding the library and the input. But your's was close.
Dieter


library(rgl)
ttt = textConnection("
food group      x      y      z
apple  fruit  0.216 -0.112 -0.893
orange fruit  0.814  0.097  0.460
broccoli veg -0.239  0.240 -0.425
banana  fruit  0.222  0.968 -0.050
parsnip  veg  0.139  0.897  0.378
garlic     veg -0.104  0.510 -0.400
")

colors =c("green","blue","red")
pca <-  read.table(ttt,header=TRUE)
p3d<- plot3d(pca$x, pca$y, pca$z, xlab="Component 1", ylab="Component 2",
zlab="Component 3",
col=as.integer(pca$group) ,
box=FALSE, size=5)
food.v<-as.vector(pca$food)
text3d(pca$x, pca$y, pca$z, food.v, cex=0.9, adj = 1)