I have done this ordination analysis on a species dataset using metaMDS()
from the 'vegan' package. I am using more than one dimension so I thought it would be nice display three of these dimensions in a 3d plot. The 'vegan' package has some 3d functionality but the results are not very clear. It can display this "spider" using the orglspider()
function which is kind of nice but still not very clear. I rather have something like 'ordihull' but than as a 3d graph. Using the 'alphashape3d' package I can get this done. It allows me to draw a 3d shape exactly as I need it. The problem is that I need 3 of these shapes plotted in the same frame. I tried "add=T"
but this doesn't work. I think I might have to change the way the second and third shapes are called, but I have no idea how to do this. Here is an example of what I have tried so far:
### Load the packages
require(vegan)
require(alphashape3d)
### Create some data
a <- c(0.5654292, 0.1960973, 0.0000000, 0.2536315, 0.1960973, 0.4658166, 0.3315565, 0.6865024, 0.7044823, 0.4385855)
b <- c(0.7093823, 0.2409255, 0.3269156, 0.0000000, 0.0000000, 0.3269156, 0.0000000, 0.0000000, 0.0000000, 0.4625978)
c <- c(0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.1022538, 0.4618177, 0.0000000, 0.0000000, 0.0000000)
d <- c(0.00000000, 0.16692171, 0.00000000, 0.22649864, 0.09400543, 0.42456471, 0.79331157, 0.72986751, 0.93837435, 0.65106335)
loc <- c("U", "U", "U", "A", "A", "U", "A", "A", "A", "U")
data <- data.frame(a,b,c,d)
#### Run the mds analysis to obtain the points that should be plotted
mdsB <- metaMDS(data, distance = "bray", autotransform = FALSE, trace = 0,k=3)
### Split the points in the mds object up into two groups
mdsBA <- mdsB
mdsBU <- mdsB
mdsBA$points <- mdsBA$points[which(loc=='A'), ]
mdsBU$points <- mdsBU$points[which(loc=='U'), ]
### Create the 3d shape just like the ordihull function does for 2d shapes
ashape3d.A <- ashape3d(unique(as.matrix(mdsBA$points[,1:3])), alpha = 1.19)
ashape3d.U <- ashape3d(unique(as.matrix(mdsBU$points[,1:3])), alpha = 1.19)
### plot the first shape and try to add the second one.
plot(ashape3d.A,bycom=TRUE,tran=0.2,shininess=0, col="lightgoldenrod")
plot(ashape3d.U,bycom=TRUE,tran=0.2,shininess=0, col="lightgoldenrod", add=T)
I probably should have waited with posting my question as I have found an answer myself. I solved the problem by using a different package to create the convex hulls for which the add=T
part actually did work. I used the "geometry" package as follows:
library("geometry")
A <- mdsBA$points[,1:3]
F <- mdsBU$points[,1:3]
A.surf <- t(convhulln(A))
U.surf <- t(convhulln(U))
rgl.triangles(A[A.surf,1],A[A.surf,2],A[A.surf,3],col="lightgoldenrod",alpha=0.5, add=T)
rgl.triangles(U[U.surf,1],U[U.surf,2],U[U.surf,3],col="lightgoldenrod",alpha=0.5, add=T)