Plotly: x-axis range slider (date) with custom start / end date

Is it possible to use a parameter rangesliderin a package plotlyto add a slider AND indicate which range is the default value. Currently, the following code adds a slider, but the entire date range is selected by default.

library(plotly)

df <- data.frame(Date = seq(as.Date("2016-01-01"), as.Date("2016-08-31"), by="days"),
                 Value = sample(100:200, size = 244, replace = T))

p <- plot_ly(data = df, x = Date, y = Value, type = "line") %>%
  layout(xaxis = list(rangeslider = list(type = "date")  ))
p

I would like to be able to specify an initial range - for example, show only the last month and allow the user to expand the range if he so desires. The documentation seems to suggest that there is no such option, and I would prefer not to use a custom javascript method.

Any ideas?

+4
source share
1 answer

, , , . - , , :

p <- plot_ly(data = df, x = Date, y = Value, type = "line") %>%
  layout(xaxis = list(range = c( as.numeric(max(df$Date)-30) *86400000,
                                 as.numeric(max(df$Date)) * 86400000   ),
  rangeslider = list(type = "date")  ))
p
+7

Source: https://habr.com/ru/post/1655645/


All Articles