forecast v7 and ggplot2 graphics

Date

9 May 2016

Topics
forecasting
graphics
R
time series

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.

library(forecast)
library(ggplot2)

# autoplot of a ts object
autoplot(mdeaths)

# autoplot of a forecast object
fc <- forecast(fdeaths)
autoplot(fc)

# autoplot of an stl object
autoplot(stl(mdeaths, s.window="periodic", robust=TRUE))

# Plotting multiple forecasts in one plot
fmdeaths <- cbind(Males=mdeaths, Females=fdeaths)
fit <- tslm(fmdeaths ~ trend + season)
fcast <- forecast(fit, h=10)
autoplot(fcast)

# Plotting the components of an ETS model
fit <- ets(mdeaths)
autoplot(fit)

# Plotting the inverse characteristic roots of an ARIMA model
fit <- auto.arima(mdeaths, D=1)
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.