library(forecast)
library(ggplot2)
# autoplot of a ts object
autoplot(mdeaths)
forecast v7 and ggplot2 graphics
Version 7 of the forecast package was released on CRAN about a month ago, but I’m only just getting around to posting about the new features.
The most visible feature was the introduction of ggplot2 graphics. I first wrote the forecast package before ggplot2 existed, and so only base graphics were available. But I figured it was time to modernize and use the nice features available from ggplot2. The following examples illustrate the main new graphical functionality.
For illustration purposes, I’m using the male and female monthly deaths from lung diseases in the UK.
For all base plot() methods, there is now an autoplot() method with the same functionality.
# autoplot of a forecast object
<- forecast(fdeaths)
fc autoplot(fc)
# autoplot of an stl object
autoplot(stl(mdeaths, s.window="periodic", robust=TRUE))
# Plotting multiple forecasts in one plot
<- cbind(Males=mdeaths, Females=fdeaths)
fmdeaths <- tslm(fmdeaths ~ trend + season)
fit <- forecast(fit, h=10)
fcast autoplot(fcast)
# Plotting the components of an ETS model
<- ets(mdeaths)
fit autoplot(fit)
# Plotting the inverse characteristic roots of an ARIMA model
<- auto.arima(mdeaths, D=1)
fit autoplot(fit)
For plotting functions that do not use an S3 plot() method, there is now a ggplot2 version with “gg” prefixed to the function name.
ggtsdisplay(mdeaths)
ggAcf(mdeaths)
ggPacf(mdeaths)
ggCcf(mdeaths, fdeaths)
ggseasonplot(mdeaths)
ggmonthplot(mdeaths)
There is also a new geom_forecast()
function which uses forecast.ts()
to obtain forecasts of the time series passed to autoplot()
.
autoplot(mdeaths) + geom_forecast(h=36)
Almost all of this new gglot2 goodness was created by Mitchell O’Hara-Wild, a Monash University student who I employ as a research assistant.