A 3D View of the US Treasury Yield Curve
Analysts typically look at bond yields as time series (tracking yield changes over time) or across different maturities (chaining interest rates on different contract lengths to form the yield curve for any given date). The St. Louis Fed's FRED database provides the relevant data on US Treasury Bonds and US Treasury Bills, making it easy to analyse bond returns in either of these ways. But sometimes you want to look at both dimensions together. In those situations, it can be useful to create a 3D graph. This post explains how to do this using R and plotly.
First, we pull data for US Treasury Bond yields from 1 month to 30 years from the St. Louis Fed's FRED database and plot them in 2D using ggplot2. Next, we use plotly to create a “surface plot”, which is plotly's term for a 3D graph. The result is an interactive 3D graph that we can rotate, pan, and zoom right within your browser, thanks to the power of WebGL. Click here to try it yourself (try switching browsers if the graph doesn't appear right away).
To see how this all works, grab the R script to generate these plots here. Also, check out this page by the New York Times to see what insights a 3D yield curve graph can provide.
Download the R script here. For the interactive chart click here.
Useful Links
- More information on the statistical programming language R can be found here
- St. Louis Fed FRED database
- St. Louis Fed FRED data on US Treasury Bonds
- St. Louis Fed FRED data on US Treasury Bills
- ggplot2 reference
- plotly R reference