Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Cannot re-arrange x-axis when axis type is category

I have the following data:

myData <- data.frame(FISCAL_YEAR_WEEK=c(‘2016-09’,‘2016-09’,‘2016-09’,‘2016-09’,‘2016-09’,‘2016-10’,‘2016-10’,‘2016-10’,‘2016-10’,‘2016-10’,‘2016-10’,‘2016-10’,‘2016-10’,‘2016-11’,‘2016-11’,‘2016-11’,‘2016-11’,‘2016-12’,‘2016-12’,‘2016-12’,‘2016-12’,‘2016-12’,‘2016-12’,‘2016-14’,‘2016-14’,‘2016-14’,‘2016-14’),MOTOR_VEND_ID = c(‘7’,‘E’,‘F’,‘F’,‘M’,‘7’,‘9’,‘E’,‘E’,‘F’,‘F’,‘M’,‘R’,‘7’,‘E’,‘F’,‘F’,‘E’,‘E’,‘F’,‘F’,‘M’,‘M’,‘7’,‘E’,‘F’,‘M’),HGA_SUPPLIER=c(‘RHO’,‘RHO’,‘HWY’,‘RHO’,‘RHO’,‘RHO’,‘RHO’,‘HWY’,‘RHO’,‘HWY’,‘RHO’,‘RHO’,‘RHO’,‘RHO’,‘RHO’,‘HWY’,‘RHO’,‘HWY’,‘RHO’,‘HWY’,‘RHO’,‘HWY’,‘RHO’,‘RHO’,‘RHO’,‘RHO’,‘RHO’),RTPAD_TOT=c(0,0,0,0,0,0,420,6,0,0,0,20,1,76,0,0,0,76,62,0,0,0,0,6,1,1,0))

I would like to have a line chart with RTPAD_TOT against FISCAL_YEAR_WEEK, grouped by the other varables. FISCAL_YEAR_WEEK represents the fiscal week in a fiscal year. 2016-11 means the 11th week of fiscal year 2016. Here’s my plotly code:

ax <- list(
type = “category”,
showgrid = TRUE,
showline = TRUE,
autorange = TRUE,
showticklabels = TRUE,
ticks = “outside”,
tickangle = 0
)

ay <- list(
range = c(0,300),
dtick = 50,
showgrid = TRUE,
showline = TRUE,
autorange = FALSE,
showticklabels = TRUE,
ticks = “outside”,
tickangle = 0
)

plot_ly(myData,
x = FISCAL_YEAR_WEEK,
y = RTPAD_TOT,
type = ‘scatter’,
mode = ‘markers+lines’,
color = interaction(MOTOR_VEND_ID,HGA_SUPPLIER)) %>%
layout(xaxis = ax, yaxis = ay)

However, this results in my x-axis not arranged in order:

Obviously, I would like 2016-09 to come before 2016-10 in the chart. But my FISCAL_YEAR_WEEK in the x-axis is all jumbled up.

Using GGPLOT to construct the same plot does not jumble up my x-axis:

ggplot(data=myData,
aes(x=FISCAL_YEAR_WEEK,
y=RTPAD_TOT,
group=interaction(MOTOR_VEND_ID,HGA_SUPPLIER)))) +
geom_line()

The appearance of my axis from the above code will be all good until…

ggplotly()

I wanted to convert my ggplot to plotly. After the ggplotly() command, my x-axis will be jumbled up again.

I need the type = “category” option in my x-axis as I only want to see the dates that are present in the data. Also, plotly will misinterpret 2016-09 to be september 2016 instead of the 9th fiscal week of 2016. I’m building a shiny application where my x-axis can be changed by the user to be not only a datetime variable, but other variable as well. Sometimes, it may be serial numbers like 001, 002, 120. So it doesn’t make sense to make it numeric as this will make a huge gap between 002 and 120. So the type = “category” option is needed here.

Would greatly appreciate if someone could help.

This can be done with the “categorymode” and “categorylist” parameters but I don’t think these features have been added to the R plotly package just yet.

See:

1 Like

The new category modes can be accessed in R. Using your code, we can add the following to ax,

categoryorder="category ascending",
categoryarray=unique(myData$FISCAL_YEAR_WEEK)

The full code looks like this.

myData <- data.frame(
  FISCAL_YEAR_WEEK=c('2016-09','2016-09','2016-09','2016-09','2016-09','2016-10','2016-10','2016-10','2016-10','2016-10','2016-10','2016-10','2016-10','2016-11','2016-11','2016-11','2016-11','2016-12','2016-12','2016-12','2016-12','2016-12','2016-12','2016-14','2016-14','2016-14','2016-14'),
  MOTOR_VEND_ID = c('7','E','F','F','M','7','9','E','E','F','F','M','R','7','E','F','F','E','E','F','F','M','M','7','E','F','M'),
  HGA_SUPPLIER=c('RHO','RHO','HWY','RHO','RHO','RHO','RHO','HWY','RHO','HWY','RHO','RHO','RHO','RHO','RHO','HWY','RHO','HWY','RHO','HWY','RHO','HWY','RHO','RHO','RHO','RHO','RHO'),
  RTPAD_TOT=c(0,0,0,0,0,0,420,6,0,0,0,20,1,76,0,0,0,76,62,0,0,0,0,6,1,1,0)
)


ax <- list(
  type = "category",
  showgrid = TRUE,
  showline = TRUE,
  autorange = TRUE,
  showticklabels = TRUE,
  ticks = "outside",
  tickangle = 0,
  categoryorder="category ascending",
  categoryarray=unique(myData$FISCAL_YEAR_WEEK)
)

ay <- list(
  range = c(0,300),
  dtick = 50,
  showgrid = TRUE,
  showline = TRUE,
  autorange = FALSE,
  showticklabels = TRUE,
  ticks = "outside",
  tickangle = 0
)

plot_ly(
  myData,
  x = FISCAL_YEAR_WEEK,
  y = RTPAD_TOT,
  type = 'scatter',
  mode = 'markers+lines',
  color = interaction(MOTOR_VEND_ID,HGA_SUPPLIER)
) %>%
layout(xaxis = ax, yaxis = ay)


ggplot(
  data=myData, 
  aes(
    x=FISCAL_YEAR_WEEK, 
    y=RTPAD_TOT, 
    group=interaction(MOTOR_VEND_ID,HGA_SUPPLIER)
  )
) + geom_line()

ggplotly() %>% layout(xaxis = ax)

2 Likes