rggplot2time-seriestrendlineggfortify

Time Series Plot using ggplot2


How can I make changes to the following things in this graph?

  1. Change the colour of trend line (currently blue)
  2. Change the colour of the peak points (currently orange)
  3. Change the colour of the Time series Pattern Line(currently black)
  4. Add a legend to indicate the colour of all the three above

Here's the Plot I got from the code source

enter image description here

Code Source:

# Libraries used: 
library(ggplot2)
library(timeSeries)
library(ggfortify)
library(ggthemes)
library(dplyr)
library(strucchange)

strucchange::breakpoints(AirPassengers ~ 1) %>%
  autoplot(ts.linetype = 'solid', ts.size = 1.1, ts.geom = 'line')+
  geom_smooth(aes(y=AirPassengers), 
              method = "loess", se= F,
              lwd = 1.2) +
  geom_point(aes(y = AirPassengers), size = 1.5, shape = 16, color = "orange3")+
  scale_x_date(date_breaks = "1 years", date_labels = "%Y")+
  labs(title = "Yearly Time Series", 
       subtitle="Sales trend from Year 2001 to 2017", 
       caption="Source: Airpassengers Time Series Analysis", 
       y="Sales", x = "Years")+
  theme_economist_white()+
  theme(axis.text.x = element_text(angle = 90, hjust = -.1))

Solution

  • The first three changes can be made using the following syntax:

    strucchange::breakpoints(AirPassengers ~ 1) %>%
      autoplot(ts.linetype = 'solid', ts.size = 1.1, ts.geom = 'line', fill = "red")+
      geom_smooth(aes(y=AirPassengers), 
              method = "loess", se= F,
              lwd = 1.2, color = "green") +
      geom_point(aes(y = AirPassengers), size = 1.5, shape = 16, color = "blue")+
      scale_x_date(date_breaks = "1 years", date_labels = "%Y")+
      labs(title = "Yearly Time Series", 
       subtitle="Sales trend from Year 2001 to 2017", 
       caption="Source: Airpassengers Time Series Analysis", 
       y="Sales", x = "Years")+
     theme_economist_white()+
     theme(axis.text.x = element_text(angle = 90, hjust = -.1)) 
    

    enter image description here

    The color of the trend line is changed by specifying color = "green" in the call to geom_smooth. The same is done for the peak points in geom_point. Lastly, autoplot in this case doesn't seem to know color, but it works with specifying fill = "red" (this throws a warning, but works nonetheless).

    I am not clear how you want your legend to look, can you clarify in the comments?