Interactive charts with chartbookR
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
“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")
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:
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:
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.