本文介绍了3D 散点图中的凸包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用包rgl"遵循了有关 3D 可视化的教程 这里

I followed the tutorial about 3D visualization using the package "rgl" here

所以我能够用虹膜"数据绘制一个 3D 散点图,并创建一个围绕 95% 数据点的椭球:

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)

我知道前 50 个数据点与数据集的其余部分相比属于不同的群体,因此以不同的方式为它们着色,并用两个椭球覆盖它们:

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)

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

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.

我认为使用geometry"包中的 QuickHull 算法的 convhulln() 函数会有所帮助,但我无法使用它.

I think the function convhulln() using the QuickHull algorithm in the package "geometry" would be helpful but I am not able to use this.

有人知道如何在 rgl 图中绘制这样的凸包吗?是否也可以使用 plot3D 包来做到这一点,因为有一个很棒的教程 这里 我可以用我自己的数据制作一个漂亮的图.

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.

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

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.

推荐答案

嘿,答案在这里:

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)

这篇关于3D 散点图中的凸包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:46