我有三个变量:x,y和z我想产生一个表面图。
z<-runif(50,0,1)
y<-runif(50,1,2)
x<-runif(50,3,6)
plot_ly(x = ~x, y = ~y, z= ~z) %>% add_surface()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Error: `z` must be a numeric matrix
z如果不是垂直轴对应的变量,究竟代表什么?我看过 Volcano 示例,其中他们使用矩阵来生成该图,但我仍然不确定 z 矩阵在该示例中代表什么。
我想要的是让某人绘制一个易于理解的 3D 函数,例如z=f(x,y) = x^2 + y^2使用该surface功能,plot_ly以便我了解如何基于三个变量生成绘图。
我可以轻松地mesh3d在 R 中绘制一个图:
library(plotly)
x <- runif(50, 0, 110)
y <- runif(50, 0, 1)
z <- runif(50, 1, 2)
plot_ly(x = ~x, y = ~y, z = ~z, type = 'mesh3d')
Run Code Online (Sandbox Code Playgroud)
我想根据变量的值对表面进行着色z:高值对应于红色或橙色,中值对应于黄色或绿色,较小值对应于浅蓝色或蓝色。我的问题:我该如何使用mesh3d?
wireframe我使用包中的函数做了类似的事情lattice。
library(lattice)
x<-runif(12,0,1)
y<-runif(12,0,2)
grid<-expand.grid(x,y)
z<-grid$Var1 + grid$Var2^2
df<-data.frame(z=z,x=grid$Var1,y=grid$Var2)
# Note: there are 144 observations and I want 6 colors, so I need 144/6 = 24 replications for each color
nrow(df)
nrow(df)/6
a<-palette(c(rep("blue",24),rep("light blue",24),rep("green",24),rep("yellow",24),rep("orange",24),rep("red",24)))
wireframe(z~x + y,data=df,drape=T,col.regions=a)
Run Code Online (Sandbox Code Playgroud)