Interactive time series with 3 lines of R

Time Series analysis is a fundamental aspect of performance management.   Before we can begin to model time-series there’s huge dividend in just visualising the data.  We can see if there is a seasonal pattern, or is the time-series additive or multiplicative.

R comes with good plotting but one thing I’ve always wanted to do is zoom around a time-series. This is because patterns at the lower level are not always visible. We often have to group time to see the pattern but this is a transform I could do without.

At the last R London meetup I saw an excellent presentation from Enzo Martoglio who demo’d HTML Widgets.  HTML widgets for  R provide a bridge between R and HTML/JavaScript visualization libraries.

This opens a door for analysts and enables modern tools to be used for communicating model output.  I’ll leave that for another post. Back to time-series.

I kid you not, with 3 lines of code I was able to create an interactive time-series chart with zoom.  The package to use is dygraphs.   Basic example below.

 
#Data sourced from Rob Hyndman's Time Series Data Library 
#Hyndman, R.J. Time Series Data Library, http://data.is/TSDLdemo 
#Data Description 
#Monthly sales for a souvenir shop on the wharf at a beach resort town 
#in Queensland, Australia. Jan 1987-Dec 1993 

library(dygraphs) 
library(fma) 
dygraph(fancy,main = "Monthly Sales") %>% dyRangeSelector() 

Interactive Chart

We can see the seasonal pattern and the multiplicative trend.  This means a log transform will be needed before an additive model can be used. I’ll be posting more about time-series in the coming months.

These widgets can be output to the usual places:

  • R Console
  • R Markdown document
  • Shiny Web App

There are so many use cases for this.  It’s going to be interesting to see where things go.

 

About Lee Hawthorn

Data Professional
This entry was posted in R, Time Series and tagged , , . Bookmark the permalink.

Leave a Reply