R用范围选择器按钮选择范围

时间:2018-07-25 14:09:58

标签: r plotly axis

我想使用plotselect的rangeselector按钮选择一个特定范围。 目前,该按钮只会选择当前结束日期之前的范围,每当我通过绘图交互选择其他范围时,该范围就会更改。 我可以使按钮始终选择范围吗:as.POSIXct(c(30,80), origin= "1970-1-1")

library(plotly)

  plot_ly(x = as.POSIXct((1:100), origin= "1970-1-1")) %>%

  add_lines(y = (1:100)^2*sin((1:100))) %>%

  add_lines(y = c(0,10001), x = as.POSIXct(c(49,51), origin="1970-1-1"))%>%

  layout(
    xaxis = list(
      rangeselector = list(
        buttons = list(
          list(
            count = 50,
            label = "50 s",
            step = "second",
            stepmode = "backward")
          ))))

1 个答案:

答案 0 :(得分:1)

使用rangeselector可能难以实现该功能。但是,relayout可以在这里成为您的朋友-

library(plotly)
x <- as.POSIXct((1:100), origin= "1970-1-1")

updatemenus <- list(
  list(
    active = -1,
    type = 'buttons',
    buttons = list(
      list(
        label = '50s',
        method = "relayout",
        args = list(list(xaxis = list(range = as.POSIXct(c(30,80), origin= "1970-1-1"))))), 
      list(
        label = 'all',
        method = "relayout",
        args = list(list(xaxis = list(range = c(min(x), max(x))))))
    )
  )
)

plot_ly(x = x) %>%

  add_lines(y = (1:100)^2*sin((1:100))) %>%

  add_lines(y = c(0,10001), x = as.POSIXct(c(49,51), origin="1970-1-1"))%>%

  layout(updatemenus = updatemenus)