0

I need help tackling this organization problem within Plotly. I searched through previous questions and while I did find similar problems, I could not find exactly what I needed or it wasn't explained in a way I could apply to my dataset.

My X-axis is supposed to be a time line between 0-13 days. The problem I am having is that Plotly organizes the axis in a way which makes days 11 and 13 come before 4-8. I also have what is called an "Initial day" in the X-axis, which might be throwing off the X-axis as a whole.

My code is this:

TLEXline <- plot_ly(TLEX, x = ~Time.line, y = ~Volume, type = "scatter", mode = "lines", linetype = ~Experimental.Group, color = I("black")) %>%
  layout(title = "MouseData",
         xaxis = list(title = "Time Line"),
         yaxis = list("Volume"))

Which creates this:

enter image description here

Jota
  • 17,281
  • 7
  • 63
  • 93

1 Answers1

0

Try setting categoryorder='trace' in the layout settings for the xaxis.

The documentation states

By default, plotly uses "trace", which specifies the order that is present in the data supplied.

but this seems to be wrong.

Without changing the layout

enter image description here

With changing the layout as shown below enter image description here

library(plotly)

t <- c("0", "1", "2", "3", "4", "8", "10", "11", "12", "13")
vol <- c(2000, 3000, 4000, 4000, 4000, 5000, 6000, 7000, 8000, 8500)

p <- plot_ly(x = t, y = vol, type='scatter', mode = 'type', )  %>%
    layout(xaxis = list(categoryorder='trace'))
p
Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99