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

时间:2019-01-30 19:52:45

标签: r lattice r-plotly

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

1 个答案:

答案 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")))