Time Series in 5-Minutes, Part 2: Visualization with the Time Plot
Written by Matt Dancho
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
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.
Then let’s learn the Time Plot
This tutorial focuses on,
plot_time_series(), a workhorse time-series plotting function that:
- Generates interactive
plotly plots (great for exploring & shiny apps)
- Consolidates 20+ lines of
- Scales well to many time series
- Can be converted from interactive
plotly to static
Load the following libraries. For the purposes of this tutorial, I’m setting all plots to static
interactive <- FALSE, but I encourage you to switch this to
TRUE to see how easy it is to make interactive
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.
plot_time_series() function generates an interactive
plotly chart by default.
- Simply provide the date variable (time-based column,
.date_var) and the numeric variable (
.value) that changes over time as the first 2 arguments
.interactive = TRUE, the
.plotly_slider = TRUE adds a date slider to the bottom of the chart.
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:
- Groups can be added in 2 ways: by
group_by() or by using the
... to add groups.
- Groups are then converted to facets.
.facet_ncol = 2 returns a 2-column faceted plot
.facet_scales = "free" allows the x and y-axis of each plot to scale independently of the other plots
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:
- Toggle on/off:
.smooth = TRUE/FALSE
- Change the flexibility of the line: Try
.smooth_period = "52 weeks" (30-days of data) or
.smooth_span = 0.25 (25% of data). By default,
.smooth_span gets priority.
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,
Visualizing Transformations & Sub-Groups
Let’s switch to an hourly dataset with multiple groups. We can showcase:
- Log transformation to the
- 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:
.value = log(value) Applies the Log Transformation
- The data is ungrouped, so we can add facets internally using the
... to supply one or more facet columns.
.color_var = week(date) The date column is transformed to a
lubridate::week() number. The color is applied to each of the week numbers.
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).
- Toggle Interactive/Static:
.interactive = TRUE/FALSE
- Add title, legend, x & y-axis labels:
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 the Time Series Course.