✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Multiple 3D surface with dropdown

x1 = c(1:10)
y1 = c(1:10)
z1 = matrix(runif(100,0,25),ncol = 10)
z1_col = matrix(runif(100,0,25),ncol = 10)

x2 = c(2:11)
y2 = c(2:11)
z2 = matrix(runif(100,0,100),ncol = 10)
z2_col = matrix(runif(100,0,25),ncol = 10)

x3 = c(3:12)
y3 = c(3:12)
z3 = matrix(runif(100,0,10),ncol = 10)
z3_col = matrix(runif(100,0,25),ncol = 10)

x4 = c(3:12)
y4 = c(3:12)
z4 = matrix(runif(100,0,5),ncol = 10)
z4_col = matrix(runif(100,0,25),ncol = 10)

I am trying to fix the position of the colorbar in plotly. I have already try to fix it by putting in the x and y axis of the color bar for each plot.

plot_ly(type = "surface", colors = c("blue","green","yellow","orange","red")) %>%
  add_trace(x = ~ x1, y = ~ y1, z = ~ z1, surfacecolor = ~ z1_col,
            colorbar = list(title = "Concentration", x = 1, y = 0.5)) %>%
  add_trace(x = ~ x2, y = ~ y2, z = ~ z2, surfacecolor = ~ z2_col,visible = F,
            colorbar = list(title = "Concentration", x = 1, y = 0.5)) %>%
  add_trace(x = ~ x3, y = ~ y3, z = ~ z3, surfacecolor = ~ z3_col,visible = F,
            colorbar = list(title = "Concentration", x = 1, y = 0.5)) %>%
  add_trace(x = ~ x4, y = ~ y4, z = ~ z4, surfacecolor = ~ z4_col,visible = F,
            colorbar = list(title = "Concentration", x = 1, y = 0.5)) %>%
  layout(updatemenus = list(
    list(
      y = 0.8,
      # FOR line, point, to show A, list(T,F,F)
      # but for 3D, list (F,T,F)
      buttons = list(
        list(method = "restyle",
             args = list("visible",list(F,T,F,F)),
             label = "A"),
        list(method = "restyle",
             args = list("visible",list(F,F,T,F)),
             label = "B"),
        list(method = "restyle",
             args = list("visible",list(F,F,F,T)),
             label = "C"),
        list(method = "restyle",
             args = list("visible",list(T,F,F,F)),
             label = "D")
      )
    )
  ))

However, whenever I change to a new plot, the position of the color bar changes (shifting downward).



Is there any way to fix the position of the color bar for all the plot ?