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
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:
- Part 1, The Time Plot
- Part 2, Autocorrelation
- Part 3, Seasonality
- Part 4, Anomalies and Anomaly Detection
- Part 5, Dealing with Missing Time Series Data
Then let’s learn the Time Plot
This tutorial focuses on,
plot_time_series(), a workhorse time-series plotting function that:
- Generates interactive
plotlyplots (great for exploring & shiny apps)
- Consolidates 20+ lines of
- Scales well to many time series
- Can be converted from interactive
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 = TRUEadds 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 = 2returns 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,
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_varto 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:
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:
- Time Series Preprocessing, Noise Reduction, & Anomaly Detection
- Feature engineering using lagged variables & external regressors
- Hyperparameter tuning
- Time series cross-validation
- Ensembling Multiple Machine Learning & Univariate Modeling Techniques (Competition Winner)
- NEW - Deep Learning with RNNs (Competition Winner)
- and more.
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).