在Plotly中绘制和添加曲面的问题

问题描述 投票:1回答:1

我是一个R新手,目前尝试将我的数据绘制在带有曲面的三维图表中。我尝试使用这个包,但没有做到。

我有一个矩阵d3(见下文),但在图中(见下面的链接),z或x等于矩阵中的数据。在图中,x始终等于1,z是计数。

我需要在代码中更改什么?

这可能是我创建矩阵的方式吗?

d3 <- data.matrix(mydataframe[1:3])
View(d3)
         z         y         x
[1,] 75.33333 1566.6667 0.2727273 
[2,] 76.66667 1585.6667 0.2788845
[3,] 75.33333 1462.6667 0.2384615
[4,] 77.66667 1340.6667 0.2489796
[5,] 75.66667 1328.3333 0.3658537
[6,] 83.33333 1315.6667 0.3459716
[...] ...

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

这是我失败的Plotly 3d图的一个例子

如果有人知道如何用曲面不同地绘制表面的3个变量,我真的很感激它。

这是一个3d回归的例子,我想以相同的颜色呈现我的数据

干杯。

r charts 3d plotly surface
1个回答
1
投票

z应该是一个矩阵。您可以使用outer,如下所示:

x <- seq(0, 8, length.out=100)
y <- seq(0, 3, length.out=30)
z <- t(outer(x, y, FUN=function(x,y) 5/3*y + 4*exp(-(x-4)^2)))

library(plotly)
plot_ly(x=x, y=y, z=z) %>% 
  add_surface() %>%
  layout(
    scene = list(
      xaxis= list(title="X"),
      yaxis= list(title="Y"),
      aspectmode = 'manual', 
      aspectratio = list(x=1, y=1, z=.5)
    )
  )

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.