Interactive charts with chartbookR

Creating impressive interactive charts with chartbookR

“There is no such thing as information overload. There is only bad design.” (— Edward Tufte).

There is nothing worse than charts overladed with information.

One solution to this are interactive charts that let users select the time series they’re interested in, zoom in on them, and focus on individual data points.

The chartbookR package makes the creation of interactive charts very easy, with minimal coding. This tutorial outlines how to create such charts and how to add further interactive features.

Preparation

If you’re not yet familiar with chartbookR I encourage you to first read this introductory tutorial and to install the chartbookR package. With that out of the way, let’s look at how to create useful interactive charts.

As a next step, install the highcharter package which is needed for interactive charts and is not installed by chartbookR:

install.packages("highcharter")

Please note that the highcharter package builds on Highcharts, which is not free for commercial or governmental use. Read more on Highcharts and their pricing models here.

Data download and charting

Now, load the chartbookR package and download the following set of data from FRED:

library(chartbookR)
zoo <- getFRED(tickers = c("T10Y3M", "T10Y2Y"), names = c("3M10Y", "2Y10Y"), time = "30Y")

This will give us a zoo object of daily time series data for two of the main US Treasury Rate curves, namely the 3M-10Y, and the 2Y-10Y curve. Both have historically been good recession predictors, with curve inversions usually followed by a US recession with a 12-18 months lag.

Now, convert this time series data to weekly frequency so that our dynamic chart will run more smoothly, then convert to basis points:

library(xts)
zoo <- apply.weekly(zoo, mean)
zoo <- zoo*100

In order to create a simple dynamic chart with this data, we can now use chartbookR’s HiChart function by doing:

HiChart(zoo)

The function is a wrapper for Joshua Kunst’s highcharter package and makes interactive highstock charts a breeze to work with.

This results in an interactive chart, in which users can remove time series (by clicking them), zooming in on time frames, and getting further information on data points by hovering over them:

These charts are highly customizable as you will see by typing ?HiChart into your R console.

Improving color palette & chart

If you want to change the color palette, simply load another color vector and re-run the chart, by doing:

palette(c("#4663AC", "#97A4CC", "#CEBC9A", "#BF7057", "#ADAFB2", "#E7C667"))
HiChart(zoo)

The chart already looks a lot nicer now with the new palette:

We can easily create a more sophisticated interactive chart that includes recession shading and highlights the zero-line by doing:

HiChart(zoo, rec = T, h1 = 0)

In order to give the user even more options to zoom in on the data we can add a date navigator, then we add a title:

HiChart(zoo, rec = T, h1 = 0, navigator = T, title = "US Curve Steepness, in bps", title_adj = "left")

This is it! Your first fully functional interactive chart.

For more information on chartbookR revisit this site regularly and check out the package help files.