我可以轻松地在R中绘制mesh3d
图:
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')
我想基于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)
答案 0 :(得分:1)
欢迎您!
您可以像这样添加自定义colorRamp:
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, intensity = ~z, type = 'mesh3d', colors = colorRamp(c("blue", "lightblue", "chartreuse3", "yellow", "red")))