Reputation: 909
Using the volcano
data, I would like to color the surface of the volcano by another variable, which is a matrix, MatrixForColor
, of the same size as volcano
and it only contains 1s and 2s (is binary). For MatrixForColor = 1
and MatrixForColor = 2
I want to color blue and red, respectively.
Inspired from Formatting of persp3d plot, I managed to achieve this using persp3d
from rgl
package as following:
library(rgl)
color = c("blue", "red")
type = MatrixForColor
persp3d(volcano, theta=50, phi=25, expand=0.75, col=color[type],
ticktype="detailed", xlab="", ylab="", zlab="", axes=TRUE)
and obtained this figure:
I also tried to achieve this with plotly
(adapting after the response from plotly - different colours for different surfaces) as following:
library(plotly)
plot_ly(colors = c('blue', 'red')) %>%
add_surface(z = volcano,
opacity = 0.8,
surfacecolor=MatrixForColor,
cauto=F,
cmax=1,
cmin=0
)
but I get this figure:
which is not what I want, since it's not colored as red and blue after MatrixForColor
.
Any has any idea how to do this with plotly
?
Upvotes: 1
Views: 1024
Reputation: 33397
You will need to set correct values for cmin
and cmax
:
library(plotly)
MatrixForColor <- matrix(1, nrow = nrow(volcano), ncol = ncol(volcano))
MatrixForColor[, 1:30] <- 2
plot_ly(colors = c('blue', 'red')) %>%
add_surface(z = volcano,
opacity = 0.8,
surfacecolor = MatrixForColor,
cauto=F,
cmax=max(MatrixForColor),
cmin=min(MatrixForColor)
)
Upvotes: 2