用plotly在R中绘制三维曲面

用plotly在R中绘制三维曲面,r,plotly,R,Plotly,我希望使用R plotly库创建x、y、z坐标数据的三维曲面图,类似于下面的链接所示: plot_y函数似乎要求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 =

我希望使用R plotly库创建x、y、z坐标数据的三维曲面图,类似于下面的链接所示:

plot_y函数似乎要求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)))

exmplList=in(exmplList,zPlotly曲面需要一个矩阵,因此您可以直接使用该位:

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
我们可以得到以下曲线图:

您可能需要单击并旋转一点才能看到飞机。希望有帮助,干杯。

z