”“Data scientists … spend from 50 percent to 80 percent of their time … collecting and preparing unruly digital data, before it can be explored for useful nuggets.” (— New York Times).
The chartbookR package allows the convenient creation of economic and financial data chartbooks. It handles most of the data wrangling and can thus create large chartbooks with few lines of short code.
chartbookR consists of four types of functions:
- Data download:
- Data transformation:
- Data visualization:
- Saving to Powerpoint:
An example chart
Let’s look at an example chart to demonstrate chartbookR’s functionality.
Fig. 1 shows that manufacturing employment has fallen by 25% since the turn of the century. Despite this decline, manufacturing output increased by almost 15% since the year 2000. This was possible because of a 60% increase in productivity, mainly in the years before the global financial crisis.
These trends were further accelerated by the global financial crisis in 2007-08 and partially explain the ongoing trade war that the US is having with China. You can read more about these trends here.
zoo <- getFRED(tickers=c("PRS30006013", "OUTMS", "OPHMFG"), names=c("Employment", "Output", "Productivity")) zoo <- Transform(data=zoo, rebase=TRUE) LineChart(zoo, d1=4:6, no=1, title="The Decline of US Manufacturing Employment (indexed, 1987=100)", y1_def=c(50,250,25), grid=TRUE, rec=TRUE)
In the following sections, we will re-create this chart from scratch, step-by-step, explaining the features of chartbookR. I recommend that you follow these steps in R Studio.
As you will see, the efficiency with which chartbookR does the data wrangling for you makes it possible to create large chartbooks in very short time.
Before we can start, we need to install
chartbookR from github. If you haven’t installed
devtools yet, now is a good time:
devtools and install
If you encounter problems during the installation, try to (re-)install the
Furthermore, if you are behind a corporate proxy server, try the following and adjust with your own parameters:
install.packages("httr") library(httr) set_config(use_proxy(url="your.proxyserver.com", port=8080, username="proxy_username", password="proxy_password")) devtools::install_github("kilianreber/chartbookR")
For the data download there are two functions to choose from:
getFRED, which return the downloaded time series as
If you have a Bloomberg terminal
Then you should use
getBBG to download data as Bloomberg provides large amounts of timely and high-quality data, which you can download with different frequencies.
Run a Bloomberg terminal session and look for the respective indices, i.e. “US Manfuacturing Employment”, “US Manufacturing Output”, and “US Manufacturing Productivity”. Simply plug the indices into the
tickers argument of
getBBG and give them proper names using the
Since two out of three indices are quarterly, you want to set
freq="QUARTERLY". And since the data starts in 1987, you want to set
time="31Y". This will download the full history of available data.
zoo <- getBBG(tickers=c("USMMMANU", "PRODMANO", "PRODMANH"), names=c("Employment", "Output", "Productivity"), freq="QUARTERLY", time="31Y")
This returns a zoo object with the respective time series.
If you don’t have a Bloomberg terminal
Then you can still download the same data from the St. Louis Fed’s FRED database. Simply head over to https://fred.stlouisfed.org/, find the right tickers for “US Manfuacturing Employment”, “US Manufacturing Output”“, and “US Manufacturing Productivity” and plug them into the
time=31Y is not required here because
getFRED always downloads all available data. Also, setting the frequency is not necessary, since the
getFRED function doesn’t offer this functionality; all selected indices are of quarterly frequency. The function will look like this:
zoo <- getFRED(tickers=c("PRS30006013", "OUTMS", "OPHMFG"), names=c("Employment", "Output", "Productivity"))
This, again, returns a zoo object with the respective time series.
getBBG, you should have gotten the same results. Now we need to rebase the data.
Transforming the data is made easy with the
Transform function, which can do rebasing, compute periodical changes, as well as other transformations. For this chart, we want to rebase all three time series, by doing:
zoo <- Transform(data=zoo, rebase=TRUE)
Transform function computes the rebased indices and appends them to the exising zoo object.
We are now ready to visualize the rebased data.
To create the above line plot we can use the
LineChart function. It is based on R’s plot function but simplifies a lot of the chart fine-tuning, such as secondary y-axes, adding a grid, shading recessions, adding ablines, etc.
To plot the rebased indices in columns 4 to 6, use:
This creates a basic line plot:
We can easily produce a more sophisticated chart instead by including a title, grid lines, recession shading, and a horizontal line at 100. You can add a title by specifying
title="The Decline of US Manufacturing Employment (indexed, 1987=100)". Grid lines are added by specifying
grid=TRUE, recession shading is added with
Finally, add a figure number by adding
no=1 and a more granular y-axis by specifying
LineChart(zoo, d1=4:6, no=1, title="The Decline of US Manufacturing Employment (indexed, 1987=100)", y1_def=c(50,250,25), grid=TRUE, rec=TRUE)
This will produce:
LineChart uses its own default color palette, but you can change this anytime with the following code in the console – just plug in your preferred color scheme, for example:
palette(c("#4663ac", "#97a4cc", "#CEBC9A", "#595959", "#BF7057", "#ADAFB2", "#E7C667"))
Saving to Powerpoint
You can easily save this chart to Powerpoint, which is particularly useful once you have a whole chartbook that you’d like to update automatically. Firstly, install and load the package
Then, load an existing Powerpoint (*.pptx) file as template (download example template here), which includes a correctly formatted title slide:
doc <- read_pptx("D:/Your/path/Powerpoint_Template.pptx")
Then, copy the current plot to a new slide within that document, by providing an optional header for the slide, as well as an optional source footnote, for example:
makePPT(header="US GDP Nowcasts", source="FRED (Fed St. Louis)")
Finally, save the Powerpoint file by doing:
This is it! Your first fully automated chartbook. Rinse and repeat above steps for a comprehensive chartbook that can be automatically updated from within R.
I strongly encourage you to read more on David Gohel’s excellent officer package to fine-tune your export to Powerpoint.
For more information on chartbookR revisit this site regularly and check out the package help files in the R console.