Agi
Agi

Reputation: 83

Specific colors for multiple surface plot

Take the example from https://plotly.com/r/3d-surface-plots/ for multiple surfaces. Here is the code of this example.

library("plotly")
z <- c(
  c(8.83,8.89,8.81,8.87,8.9,8.87),
  c(8.89,8.94,8.85,8.94,8.96,8.92),
  c(8.84,8.9,8.82,8.92,8.93,8.91),
  c(8.79,8.85,8.79,8.9,8.94,8.92),
  c(8.79,8.88,8.81,8.9,8.95,8.92),
  c(8.8,8.82,8.78,8.91,8.94,8.92),
  c(8.75,8.78,8.77,8.91,8.95,8.92),
  c(8.8,8.8,8.77,8.91,8.95,8.94),
  c(8.74,8.81,8.76,8.93,8.98,8.99),
  c(8.89,8.99,8.92,9.1,9.13,9.11),
  c(8.97,8.97,8.91,9.09,9.11,9.11),
  c(9.04,9.08,9.05,9.25,9.28,9.27),
  c(9,9.01,9,9.2,9.23,9.2),
  c(8.99,8.99,8.98,9.18,9.2,9.19),
  c(8.93,8.97,8.97,9.18,9.2,9.18)
)
dim(z) <- c(15,6)
z2 <- z + 1
z3 <- z - 1

fig <- plot_ly(showscale = FALSE)
fig <- fig %>% add_surface(z = ~z)
fig <- fig %>% add_surface(z = ~z2, opacity = 0.98)
fig <- fig %>% add_surface(z = ~z3, opacity = 0.98)

How can I make each surface a specific color? i.e. the first surface be all red, the second blue and so on. Any help would be appreciated!

Upvotes: 1

Views: 1348

Answers (1)

LuizZ
LuizZ

Reputation: 1044

You can use the argument colorscale, as the following:


fig <- plot_ly(showscale = FALSE)
fig <- fig %>% add_surface(z = ~z, colorscale = list(c(0,1),c("rgb(107,184,255)","rgb(0,90,124)")) )
fig <- fig %>% add_surface(z = ~z2, opacity = 0.98, colorscale = list(c(0,1),c("rgb(255,107,184)","rgb(128,0,64)")))
fig <- fig %>% add_surface(z = ~z3, opacity = 0.98, colorscale = list(c(0,1),c("rgb(107,255,184)","rgb(0,124,90)")))
fig

For each surface, you specify two colors: a darker and a lighter one. The contrast between them correspond to different heights of each surface. The heights of surfaces are scaled between 0 and 1. The pair of colors specified are assigned to respective bottom and top heights of each surface.

You can play with shades of RGB colors to get exactly the colors you want. Check this or this sites for more information on RGB color scales.

enter image description here

My code was adapted from this page, which worked with a very similar example from yours, but only two surfaces.

Based on the same logic, you can expand the number of layers, as requested in the comments:

z4 <- z + 2 
z5 <- z + 3 
z6 <- z + 4

fig <- plot_ly(showscale = FALSE)
fig <- fig %>% add_surface(z = ~z, colorscale = list(c(0,1),c("rgb(107,184,255)","rgb(90,90,124)")) )
fig <- fig %>% add_surface(z = ~z2, opacity = 0.98, colorscale = list(c(0,1),c("rgb(255,107,184)","rgb(128,0,64)")))
fig <- fig %>% add_surface(z = ~z3, opacity = 0.98, colorscale = list(c(0,1),c("rgb(107,255,184)","rgb(0,124,90)")))
fig <- fig %>% add_surface(z = ~z4, opacity = 0.98, colorscale = list(c(0,1),c("rgb(182,142,242)","rgb(104,3,255)")))
fig <- fig %>% add_surface(z = ~z5, opacity = 0.98, colorscale = list(c(0,1),c("rgb(247,226,157)","rgb(255,192,7)")))
fig <- fig %>% add_surface(z = ~z6, opacity = 0.98, colorscale = list(c(0,1),c("rgb(129,212,247)","rgb(12,177,247)")))
fig

The above code produced:

enter image description here

Upvotes: 2

Related Questions