You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Hypervolume与rgl绘制平滑3D实体展示数据超体积重叠

如何用Hypervolume包绘制平滑3D超体积实体图

我想要用Hypervolume包结合rgl绘制类似下图的3D平滑实体图,展示两组数据的超体积重叠情况:
目标效果图

但运行以下代码后,得到的却是散点图(如下图),无法得到平滑的3D实体,请问该怎么调整?
当前错误图


现有代码

library(hypervolume)

hv1 = hypervolume_svm(specie1, name = "specie1")
summary(hv1)

hv2 = hypervolume_svm(specie2, name = "specie2")
summary(hv2)

hv_set1 <- hypervolume_set(hv1, hv2, check.memory=FALSE)

hypervolume_overlap_statistics(hv_set1) 

plot(hv_set1, main="a. Specie 1 vs Specie 2",show.3d=T, 
     show.axes=T, show.frame=F, show.legend=F, colors = c("green", "red"),
     show.centroid = F)

数据示例

specie1头部数据

> dput(head(specie1))
structure(list(Comp.1 = c(0, 1, 0.782367218362161, 0.0189539143285594, 
0.955187776070299, 0.456054304244467), Comp.2 = c(0.506016598824208, 
0.216422495760754, 0.919462144900217, 0.681372219919292, 0, 0.476979813131793
), Comp.3 = c(0.435356477269505, 0.537011422304908, 0.738929724677427, 
0.289196178254202, 0.621418853480231, 0.603396889027632)), row.names = c(NA, 
6L), class = "data.frame")

specie2头部数据

> dput(head(specie2))
structure(list(Comp.1 = c(0.472340614903222, 0.325927835046529, 
0.245534540132887, 0.272887943374725, 0.384533458010776, 0.439560326282721
), Comp.2 = c(0.719317859710476, 0.225434087659521, 0.476358549235223, 
0.323667678599905, 0.803420829967659, 0.48750667692359), Comp.3 = c(0, 
0.90482339881728, 0.744437223931766, 0.21705479302961, 0.694891050735555, 
0.571969487191824)), row.names = 21:26, class = "data.frame")

解决方案

当前得到散点图的核心原因是:生成的超体积对象采样点不足,或者绘图时未指定曲面渲染模式。以下两种方法可以解决问题:

方法1:调整SVM超体积的采样参数

hypervolume_svm默认采样点较少,需手动增加采样点数,同时指定绘图类型为曲面:

library(hypervolume)
library(rgl)

# 创建SVM超体积时增加采样点数,确保足够生成平滑曲面
hv1 = hypervolume_svm(specie1, name = "specie1", n.samples = 10000)
hv2 = hypervolume_svm(specie2, name = "specie2", n.samples = 10000)

hv_set1 <- hypervolume_set(hv1, hv2, check.memory=FALSE)

# 强制绘制曲面,设置透明度方便查看重叠区域
plot(hv_set1, main="a. Specie 1 vs Specie 2", show.3d=TRUE, 
     show.axes=TRUE, show.frame=FALSE, show.legend=FALSE, 
     colors = c("green", "red"), show.centroid = FALSE,
     plot.type = "surface", alpha = 0.5)

方法2:使用高斯核超体积(推荐)

hypervolume_gaussian天生适合生成平滑的3D实体,无需额外调整采样点,只需指定曲面渲染:

library(hypervolume)
library(rgl)

# 创建高斯核超体积
hv1 = hypervolume_gaussian(specie1, name = "specie1")
hv2 = hypervolume_gaussian(specie2, name = "specie2")

hv_set1 <- hypervolume_set(hv1, hv2, check.memory=FALSE)

# 绘制平滑3D曲面
plot(hv_set1, main="a. Specie 1 vs Specie 2", show.3d=TRUE, 
     show.axes=TRUE, show.frame=FALSE, show.legend=FALSE, 
     colors = c("green", "red"), show.centroid = FALSE,
     plot.type = "surface", alpha = 0.5)

关键说明

  • plot.type = "surface"是强制绘制曲面的核心参数,默认情况下采样点不足会自动切换为散点图。
  • alpha参数设置透明度,能更清晰展示两个超体积的重叠区域。
  • 使用hypervolume_svm时,n.samples建议设置为5000以上,点数越多曲面越平滑,但计算时间会相应增加。

内容的提问来源于stack exchange,提问作者Manuela Portas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 06:04:55