plotly/plotly.R

Bar plot shows too many ticks

emrichter opened this issue · 1 comments

In plotly 4.10.4 a bar plot with only one date observation shows actually multiple ticks when the ticks' format is converted. Here a minimal reproducible example:

library(dplyr)
library(plotly)

df <- tibble(Time = "2025-01", Animal = "Monkey", value = 2)

df %>%
  plot_ly(x = ~Time, y= ~value, color = ~Animal) %>%
  layout(
    xaxis = list(
      type = "date",
      tickformat = "%b '%y"
    )
  )

Image

This is not the case though for a stacked bar plot:

df <- tibble(Time = c("2025-01", "2025-01"), Animal = c("Monkey", "Lion"), value = c(2, 3))

df %>%
  plot_ly(x = ~Time, y= ~value, color = ~Animal) %>%
  layout(
    barmode = "stacked",
    xaxis = list(
      type = "date",
      tickformat = "%b '%y"
    )
  )

Image

When looking at the dates without formatting one can see a few minimal shifts around the actual date:

df <- tibble(Time = "2025-01", Animal = "Monkey", value = 2)

df %>%
  plot_ly(x = ~Time, y= ~value, color = ~Animal) %>%
  layout(
    xaxis = list(
      type = "date"
    )
  )

Image

Is this already somewhere addressed?

I am not aware of this being a duplicate. As a workaround, you can switch the X axis type from date to just text and it will work, but it is definitely not ideal. I think this is a strange corner case that needs to be fixed either in the plotly.js or plotly.R, not sure which one just yet.