I followed the tutorial about 3D visualization using the package "rgl" here
So I was able to draw a 3D Scatter Plot with "iris" data and create an ellipsoid surrounding 95 % of the data points:
library("rgl")
data(iris)
x <- sep.l <- iris$Sepal.Length
y <- pet.l <- iris$Petal.Length
z <- sep.w <- iris$Sepal.Width
plot3d(x, y, z, col="blue", box = FALSE,
type ="s", radius = 0.15)
ellips <- ellipse3d(cov(cbind(x,y,z)),
centre=c(mean(x), mean(y), mean(z)), level = 0.95)
plot3d(ellips, col = "blue", alpha = 0.2, add = TRUE, box = FALSE)
I know that the first 50 data points belong to a different population compared the the rest of the dataset, so colour them in a different way and us two ellipsoids to cover them:
plot3d(x, y, z, col=c(rep("gold2",50),rep("forestgreen",100)), box = FALSE,
type ="s", radius = 0.15)
ellips1 <- ellipse3d(cov(cbind(x[1:50],y[1:50],z[1:50])),
centre=c(mean(x[1:50]), mean(y[1:50]), mean(z[1:50])), level = 0.999)
ellips2 <- ellipse3d(cov(cbind(x[51:150],y[51:150],z[51:150])),
centre=c(mean(x[51:150]), mean(y[51:150]), mean(z[51:150])), level = 0.999)
plot3d(ellips1, col = "gold2", alpha = 0.2, add = TRUE, box = FALSE)
plot3d(ellips2, col = "forestgreen", alpha = 0.2, add = TRUE, box = FALSE)
Although both populations can be clearly differentiated from each other, the ellipsoids touch each other. Therefore the ellipsoids are not a good visual representation of the data points. In a 2D Plot I would prefer to use a polynom whitch sourrounds all the data points, but in 3D something like a convex hull should be adequate, i.e. a polyhedron consisting of triangel areas which combine three outer data points each.
I think the function convhulln() using the QuickHull algorithm in the package "geometry" would be helpful but I am not able to use this.
Does somebody have an idea how to picture such a convex hull in the rgl plot? Is it also possible to do this with the plot3D package, since there is a great tutorial here which I could use to make a beautiful plot with my own data.
I am "only" a Biologist using R for science and not a mathematician or R programmer, so please explain your solution for me. Thanks a lot.
Hey found out the answer here it is:
library("rgl")
data(iris)
x <- sep.l <- iris$Sepal.Length
y <- pet.l <- iris$Petal.Length
z <- sep.w <- iris$Sepal.Width
plot3d(x, y, z, col="blue", box = FALSE,
type ="s", radius = 0.15)
ellips <- ellipse3d(cov(cbind(x,y,z)),
centre=c(mean(x), mean(y), mean(z)), level = 0.95)
plot3d(ellips, col = "blue", alpha = 0.2, add = TRUE, box = FALSE)
plot3d(x, y, z, col=c(rep("gold2",50),rep("forestgreen",100)), box = FALSE,
type ="s", radius = 0.15)
After what what you did above I added this:
library(geometry)
ps1 <- matrix(c(x[1:50],y[1:50],z[1:50]), ncol=3) # generate points on a sphere
ts.surf1 <- t(convhulln(ps1)) # see the qhull documentations for the options
convex1 <- rgl.triangles(ps1[ts.surf1,1],ps1[ts.surf1,2],ps1[ts.surf1,3],col="gold2",alpha=.6)
ps2 <- matrix(c(x[51:150],y[51:150],z[51:150]), ncol=3) # generate points on a sphere
ts.surf2 <- t(convhulln(ps2)) # see the qhull documentations for the options
convex2 <- rgl.triangles(ps2[ts.surf2,1],ps2[ts.surf2,2],ps2[ts.surf2,3],col="forestgreen",alpha=.6)