Site icon R-bloggers

Time Series in 5-Minutes, Part 1: Visualization with the Time Plot

[This article was first published on business-science.io, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Have 5-minutes? Then let’s learn time series. In this short articles series, I highlight how you can get up to speed quickly on important aspects of time series analysis. Today we are focusing on the most fundamental tool, the time plot. Learn how to make interactive (plotly) and static (ggplot2) visualizations easily with timetk.

Updates

This article has been updated. View the updated Time Series in 5-Minutes article at Business Science.

Time Series in 5-Mintues
Articles in this Series

The time plot – A fundamental tool in your arsenal

I just released timetk 2.0.0 (read the release announcement). A ton of new functionality has been added. We’ll discuss some of the key pieces in this article series:

???? Register for our blog to get new articles as we release them.

Have 5-Minutes?
Then let’s learn the Time Plot

This tutorial focuses on, plot_time_series(), a workhorse time-series plotting function that:

Load the following libraries. For the purposes of this tutorial, I’m setting all plots to static ggplot2 using interactive <- FALSE, but I encourage you to switch this to TRUE to see how easy it is to make interactive plotly plots.

library(tidyverse)
library(lubridate)
library(timetk)

# Setup for the plotly charts (# FALSE returns ggplots)
interactive <- FALSE


Plotting a Single Time Series

Let’s start with a popular time series, taylor_30_min, which includes energy demand in megawatts at a sampling interval of 30-minutes. This is a single time series.

taylor_30_min
## # A tibble: 4,032 x 2
##    date                value
##    <dttm>              <dbl>
##  1 2000-06-05 00:00:00 22262
##  2 2000-06-05 00:30:00 21756
##  3 2000-06-05 01:00:00 22247
##  4 2000-06-05 01:30:00 22759
##  5 2000-06-05 02:00:00 22549
##  6 2000-06-05 02:30:00 22313
##  7 2000-06-05 03:00:00 22128
##  8 2000-06-05 03:30:00 21860
##  9 2000-06-05 04:00:00 21751
## 10 2000-06-05 04:30:00 21336
## # … with 4,022 more rows

The plot_time_series() function generates an interactive plotly chart by default.

taylor_30_min %>% 
  plot_time_series(date, value, 
                   .interactive = interactive, # <- Toggle this TRUE/FALSE
                   .plotly_slider = TRUE)

Plotting Groups

Next, let’s move on to a dataset with time series groups, m4_daily, which is a sample of 4 time series from the M4 competition that are sampled at a daily frequency.

Visualizing grouped data is as simple as grouping the data set with group_by() prior to piping into the plot_time_series() function. Key points:

m4_daily %>%
  group_by(id) %>%
  plot_time_series(date, value, 
                   .facet_ncol = 2, .facet_scales = "free",
                   .interactive = interactive)

Visualizing Trend with the Smoother

You may be wondering, what is that blue line that keeps showing up on all of our plots. It’s called a smoother, and it’s a really awesome way to visualize trend through the noise in a time series.

We can adjust the smoother using:

Here I’m changing the smooth_span = 0.25 to increase the flexibility by using 25% of the data in the smoother. Not that the time series I’m using is a weekly series, m4_weekly.

m4_weekly %>%
  group_by(id) %>%
  plot_time_series(date, value, 
                   
                   # Smoother
                   .smooth = TRUE, 
                   .smooth_span = 0.25,           # <- Uses % of data
                   # .smooth_period = "52 weeks", # <- Uses windows of data
                   
                   .facet_ncol = 2, .facet_scales = "free",
                   .interactive = interactive)

Visualizing Transformations & Sub-Groups

Let’s switch to an hourly dataset with multiple groups. We can showcase:

  1. Log transformation to the .value
  2. Use of .color_var to highlight sub-groups.

The intent is to showcase the groups in faceted plots, but to highlight weekly windows (weekly sub-groups, using week()) within the data while simultaneously doing a log() transformation to the value. This is simple to do:

  1. .value = log(value) Applies the Log Transformation
  2. The data is ungrouped, so we can add facets internally using the ... to supply one or more facet columns.
  3. .color_var = week(date) The date column is transformed to a lubridate::week() number. The color is applied to each of the week numbers.
m4_hourly %>%
  group_by(id) %>%
  plot_time_series(date, log(value),             # Apply a Log Transformation
                   .color_var = week(date),      # Color applied to Week transformation
                   # Facet formatting
                   .facet_ncol = 2, 
                   .facet_scales = "free", 
                   .interactive = interactive)

Static ggplot2 Visualizations & Customizations

All of the visualizations can be converted from interactive plotly (great for exploring and shiny apps) to static ggplot2 visualizations (great for reports).

taylor_30_min %>%
  plot_time_series(date, value, 
                   .color_var = month(date, label = TRUE),
                   
                   .interactive = FALSE,   # <- Returns static ggplot
                   
                   # Customization
                   .title = "Taylor's MegaWatt Data",
                   .x_lab = "Date (30-min intervals)",
                   .y_lab = "Energy Demand (MW)",
                   .color_lab = "Month") +
  scale_y_continuous(labels = scales::comma_format())

Time Series Course (Coming Soon)

I teach Time Series (timetk, more) in my Time Series Analysis & Forecasting Course. If interested in learning Pro-Forecasting Strategies then join my waitlist. The course is coming soon.

You will learn:

Signup for the Time Series Course waitlist


Have questions on using Timetk for time series?

Make a comment in the chat below. ????

And, if you plan on using timetk for your business, it’s a no-brainer – Join my Time Series Course Waitlist (It’s coming, it’s really insane).

To leave a comment for the author, please follow the link and comment on their blog: business-science.io.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.