IN THIS POST I WANT TO PROVIDE some additional examples of using tweenr and gganimate to create nice smooth transitions in an animated GIF. In this post we’ll look at an animated waterfall chart.
For this data I’m going to use the National Income and Products Accounts from the U.S. Bureau of Economic Analysis (BEA). Specifically we’ll be looking at the contributions to growth in Real Gross Domstic Product, which you can find here.
Ultimately, we will end up with this:
How to construct the chart
I found a nice example of how to make a waterfall chart in R that I used. The basic idea is to draw a rectangle shifted over to the right (or left) depending on the data. The basic idea is to show how individual contributions contribute positivley or negatively to a whole. In this case, we’re going to look at how various components of the economy affect total economic growth in a given quarter.
If you follow the link above, you can download a spreadsheet from BEA. I had to do a bit of wrangling, which I happened to do in Excel. I’m not writing up the data wrangling bits here, but you can check out my earlier post on house prices to get some ideas.
After some wrangling, I ended up with data that looked like this:
Next I imported these data into R as a text file name gdpc.txt. We’ll pick up the R code from there:
These data are now ready to draw the waterfall chart. Let’s make one:
Draw a static chart
Ready for animation
Now we’re ready to animate. For more explicit discussion, see my earlier post. The code below takes the static plot and animates it with nice smooth transitions using tweenr.
You can adjust the dates to look at different periods: