R 中的 3d 曲面图与 plotly [英] 3d Surface Plot in R with plotly

查看:51
本文介绍了R 中的 3d 曲面图与 plotly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

I am looking to use the R plotly library to create a 3d surface plot of x,y,z coordinate data, similar to what is shown at the link below:

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

It appears that the plot_ly function requires the z coordinates to be in a matrix of dimensions x * y, as seen in datasets::volcano, used in the linked example. I'd appreciate some guidance on how to construct this matrix. Here is my sample x,y coordinate data:

## 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)

The z coordinates would be calculated via a formula from the x,y pairs (example formula used here is x + y). I've played around with something like:

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

But that doesn't accomplish the pair combinations that I am trying to achieve.

解决方案

Plotly surface needs a matrix so you could simply use this bit directly:

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

Instead of doing a list. So, by running the following code:

## 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

One obtains the following plot:

You might need to click and rotate a bit to see the plane. Hope it helps, cheers.

这篇关于R 中的 3d 曲面图与 plotly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆