Home > OS >  Using ggplot with the AirPassengers Dataset
Using ggplot with the AirPassengers Dataset

Time:09-17

I am trying to graph the trend of AirPassengers and the "raw data" with ggplot but I am not able to get the data to play nicely with ggplot. I'm not looking for someone to do the problem for me but I am in need of advice as I have hit a wall.

code :

# how I am given the data set to work with 
AirPassengers <- as.data.frame(AirPassengers)

# trying to get the data with a yearly trend
AP <- ts(AirPassengers, start = c(1949,1), frequency = 12) # doesn't work with freq = 1
dAP <- decompose(AP, type = "multiplicative")
dAP_trend <- dAP$trend

AirPassengers AP, and dAP (From the code above) don't work with ggplot. However, I was able to get the plots of the trend and the "raw data" painlessly with plot and using :

plot(AirPassengers)
par(new=TRUE)
plot(dAP_trend, type="l", col="green" )

CodePudding user response:

By zoo::as.zoo and time()

autoplot(as.zoo(AP), geom = "line")  
  geom_line(aes(x = time(dAP_trend), y = dAP_trend), color = "green")

ts

  • Related