r - plotly - different colours for different surfaces -


using plotly have each surface have different colour.

library(plotly) t1 <- seq(-3, 3, 0.1); t2 <- seq(-3, 3, 0.1)  p1 <- matrix(nrow = length(t1), ncol = length(t2)) p2 <- matrix(nrow = length(t1), ncol = length(t2))  p8a1 <- 1.2 p8a2 <- 1 p8d <- -1 p8b1 <- 0.7 p8b2 <- 0.6  (i in 1:length(t2)) {    (j in 1:length(t1)) {       p1[i, j] <- 1 / (1 + exp(-1.7 * (p8a1 * t1[j] + p8a2 * t2[i] + p8d)))       p2[i, j] <- (1 / (1 + exp(-1.7 * p8a1 * (t1[j]- p8b1)))) *                    (1 / (1 + exp(-1.7 * p8a2 * (t2[j]- p8b2))))    } }  df1 <- list(t1, t2, p1) df2 <- list(t1, t2, p2)  names(df1) <- c("t1", "t2", "p1") names(df2) <- c("t1", "t2", "p2") m <- list(l = 10, r = 10, b = 5, t = 0, pad = 3)  p <- plot_ly(color = c("red", "blue")) %>%      add_surface(x = df1$t1,                  y = df1$t2,                  z = df1$p1,                  opacity = 0.8) %>%      add_surface(x = df2$t1,                  y = df2$t2,                  z = df2$p2,                  opacity = 1) %>%      layout(autosize = f, width = 550, height = 550, margin = m,             scene = list(xaxis = list(title = "theta 1"),                          yaxis = list(title = "theta 2"),                          zaxis = list(title = "p")),             dragmode = "turntable") p 

unfortunately, i'm not able change colours of these 2 surfaces. tried add color = i("red") , color = i("blue") arguments add_surface changed colour scale red blue both surfaces.

i tried add color = "red" plot_ly() , add inherit = f second add_surface. changed first surface only, yellow default color red. love have 1 surface red , second 1 blue.

sounds trivial it's bit tricky in plotly. color of surface plot either derived z values or array same dimensions z. color array accepts numerical values, no color strings or rgb values.

so let's define array our colors

color <- rep(0, length(df1$p1)) dim(color) <- dim(df1$p1) 

next need trick plotly ignoring colorscale.

surfacecolor=color,              cauto=f,              cmax=1,              cmin=0 

et voilĂ , have uniformely colored plot.

enter image description here


library(plotly) t1 <- seq(-3, 3, 0.1); t2 <- seq(-3, 3, 0.1)  p1 <- matrix(nrow = length(t1), ncol = length(t2)) p2 <- matrix(nrow = length(t1), ncol = length(t2))  p8a1 <- 1.2 p8a2 <- 1 p8d <- -1 p8b1 <- 0.7 p8b2 <- 0.6  (i in 1:length(t2)) {   (j in 1:length(t1)) {     p1[i, j] <- 1 / (1 + exp(-1.7 * (p8a1 * t1[j] + p8a2 * t2[i] + p8d)))     p2[i, j] <- (1 / (1 + exp(-1.7 * p8a1 * (t1[j]- p8b1)))) *        (1 / (1 + exp(-1.7 * p8a2 * (t2[j]- p8b2))))   } }  df1 <- list(t1, t2, p1) df2 <- list(t1, t2, p2)  names(df1) <- c("t1", "t2", "p1") names(df2) <- c("t1", "t2", "p2") m <- list(l = 10, r = 10, b = 5, t = 0, pad = 3)  color <- rep(0, length(df1$p1)) dim(color) <- dim(df1$p1) p <- plot_ly(colors = c('red', 'blue')) %>%   add_surface(x = df1$t1,               y = df1$t2,               z = df1$p1,               opacity = 0.8,               #surfacecolor=c('red')               surfacecolor=color,               cauto=f,               cmax=1,               cmin=0   ) color2 <- rep(1, length(df2$p2)) dim(color2) <- dim(df2$p2 )  p <-  add_surface(p,               x = df2$t1,               y = df2$t2,               z = df2$p2,               opacity = 1,               surfacecolor=color2,               cauto=f,               cmax=1,               cmin=0) p 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -