Change legend order

How can I change the order of the legends: .model appears first then level?

library(fpp3)

us_change %>%
  model(model1 = ARIMA(Consumption ~ PDQ(0,0,0)),
        model2 = ARIMA(Consumption ~ PDQ(1,0,0)),
        model3 = ARIMA(Consumption ~ PDQ(2,0,0))) %>% 
  forecast(h = 36) %>% 
  autoplot(us_change) +
  theme(legend.position ='top')

Created on 2020-11-29 by the reprex package (v0.3.0)

This should work:

library(fpp3)

us_change %>%
  model(model1 = ARIMA(Consumption ~ PDQ(0,0,0)),
        model2 = ARIMA(Consumption ~ PDQ(1,0,0)),
        model3 = ARIMA(Consumption ~ PDQ(2,0,0))) %>% 
  forecast(h = 36) %>% 
  autoplot(us_change) +
  guides(colour = guide_legend(order = 1), fill = guide_legend(order = 1)) +
  theme(legend.position ='top')

1 Like

Thank you! Just what I wanted :smiley:
Could you explain how does order = 1 work?

The help documentation for that function can be found here.

order - positive integer less than 99 that specifies the order of this guide among multiple guides. This controls the order in which multiple guides are displayed, not the contents of the guide itself. If 0 (default), the order is determined by a secret algorithm.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.