将一个3D形状添加到现有的rgl对象中:alphashape3d

4
我使用“vegan”软件包中的metaMDS()对物种数据集进行了排序分析。我正在使用多个维度,因此想在3D图中显示其中三个维度。虽然“vegan”软件包具有一些3D功能,但结果并不是很清晰。它可以使用orglspider()函数显示“蜘蛛网状图”,这很好看,但仍然不够清晰。我更喜欢像“ordihull”那样的3D图形。使用“alphashape3d”软件包可以实现这一点。它允许我精确地绘制我需要的3D形状。问题是我需要在同一框架中绘制3个这样的形状。我尝试了"add=T",但它不起作用。我认为我可能必须改变调用第二个和第三个形状的方式,但我不知道如何做到这一点。这是我迄今为止尝试过的一个示例:
### 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)
1个回答

2

我发帖问问题可能有些仓促,但是我已经自己找到了答案。我通过使用不同的包来创建凸壳解决了这个问题,其中add=T部分确实起作用。我使用了"geometry"包,具体如下:

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接