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

如何在R中绘制含三维交互项的4D预测结果曲面?

解决方案:在R中展示三维参数与概率的3D可视化

针对你需要同时展示X、Y、Z三维参数及对应Probability的需求,下面提供几个直接可用的方案,解决你提到的现有包的痛点:

方案1:用plot_ly直接绘制带颜色映射的3D散点/曲面

plot_ly并非只能自动识别数据关系,你可以手动指定每个维度的映射,完全基于已生成的预测结果绘图:

3D散点图(适合展示所有离散预测点)

library(plotly)

# 直接用散点映射三维位置与颜色
plot_ly(example_results, x = ~x, y = ~y, z = ~z, color = ~probability,
        type = "scatter3d", mode = "markers",
        marker = list(size = 3, colorbar = list(title = "Probability"))) %>%
  layout(scene = list(xaxis = list(title = "X"),
                      yaxis = list(title = "Y"),
                      zaxis = list(title = "Z")))

3D曲面图(适合连续网格数据)

如果你的数据是规则网格(像示例中用expand.grid生成的),可以把probability转换为矩阵格式,绘制曲面并将颜色映射到概率:

library(dplyr)
library(plotly)

# 先按x、y、z排序数据
example_results_sorted <- example_results %>% arrange(x, y, z)

# 构建概率的三维数组
prob_array <- array(example_results_sorted$probability, 
                    dim = c(length(unique(example_results$x)),
                            length(unique(example_results$y)),
                            length(unique(example_results$z))))

# 绘制单z层曲面,颜色映射概率
plot_ly() %>%
  add_surface(z = ~prob_array[,,1], x = ~unique(example_results$x), y = ~unique(example_results$y),
              surfacecolor = ~prob_array[,,1], colorbar = list(title = "Probability")) %>%
  layout(scene = list(xaxis = list(title = "X"),
                      yaxis = list(title = "Y"),
                      zaxis = list(title = "Z")))
# 若要展示多z层,可循环调用add_surface并调整opacity实现叠加

方案2:用rgl包实现灵活的3D点可视化

rgl包对3D图形的控制度极高,能轻松分离位置坐标(X/Y/Z)和颜色映射(Probability):

library(rgl)

# 打开3D绘图设备
open3d()

# 绘制3D点,位置对应x/y/z,颜色根据probability映射
points3d(example_results$x, example_results$y, example_results$z,
         col = colorRampPalette(c("blue", "red"))(100)[cut(example_results$probability, 100)],
         size = 2)

# 添加坐标轴标签
axes3d(xlab = "X", ylab = "Y", zlab = "Z")

# 添加颜色图例
legend3d("topright", legend = seq(0, 1, by = 0.2),
         col = colorRampPalette(c("blue", "red"))(6),
         pch = 16, cex = 0.8, title = "Probability")

# 保存图形(可选)
# rgl.postscript("3d_prob_plot.pdf", "pdf")

方案3:用plot3D包的scatter3D函数

plot3D包专门针对科学可视化设计,语法简洁,能快速实现位置与颜色的分离:

library(plot3D)

# 绘制3D散点,colvar指定颜色映射的变量为probability
scatter3D(x = example_results$x, y = example_results$y, z = example_results$z,
          colvar = example_results$probability,
          xlab = "X", ylab = "Y", zlab = "Z",
          main = "3D Parameter Space with Probability",
          col = colorRampPalette(c("navy", "orange", "red"))(100),
          pch = 16, cex = 0.7)

# 添加颜色条
colorbar(col = colorRampPalette(c("navy", "orange", "red"))(100),
         at = seq(0, 1, by = 0.1), label = "Probability")

关键说明

  • 若数据是规则网格,曲面图能更直观展示概率的连续变化;若为离散预测点,散点图更合适。
  • 所有方案都完全基于你已生成的example_results数据,无需额外拟合模型,直接映射已有结果。

内容的提问来源于stack exchange,提问作者Tess H

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 23:31:25