“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.
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:
Data download and charting
Now, load the chartbookR package and download the following set of data from FRED:
library(chartbookR) zoo <- getFRED(tickers = c("T10Y3MM", "T10Y2YM"), names = c("3M10Y", "2Y10Y"), time = "30Y")
This will give us a zoo object of monthly 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.
In order to create a simple dynamic chart with this data, we can use chartbookR’s HiChart function by doing:
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: