Moreno Ignazio Coco
2008-May-13 16:49 UTC
[R] 3dscatterplot -different colors for different factors of the same variable
Dear R users, I was trying to do a 3d scatterplot for the following set of datas: "obj" "time" "X" "Y" "1" "yellow" "333" "388.7" "492.3" "2" "yellow" "567" "388.7" "492.3" "3" "green" "621" "135.5" "371.7" "4" "green" "1039" "135.5" "371.7" "5" "red" "1373" "744.1" "205.0" "6" "red" "1763" "744.1" "205.0" The points should be drew in the plot taking information of "time", "X" and "Y". The colors of these points should be assigned according to the information contained in variable "obj". What I did but it didn't work out is: 1) Plotting only one set of points (the green one) s3d<-scatterplot3d(greenpts[,3],greenpts[,2],greenpts[,4],type="p",scale.y=4, angle=10, color="green") and try to overlay the others (as you normally would do with "plot"): s3d$points3d(redpts[,3],redpts[,2],redpts[,4],col="red",type="o") s3d$points3d(yellowpts[,3],yellowpts[,2],yellowpts[,4], col="yellow",type="o") s3d$points3d(bluepts[,3],bluepts[,2],bluepts[,4],col="blue",type="o") s3d$points3d(pinkpts[,3],pinkpts[,2],pinkpts[,4],col="pink",type="o") R has plotted only the first set and wasn't able to overlay other points. 2) I have generated a "for" loop that should have taken points information ,arranged into a list object, plotting them one by one. - listpoints = list(X,Y,time) -color<-rep("black",26) ##solution found in one of the discussion around scatterplot color[mat1$obj=="green"]<-"green" color[mat1$obj=="yellow"]<-"yellow" color[mat1$obj=="red"]<-"red" color[mat1$obj=="pink"]<-"pink" color[mat1$obj=="blue"]<-"blue" for (i in 1:26) points3d(listpoints[i],type=1, col=color[i]) Also this way didn't work out. Do you have any solution? Many thanks Moreno P.S. I have tried also points3d(rgl) trying to follow an explanation given in the forum but i haven't understood how to set the function. -- The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.
Dieter Menne
2008-May-13 19:02 UTC
[R] 3dscatterplot -different colors for different factors of the same variable
Moreno Ignazio Coco <M.I.Coco <at> sms.ed.ac.uk> writes:> I was trying to do a 3d scatterplot for the following set of datas: > > "obj" "time" "X" "Y" > "1" "yellow" "333" "388.7" "492.3" > "2" "yellow" "567" "388.7" "492.3" > "3" "green" "621" "135.5" "371.7" > "4" "green" "1039" "135.5" "371.7" > "5" "red" "1373" "744.1" "205.0" > "6" "red" "1763" "744.1" "205.0" > > The points should be drew in the plot taking information of "time", > "X" and "Y". The colors of these points should be assigned according > to the information contained in variable "obj". > What I did but it didn't work out is: > > 1) Plotting only one set of points (the green one) > > s3d<-scatterplot3d(greenpts[,3],greenpts[,2],greenpts[,4],type="p",scale.y=4, > angle=10, color="green")This does not plot the "green" points (you did not tell us how to get the data, please show the way you read these in). With a bit of guessing, I assume that you plotted all 6 points in green.> and try to overlay the others (as you normally would do with "plot"): > s3d$points3d(redpts[,3],redpts[,2],redpts[,4],col="red",type="o")I don't understand this one. Try the following # See also http://finzi.psych.upenn.edu/R/Rhelp02a/archive/62479.html library(scatterplot3d) pts = read.table("d3data.txt") pts # gives for me: # obj time X Y #1 yellow 333 388.7 492.3 #2 yellow 567 388.7 492.3 #3 green 621 135.5 371.7 #4 green 1039 135.5 371.7 #5 red 1373 744.1 205.0 #6 red 1763 744.1 205.0 # must use larger points and pch=16 here, otherwise yellow is almost invisible scatterplot3d(pts$X,pts$time,pts$Y,type="p",scale.y=4, angle=10, color=pts$obj,pch=16) # Here come the two rgl-base alternatives. Note that rgls for historical reasons has two interfaces which you should not mix; only use one of the two methods. You can rotate the rgl plot with the mouse, and also add axis. rgl is an amazing package; thanks, Duncan. Dieter library(rgl) # 3d interface open3d() points3d(x=pts$X,y=pts$time,z=pts$Y,pch=16,color=pts$obj,size=5) # rgl.* interface rgl.open() rgl.points(x=pts$X,y=pts$time,z=pts$Y,pch=16,color=pts$obj,size=5)