0

Let's say I have the following dataset and generate the subsequent plot:

df <- data.frame(date=seq.Date(as.Date('2023-01-01'), as.Date('2023-05-01'), by='month'), 
                 baseline=rnorm(5), bad_economy=rnorm(5), good_economy=rnorm(5))
p <- ggplot(df, aes(x=date)) + 
  geom_line(aes(y=baseline, color='Baseline')) + 
  geom_line(aes(y=bad_economy, color='Bad Economy')) + 
  geom_line(aes(y=good_economy, color='Good Economy'))

This results in the following plot: enter image description here In the legend of the plot, I would like 'Baseline' to be first. How can I do this? The examples I've seen all require modifying the data to use a factor variable to identify colour. Is there a way I can change the order without having to do that?

Gaurav Bansal
  • 5,221
  • 14
  • 45
  • 91
  • 2
    Try `+ scale_color_discrete(breaks = c("Baseline", 'Bad Economy', 'Good Economy'))`. See https://stackoverflow.com/questions/69541579/ggplot2-reorder-items-in-a-legend or https://stackoverflow.com/questions/38619402/how-to-reorder-the-items-in-a-legend – stefan Apr 05 '23 at 19:47

0 Answers0