How to Order Stacked Groups for Scatter/Line Plots in R

I’m wanting to be able to order categorical groups the way I’d like to in a stacked line plot, but am not having any success finding examples online. The only indication of being able to order categories is in layout’s categoryorder, which I believe applies to some other plots but not scatter/line plots.

I tried using it anyway with a line plot but without success:

library(plotly)
packageVersion('plotly')  #4.9.0

plot_ly(filter(mpg, manufacturer=='audi'), 
    x=~year, 
    y=~hwy, 
    color=~model,
    type='scatter', 
    mode='line', 
    fill='tonexty',
    stackgroup='one') %>% 
layout(title='Audi Highway MPG Over Time',
     yaxis=list(categoryorder='array', categoryarray=c('a4 quattro', 'a4', 'a6 quattro'))
     )

As you can see the order I specified is not being plotted

Any suggestions?

1 Like