3D 散点图中的凸包

oss*_*sso 2 3d r convex-hull qhull

我在此处使用包“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)
Run Code Online (Sandbox Code Playgroud)

我知道前 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)
Run Code Online (Sandbox Code Playgroud)

尽管可以清楚地区分两个种群,但椭球体彼此接触。因此,椭球不是数据点的良好视觉表示。在 2D 绘图中,我更喜欢使用环绕所有数据点的多项式,但在 3D 中,像凸包这样的东西应该足够了,即由三角形区域组成的多面体,每个三角形区域都结合了三个外部数据点。

我认为使用“几何”包中的 QuickHull 算法的 convhulln() 函数会有所帮助,但我无法使用它。

有人知道如何在 rgl 图中描绘这样的凸包吗?是否也可以用PLOT3D格式封装要做到这一点,因为有一个伟大的教程在这里,我可以使用,使我自己的数据的漂亮曲线。

我“只是”使用 R 进行科学的生物学家,而不是数学家或 R 程序员,所以请为我解释您的解决方案。非常感谢。

Der*_*ran 5

嘿,在这里找到了答案:

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)
Run Code Online (Sandbox Code Playgroud)

在您执行上述操作之后,我添加了以下内容:

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)
Run Code Online (Sandbox Code Playgroud)