This document explains plotting time series statistics using ggplot2 and ggfortify.

Installation

First, install ggfortify from CRAN.

install.packages('ggfortify')

Plotting time series statistics

ggfortify supports following time series related statistics in stats package:

library(ggfortify)
autoplot(stl(AirPassengers, s.window = 'periodic'), ts.colour = 'blue')

NOTE With acf and spec.*, specify plot = FALSE to suppress default plotting outputs.

autoplot(acf(AirPassengers, plot = FALSE))

You can pass some options when plotting acf via autoplot. Built-in acf calcurates the confidence interval at plotting time and doesn’t hold the result, equivalent options can be passed to autoplot. Following example shows to change the value of confidence interval and method (use ma assuming the input follows MA model).

autoplot(acf(AirPassengers, plot = FALSE), conf.int.fill = '#0000FF', conf.int.value = 0.8, conf.int.type = 'ma')

autoplot(spec.ar(AirPassengers, plot = FALSE))

ggcpgram should output the cumulative periodogram as the same as cpgram. Because cpgram doesn’t have return value, we cannot use autoplot(cpgram(...)).

ggcpgram(arima.sim(list(ar = c(0.7, -0.5)), n = 50))

ggtsdiag should output the similar diagram as tsdiag.

library(forecast)
ggtsdiag(auto.arima(AirPassengers))

gglagplot is for lag.plot.

gglagplot(AirPassengers, lags = 4)

ggfreqplot is a genelarized month.plot. You can pass freq if you want, otherwise time-series’s frequency will be used.

ggfreqplot(AirPassengers)

ggfreqplot(AirPassengers, freq = 4)