簡體   English   中英

3D散點圖中的圖片凸包

[英]Picture Convex hull in 3D Scatter Plot

我在這里使用軟件包“ rgl”關注有關3D可視化的教程

因此,我能夠使用“ 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)

我知道與其他數據集相比,前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格式封裝要做到這一點,因為有一個偉大的教程在這里 ,我可以使用,使我自己的數據的漂亮曲線。

我“只是”使用R進行科學研究的生物學家,而不是數學家或R程序員,因此請為我解釋您的解決方案。 非常感謝。

嘿,找到答案了:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM