如何在 Plot_ly 中为 mesh3d 图添加自定义颜色索引?

gm1*_*991 5 r lattice r-plotly

我可以轻松地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)

ism*_*gal 7

欢迎来到SO!

您可以像这样添加自定义 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")))
Run Code Online (Sandbox Code Playgroud)