Introducing tsviz, interactive time series visualization in R Studio
By: Emanuele Fabbiani
Tags: Machine Learning, Time Series Analysis, Data Science, R, Data Visualization

Introducing tsviz, interactive time series visualization in R Studio

Why and how we developed an R Studio add-in

tsviz logo

Context: R tools for data visualization

We all know. R is great for data visualization.

Hadley Wickham’s ggplot2 provides a simple yet elegant syntax to create very effective charts. Moreover, in the unlikely event that ggplot2 does not support some exotic chart, R offers lots of valid alternatives, like lattice.

In this scenario, at the beginning of 2016, a new package was released. Following the open-source publication of the JavaScript library, Plotly for R was made available on CRAN. Plotly allowed to create interactive charts, where the user can zoom, pan, select data and see additional information by hovering on datapoints.

Interactivity may just seem a fancy feature. And it probably is, in academic works, where data is known and what really matters is to produce clear and informative charts for publications.

But…

For practitioners, interactivity is a big deal. It helps to be close to the data and to gain early insight, thus speeding up the whole process of Data Exploration.

No surprise Plotly had an immediate success, and its popularity grew more and more as new features were added, like the integration with ggplot2.

However, even Plotly and ggplot2 come at a cost.

Plotly R package daily downloads. Data from rpackages.io.

Motivation: the quest for early insight

Plotly and ggplot2 fail to provide a common syntax: mixing charts in pure plotly with plotlified figures created with ggplot2 is tricky. Even with the help of the libraries, creating effective visualizations still takes time.

Most data scientists love creating charts. Charts are our way to communicate, the single most effective language to share the messages we get from data.

However, creating charts to share results is different from creating charts to explore and investigate data.

While we are happy to spend lots of time on a single figure in the first situation, in the second one we are not. We want to get the knowledge as soon as possible, so that we can take better decisions and avoid dangerous misunderstandings.

Fortunately, R and R Studio have an answer for this need. In early 2016, R Studio announced the support for add-ins. Effectively extensions of the IDE, add-ins may help with development-related tasks, but also provide support for data analysis. For instance, some add-ins ease data subsetting, while others provide assistance in choosing colors for charts.

Thus, why not creating an add-in to ease the usage of interactive charts?

During the last months, in xtream we worked on several projects involving time series analysis. And several times we repeated the same process:

Plot the time series Examine the scatter plots between target series and features Build and analyze autocorrelation, partial autocorrelation and periodogram

Sure, we built and used functions, but it was tedious to create a script or an R Markdown document to use them all the times.

So, we decided to encapsulate the most useful features in an add-in.

Creation: on the shoulders of giants

The process of developing the R Studio plugin was incredibly quick, thanks to a lot of useful resources. Here is the list:

Result: tsviz

tsviz is an R package which contains the tsviz add-in and a sample dataset.

You can install tsviz from CRAN, by running:

Or you can download the development version from GitHub. Make sure you have the devtools package installed and run:

Once installed, you can try the add-in:

You should be able to do something like this:

tsviz usage example

tsviz can also be run anytime from the add-ins menu in R Studio: there is no need of loading the package with library() .

Of course, you can try tsviz on your own data.

When launched, the add-in automatically looks for suitable data frames in the the Global Environment. A data frame can be analyzed with tsviz if:

it has at least one column of type Date

it has at least one column of type numeric

References

tsviz is on GitHub: https://github.com/xtreamsrl/tsviz. It is still under early development: every feedback or bug report is welcome!