[R] 3dscatterplot -different colors for different factors of the same variable
Dieter Menne
dieter.menne at menne-biomed.de
Tue May 13 21:02:27 CEST 2008
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)
More information about the R-help
mailing list