繁体   English   中英

R 中的 3d 曲面图与 plotly

[英]3d Surface Plot in R with plotly

我希望使用 R plotly库来创建 x、y、z 坐标数据的 3d 曲面图,类似于以下链接中显示的内容:

https://plot.ly/r/3d-surface-plots/

似乎 plot_ly 函数要求 z 坐标位于维度 x * y 的矩阵中,如在链接示例中使用的datasets::volcano中所示。 我很感激有关如何构建此矩阵的一些指导。 这是我的示例 x,y 坐标数据:

## x coordinates
xSeq = seq(0, 1, .01)
## y coordinates
ySeq = seq(0, 1, .01)
## list with x, y coordinates
exmplList = list(x = xSeq, y = ySeq)

z 坐标将通过 x,y 对的公式计算(此处使用的示例公式为 x + y)。 我玩过类似的东西:

exmplList = within(exmplList, z <- matrix(x + y, nrow = length(xSeq), ncol = length(ySeq)))

但这并不能实现我想要实现的配对组合。

Plotly surface 需要一个矩阵,所以你可以直接使用这个位:

z = matrix(xSeq + ySeq, nrow = length(xSeq), ncol = length(ySeq))

而不是做一个清单。 因此,通过运行以下代码:

## x coordinates
xSeq = seq(0, 1, 0.01)
## y coordinates
ySeq = seq(0, 1, 0.01)
## list with x, y coordinates
z = matrix(xSeq + ySeq, nrow = length(xSeq), ncol = length(ySeq))

fig = plot_ly(z = ~z) %>% add_surface()
fig

得到如下图: 3D 绘图

您可能需要单击并稍微旋转一下才能看到飞机。 希望能帮到你,加油。

暂无
暂无

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

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