Search code examples
rplotlycrosstalk

Change time data labels to Danish formats and increase step to 1 month


I am making a time series visualization using plotly using monthly data. I want the user to be able to choose which months are shown and for this I use filter_slider from crosstalk. My data looks like this:

library(plotly)
library(crosstalk)
library(dplyr)

df <- data.frame(
  value = runif(18, min = -1, max = 1),
  time  = rep(c('2021-05-01', '2021-06-01', '2021-07-01', '2021-08-01',
                '2021-09-01', '2021-10-01'), 3) %>% as.Date(),
  group = rep(c(1, 2, 3), each = 3) %>% factor()
)

sd <- highlight_key(df)

widgets <- bscols(
  filter_slider('ti', 'Time', sd, ~time, timeFormat = '%y-%b')
)

plot <- plot_ly(
  sd, x = ~time, y = ~value, mode = 'lines+markers', 
  type = 'scatter', color = ~group)

bscols(widgets, plot)

I have two issues

  1. The user is Danish, so I would like to have Danish labels for the months. This is a small issue, since the only months where the 3 letter labels differ are 'May' which is 'Maj' in Danish, and 'Oct' (October) which should be 'Okt' (Oktober). How do I change the 3-letter labels to their Danish version?
  2. Since I only have monthly data, I would like the step size to be 1 month. Currently when I drag the slider, its very 'continous', i.e., lets me select individual days, but the data visualization only changes whenever a new month is encountered on the slider.

Solution

  • For your second question, adding the argument "step = 31" to filter_slider appears to work.