✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Is it possible to change animation slider (frame) tick labels from numeric to string?

I am wondering if there is anyway to alter the slider (frame) tick labels used on animations through code in the layout or animation options? All of the Plotly examples have numeric tick labels, but what if I wanted the tick labels to read text, such as months?

The way the ~frame API and the accumulate_by function seem to work best is with integers and not strings.

See image below for tick reference:


Have you figured anything out? I’m trying to do the exact same thing with months. I’d prefer it to display the month spelled out instead of just the number. When I use an ordered factor of the month names, it ends up adding a bunch of traces to the chart and there is no animation. Here’s some code I’m working with:

snow_plot <- snow_depth %>% plot_ly(
    x = ~year, 
    y = ~avg_depth, 
    color = ~avg_temp, 
    frame = ~month_char,
    text = ~paste('<i>Month</i>: ', month_char,
                  '<br><b>Avg. Depth</b>: ', avg_depth, 
                  '<br><b>Avg. Temp</b>: ', avg_temp),
    hoverinfo = 'text',
    type = 'bar'
  ) %>%
  layout(
    title = "Average Snow Depth by Week", 
    yaxis = list(title = "Average Depth (mm)")
  ) %>%
  animation_opts(
    frame = 1000
  )

In this code, month_char is the ordered factor of months using three character abbreviations (e.g., Mar, Apr, May, etc.).

A helpful user on StackOverflow was able to answer my version of the question. It still isn’t a perfect solution and is kind of a workaround. Below is the code ismirsehregal provided:

library(plotly)

DF <- data.frame(
  year = rep(seq(1980L, 2020L), each = 12), 
  month = rep(1:12, 41), 
  month_char = rep(factor(month.abb), 41),
  avg_depth = runif(492)
)

fig <- DF %>%
  plot_ly(
    x = ~year, 
    y = ~avg_depth,
    frame = ~paste0(sprintf("%02d", month), " - ", month_char),
    type = 'bar'
  ) %>%
  animation_slider(
    currentvalue = list(prefix = "Month: ")
  )

fig

And here’s a link directly to my question on SO.