2

Here is a minimum working example:

library(tidyverse)
library(dplyr)
library(plotly)

x_val=c(as.Date("2015-01-01"), as.Date("2016-01-01"), as.Date("2017-01-01"), as.Date("2018-01-01"))

y_val= c(1,2,3,4)

fig <- plot_ly(
  x = x_val, y = y_val, type = "scatter", mode = "lines") %>% layout(xaxis = list(
    rangeslider = list(type = "date", tick0 = "2015-01-01", dtick="M6", showticklabels=TRUE, showticks=TRUE )
  ))

fig

If you run it, you'll see that the slider below the figure does not have any ticks; According to the documentation

https://plotly.com/r/reference/#layout-xaxis-rangeslider

ticks should be possible, because there are many tick options like dtick, tick0 etc specifically for rangeslider - nevertheless, it is not working; I saw this question:

r - show ticks for rangeslider (plotly)

which basically asks the same questions, which, however, has no replies or solutions

Johannes
  • 101
  • 10

1 Answers1

1

Altough I am not able to get it running with plotly, I would have a alternative: use highcharter. Here is a working example with your provided data:

library(highcharter)
library(xts)
dat <- data.frame(x_val = c(as.Date("2015-01-01"), as.Date("2016-01-01"), as.Date("2017-01-01"), as.Date("2018-01-01")),
                  y_val = c(1, 2, 3, 4))
dat <- as.xts(dat[,2], dat[,1])
highchart(type = "stock") %>%
  hc_add_series(dat)

You need to format your data to a xts-object (timeseries), so you can set the type to stock and get a slider. If you're flexible, this approach might work.

Nico
  • 463
  • 5
  • 11