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 ts object

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

# 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)

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

# Plotting the inverse characteristic roots of an ARIMA model
fit <- auto.arima(mdeaths, D=1)

For plotting functions that do not use an S3 plot() method, there is now a ggplot2 version with “gg” prefixed to the function name.


    ggAcf(mdeaths, main="")

    ggPacf(mdeaths, main="")

    ggCcf(mdeaths, fdeaths)



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.

comments powered by Disqus