How to create rangeselectors buttons for different periods on the plot - plotly

Viewed 1100

I'm looking for a way to automatically create buttons for different time periods in my dataset. As example from here shows it can be done using the following code:

library(plotly)
library(quantmod)

# Download some data
getSymbols(Symbols = c("AAPL", "MSFT"), from = '2018-01-01', to = '2020-01-01')
ds <- data.frame(Date = index(AAPL), AAPL[,6], MSFT[,6])

fig <- plot_ly(ds, x = ~Date)
fig <- fig %>% add_lines(y = ~AAPL.Adjusted, name = "Apple")
fig <- fig %>% add_lines(y = ~MSFT.Adjusted, name = "Microsoft")
fig <- fig %>% layout(
  title = "Stock Prices",
  xaxis = list(
    rangeselector = list(
      buttons = list(
        list(
          count = 3,
          label = "3 mo",
          step = "month",
          stepmode = "backward"),
        list(
          count = 6,
          label = "6 mo",
          step = "month",
          stepmode = "backward"),
        list(
          count = 1,
          label = "1 yr",
          step = "year",
          stepmode = "backward"),
        list(
          count = 1,
          label = "YTD",
          step = "year",
          stepmode = "todate"),
        list(step = "all"))),
    
    rangeslider = list(type = "date")),
  
  yaxis = list(title = "Price"))

fig

However, now I'd like to create a buttons to select the first year (2018) and second year (2019) separately. Both stepmode options seems to define ranges backwards from the last date only, so I'm not really sure how can I specify the range for 2018 year. Is it possible? If so, how can it be done?

1 Answers

You could use an updatemenu to select range:

library(plotly)
fig <- plot_ly(ds, x = ~Date)
fig <- fig %>% add_lines(y = ~AAPL.Adjusted, name = "Apple")
fig <- fig %>% add_lines(y = ~MSFT.Adjusted, name = "Microsoft")


  rangeselector = list(
    buttons = list(
      list(
        count = 3,
        label = "3 mo",
        step = "month",
        stepmode = "backward"),
      list(
        count = 6,
        label = "6 mo",
        step = "month",
        stepmode = "backward"),
      list(
        count = 1,
        label = "1 yr",
        step = "year",
        stepmode = "backward"),
      list(
        count = 1,
        label = "YTD",
        step = "year",
        stepmode = "todate")))
xaxis = list(rangeselector = rangeselector,
             rangeslider = list(type = "date"))


fig <- fig %>% layout(
  title = "Stock Prices",
  updatemenus = list(
    list(
      active = -1,
      type = 'buttons',
      buttons = list(
        list(
          label = '2018',
          method = "relayout",
          args = list(list(xaxis = list(range = as.POSIXct(c("2018-01-01","2018-12-31"), origin= "1970-1-1"),
                                        rangeselector = rangeselector,
                                        rangeslider = list(type = "date"))))), 
        list(
          label = '2019',
          method = "relayout",
          args = list(list(xaxis = list(range = as.POSIXct(c("2019-01-01","2019-12-31"), origin= "1970-1-1"),
                                        rangeselector = rangeselector,
                                        rangeslider = list(type = "date"))))),
        list(
          label = 'All',
          method = "relayout",
          args = list(list(xaxis = list(range = as.POSIXct(c("2018-01-01","2019-12-31"), origin= "1970-1-1"),
                                        rangeselector = rangeselector,
                                        rangeslider = list(type = "date")))))
      )
    )
  ),
  xaxis = xaxis,
  
  yaxis = list(title = "Price"))


fig

enter image description here

Related