3D散点图中的凸包图像

3
我按照使用“rgl”包进行3D可视化的教程进行了操作,教程链接在这里
我成功绘制了一个“鸢尾花”数据的3D散点图,并创建了一个包围95%数据点的椭球体:
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)

我知道前50个数据点与数据集的其余部分属于不同的人群,因此请用不同的颜色标记它们,并使用两个椭圆覆盖它们:

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)

虽然两个种群之间可以清楚地区分开来,但椭球体彼此相交。因此,椭球不能很好地可视化数据点。在2D图中,我会选择使用多项式包围所有数据点,但在3D中,应该使用凸包,即由三个外部数据点组成的三角形区域组成的多面体。
我认为使用“geometry”包中的QuickHull算法的convhulln()函数将很有帮助,但我无法使用它。
有人有想法如何在rgl绘图中呈现这样的凸包吗?是否也可以使用plot3D包做到这一点,因为这里有一个很棒的教程here,我可以用它来制作自己的数据漂亮的图形。
我只是一个生物学家,使用R进行科学研究,而不是数学家或R程序员,请为我解释你的解决方案。非常感谢。

你尝试过R几何包吗?如果你在使用它时遇到问题,我可以帮忙。 - Derek Corcoran
是的,我尝试过了。我认为函数convhulln()可能会有帮助,但我无法将结果包含在rbl或plot3D图中。“convhulln(cbind(x,y,z))”会给出一个矩阵,但整数值非常高,比cbind(x,y,z)还要高。所以要么是某些东西不起作用,要么是我没有理解输出。 - ossesso
1个回答

6

嘿,我在这里找到了答案:

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)

在你上面所做的操作之后,我加入了以下内容:
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)

非常感谢!这正是我正在寻找的,并且它也能很好地与我的数据配合使用。 - ossesso

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