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

时间:2017-10-25 18:45:10

标签: r plotly colorbar

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)

我试图在情节上修正颜色条的位置。我已经尝试通过在每个图中放入颜色条的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 有没有办法确定所有情节的颜色条的位置?

1 个答案:

答案 0 :(得分: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) %>% ...
相关问题