修复多个情节的位置颜色条

问题描述 投票:2回答:1
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)

我试图在剧情中修复colorbar的位置。我已经尝试通过在每个绘图中放置颜色条的x和y轴来修复它。

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

但是,每当我更改为新绘图时,颜色条的位置会发生变化(向下移动)。 enter image description here enter image description here有没有办法确定所有情节的颜色条的位置?

r plotly colorbar
1个回答
1
投票

尝试将colorbar参数移到add_trace()之外,而不是

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)) ...

做:

plot_ly(type = "surface", colors = c("blue","green","yellow","orange","red")) %>%
  add_trace(x = ~ x1, y = ~ y1, z = ~ z1, surfacecolor = ~ z1_col) %>% 
  colorbar(title = "Concentration", x = 1, y = 0.5) %>% ...
© www.soinside.com 2019 - 2024. All rights reserved.