3D散点图中的图片凸包 [英] Picture Convex hull in 3D Scatter Plot
问题描述
我遵循了有关使用 rgl软件包进行3D可视化的教程在这里
I followed the tutorial about 3D visualization using the package "rgl" here
所以我能够绘制3D散点图使用 iris数据并创建围绕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个数据点属于不同的总体,因此请在广告中为它们上色ifferent方法,我们用两个椭圆形覆盖它们:
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.
我认为使用几何包中的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散点图中的图片凸包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!