Automated NBER recession shading in R

Fabian Scheler
November 1, 2021

In economics or equity research, we are frequently interested in displaying recession shades in our timer series graphics. These grey background stripes help viewers identify economic and financial distress times and easily spot the relation between these extraordinary periods and the data displayed. Usually, we utilize the NBER recession indicator, a binary variable that takes a value of 1 during recessions and is set to 0 otherwise.

The chart below provides an example, charting the development of aggregate balance sheets of major Central Banks denominated in USD. Recession shading allows the viewer to directly spot the relation between step-ups in balance sheet expansion and the past two recessions, notably the swift reaction to the Covid crisis.

An example chart including the typical NBER recession shading, produced in ggplot2

Recession Shading Example

Everybody, who has ever produced a visualization like this in Excel, probably shares my opinion that it is hilariously tedious. However, using a package like ggplot2 greatly simplifies this task.

However, the function below goes on step further by directly sourcing the required data from FRED.


After loading the function, all you need to do is to obtain an API key here.

Subsequently, it can be used to add recession shading just like any other ggplot2 element. A complete, reproducible example can be accessed through the link under this post.

Exporting Presentation Ready R Charts

While it is easy to visualize vast amounts of data in R or Python, the default graphics produced by these languages tend to look ... a bit too scientific. While this may be fine for journals or presentations to a scientific community, it doesn't work for marketing and sales activity in finance.

Fortunately, packages such as Plotly and ggplot2 have made it much easier to customize visualizations. Nevertheless, I think that the production of high-quality exports that can be copied into a PowerPoint Presentation is not so straightforward. For this reason, I am showing a couple of valuable codes below.

Example export with Plotly and Orca

Of course, it becomes much easier to work with Plotly if you subscribe to their premium services. However, not everybody has the budget for that. This is where Orca comes into play. Orca is a command-line Electron app that allows you to export Plotly graphics directly from RStudio with a single line of code. Just pull the app from GitHub, install and update the path.

Example download from Shiny with Plotly

As an avid Shiny user, I frequently produce Plotly charts in web applications that I would then like to use in Tex or PowerPoint. This is fairly simple as Plotly graphics come with a download link. However, the default setting triggers the download of a PNG graphic which doesn't look nice in a presentation. The command below allows you to easily change this. In this context, it can be helpful to add a numeric input widget that allows the user to directly configure the width and height of the exported plot.

Example export with ggplot2  

If you use ggplot2 to produce your graphics, the high-quality export from RStudio is much easier, and there is no need to install additional programs.

Example download from Shiny with ggplot2

Unfortunately ggplot2 doesn't provide a download bar in Shiny similar to Plotly's. Of course, there is ggplotly but according to my experience it tends to mess with graphics quite a bit. I usually utilize Plotly in my Shiny applications, but there can be situations where ggplot2 is more suitable or the easier route. The workaround below facilitates the download if you regularly produce ggplot2 charts in Shiny that are supposed to be used further in other applications.

Example download from Dash with plotly

Of course, this overview would not complete without mentioning Dash, Plotly's web framework for R, Python and Java based apps. The configuration is similar to Shiny's but it requires slightly different codes to change the default output from png to high quality svg graphics. The code below provides a simple example.