简体   繁体   English

3D散点图中的图片凸包

[英]Picture Convex hull in 3D Scatter Plot

I followed the tutorial about 3D visualization using the package "rgl" here 我在这里使用软件包“ rgl”关注有关3D可视化的教程

So I was able to draw a 3D Scatter Plot with "iris" data and create an ellipsoid surrounding 95 % of the data points: 因此,我能够使用“ iris”数据绘制一个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)

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: 我知道与其他数据集相比,前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)

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, ie a polyhedron consisting of triangel areas which combine three outer data points each. 在2D绘图中,我希望使用环绕所有数据点的多项式鞭子,但在3D中,像凸包的东西就足够了,即由三角形区域组成的多面体,每个三角形区域结合了三个外部数据点。

I think the function convhulln() using the QuickHull algorithm in the package "geometry" would be helpful but I am not able to use this. 我认为在软件包“ geometry”中使用QuickHull算法的功能convhulln()会有所帮助,但我无法使用它。

Does somebody have an idea how to picture such a convex hull in the rgl plot? 有人知道如何在rgl图中描绘出这样的凸包吗? 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. 是否也可以用PLOT3D格式封装要做到这一点,因为有一个伟大的教程在这里 ,我可以使用,使我自己的数据的漂亮曲线。

I am "only" a Biologist using R for science and not a mathematician or R programmer, so please explain your solution for me. 我“只是”使用R进行科学研究的生物学家,而不是数学家或R程序员,因此请为我解释您的解决方案。 Thanks a lot. 非常感谢。

Hey found out the answer here it is: 嘿,找到答案了:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM