Moving to Tidy Forecasting in R: How to Visualize Time Series Data
By: Joon Im
Tags: R, Time Series Analysis, Forecasting, Ggplot2, Fable

Plot time series data using the fpp2 , fpp3 , and modeltime forecasting frameworks.

Examples of different types of time series

1. Set Up

1.1. Introduction

There are a number of forecasting packages written in R to choose from, each with their own pros and cons.

For almost a decade, the forecast package has been a rock-solid framework for time series forecasting. However, within the last year or so an official updated version has been released named fable which now follows tidy methods as opposed to base R.

More recently, modeltime has been released and this also follows tidy methods. However, it is strictly used for modeling. For data manipulation and visualization, the timetk package will be used which is written by the same author as modeltime .

The following is a code comparison of various time series visualizations between these frameworks: fpp2 , fpp3 and timetk .

A few things to keep in mind:

Only the essential code has been provided

Non-essential code such as plot titles and themes has been excluded

All plots utilize the Business Science ggplot theme

1.2 Load Libraries

# Load libraries

library(fpp2) # An older forecasting framework

library(fpp3) # A newer tidy forecasting framework

library(timetk) # An even newer tidy forecasting framework

library(tidyverse) # Collection of data manipulation tools

library(tidyquant) # Business Science ggplot theme

library(cowplot) # A ggplot add-on for arranging plots

2. TS vs tsibble

The base ts object is used by forecast & fpp2

object is used by & The special tsibble object is used by fable & fpp3

object is used by & The standard tibble object is used by timetk & modeltime

2.1 Load Time Series Data

For the next few visualizations, we will utilize a dataset containing quarterly production values of certain commodities in Australia.

# Quarterly Australian production data as tibble

aus <- tsibbledata::aus_production %>% as_tibble() # Check structure

aus %>% str()

Always check the class of your time series data.

2.2 fpp2 Method: From tibble to ts

# Convert tibble to time series object

aus_prod_ts <- ts(aus[, 2:7], # Choose columns

start = c(1956, 1), # Choose start date

end = c(2010, 2), # Choose end date

frequency = 4) # Choose frequency per yr # Check it out

aus_prod_ts %>% tail()

Quarterly production of select Australian commodities.

2.3 fpp3 Method: From ts to tsibble

2.3.1 Pivot Wide

# Convert ts to tsibble and keep wide format

aus_prod_tbl_wide <- aus_prod_ts %>% # TS object

as_tsibble(index = "index", # Set index column

pivot_longer = FALSE) # Wide format

Open wide.

2.3.2 Pivot Long

# Convert ts to tsibble and pivot to long format

aus_prod_tbl_long <- aus_prod_ts %>% # TS object

as_tsibble(index = "index", # Set index column

pivot_longer = TRUE) # Long format

Note

2.4 timetk Method: From tsibble/ts to tibble

2.4.1 Pivot Wide

# Convert tsibble to tibble, keep wide format

aus <- tsibbledata::aus_production %>%

tk_tbl() %>%

mutate(Quarter = as_date(as.POSIXct.Date(Quarter)))

Workaround for indexing issue with tsibble and R 4.0 and up.

2.4.2 Pivot Long

# Quarterly Australian production data to long format

aus_long <- aus %>%

rename(date = Quarter) %>%

pivot_longer(

cols = c("Beer","Tobacco","Bricks",

"Cement","Electricity","Gas"))

3. Time Series Plots

When analyzing time series plots, look for the following patterns:

Trend: A long-term increase or decrease in the data; a “changing direction”.

A long-term increase or decrease in the data; a “changing direction”. Seasonality: A seasonal pattern of a fixed and known period. If the frequency is unchanging and associated with some aspect of the calendar, then the pattern is seasonal.

A seasonal pattern of a fixed and known period. If the frequency is unchanging and associated with some aspect of the calendar, then the pattern is seasonal. Cycle: A rise and fall pattern not of a fixed frequency. If the fluctuations are not of a fixed frequency then they are cyclic.

A rise and fall pattern not of a fixed frequency. If the fluctuations are not of a fixed frequency then they are cyclic. Seasonal vs Cyclic: Cyclic patterns are longer and more variable than seasonal patterns in general.

3.1 fpp2 Method: Plot Multiple Series On Same Axes

# Using fpp2

aus_prod_ts %>% # TS object

autoplot(facets=FALSE) # No facetting

3.2 fpp3 Method: Plot Multiple Series On Same Axes

# Using fpp3

aus_prod_tbl_long %>% # Data in long format

autoplot(value)

3.3 ggplot Method: Plot Multiple Series On Same Axes

Note that plotting multiple plots on the same axes has not been implemented into timetk . Use ggplot .