首页 > 解决方案 > 我可以使用 Plotly 的自定义更新菜单按钮来更新 x 轴范围,而 shareX = FALSE 在子图中?

问题描述

我正在使用 plotly 并将两个数字与 subplot 结合在一起,我提到shareX = FALSEx 轴的范围会有所不同。和“plot_ly ”data.frame代码如下:

library(dplyr)
library(plotly)
set.seed(600)
df1 <- data.frame(
  class = c(rep("1st", times = 60), rep("2nd", time = 30)),
  week = c(rep(1:20, times = 3), rep(1:10, times = 3)),
  cat = c(rep("A", times = 20), rep("B", times = 20), rep("C", times = 20), rep("A", times = 10), rep("B", times = 10), rep("C", times = 10)),
  count = round(runif(90, 1, 100))
)

df1 %>%
  split(df1$class) %>%
  purrr::map(~{
    plotly::plot_ly(.x, x = .x$week, y = .x$count, type = "bar", split = .x$cat) %>%
      layout(
        barmode = "group",
        showlegend = FALSE,
        updatemenus = list(list(
          active = -1,
          type = 'buttons',
          buttons = list(
            list(label = "1st 2W",
                 method = "relayout",
                 args = list("xaxis.range", c(0.5, min(.x$week)+1.5)))
          )
        ))
      )
  }) %>%
  plotly::subplot(nrows = 2,
                  shareY = FALSE,
                  shareX = FALSE,
                  margin = 0.05
                  )

在这种情况下,按钮只会更新第一个图,就像我提到的shareX = TRUE那样,它会更新两个图。是否可以使用 updatemenus while shareX = FALSE

标签: rbuttonrangeplotly

解决方案


推荐阅读