28 January 2017

IN THIS POST I WANT TO SHARE WITH YOU some code to create an animated plot of annual growth rates in U.S. Real Gross Domestic Product (GDP).

As in most of my posts, we’ll be creating these graphs in R.

# GDP Plot

On Friday the U.S. Bureau of Economic Analysis (BEA) released the “advance” or first estimate of GDP growth for the fourth quarter of 2016. With that advance estimate we now have estimates for full-year 2016 economic growth. For more on the GDP release, check out this article from the Wall Street Journal that has several interesting data visualizations.

Per the advance release, full year 2016 GDP growth was 1.6 percent. Note these calculations are made by comparing the average of GDP for each of the four quarters in 2016 to the average of GDP for each of the four quarters of 2015. Sometimes analysts like to compare Q4/Q4 measures, which will often give you different estimated growth rates.

If you go to text and full release you can find estimates for full year 2014, 2015 and 2016 GDP growth in Table 1. We could also download the GDP data for all years using the BEA’s interactive tables. However, for this exercise I decided to go a different route.

## Get the data

For this plot, I chose to use the quantmod package to get the data from the St. Louis Federal Reserve Economic Database (FRED).

Getting the data into R is very straightforward using quantmod:

The quarterly Real GDP series is named GDPC96 and using `getSymbols()` we can download the data directly from FRED. Then we turn the returned data into a data frame.

Next we’ll need to compute the annual average growth rates. I’m going to do it using the data.table library:

Let’s take a look at the GDP series and its annual growth rates:

## Add some stylin’ and some motion

Now we’re going to add a bit of styling and some animation to make the plot.

For smooth animations we’ll use tweenr. See my earlier post about tweenr for an introduction to tweenr, and more examples here and here.

Run that and you’ll get: