WE ARE BACK WITH EVEN MORE WAYS TO VISUALIZE mortgage rates. A few days ago, I shared some ways to visualize mortgage rate trends and here I posted some additional gifs without the code. I’m going to expand on that last post with R code for one those charts, and give you a totally new one.
The data I’m going to use are estimates of weekly U.S. average 30-year fixed mortgage rates from the Primary Mortgage Market Survey from Freddie Mac. These data can be easily downloaded from the St. Louis Fred database here.
I have the data saved in a simple text file with a column for data, the mortgage rate, and helper columns week, month, and year, where week is the week number starting with the first week of the year.
Let’s load the data and take a peek.
30-year Fixed Mortgage Rate (%)
Source: Freddie Mac Primary Mortgage Market Survey
The data are weekly observations on mortgage rates running from April 2, 1971 through December 15, 2016 (we added one week since last time).
Let’s start by creating this chart:
This chart shows how the distribution of weekly mortgage rates has changed since the year 2000. Even though rates have been heading higher recently, they are still quite low, even judging by the standards of this century.
To make this chart we need to first take our data and bin the data into buckets. We can easily do this using the cut function to “cut” up the data weekly interest rates into non-overlapping intervals. Then we can use the data.table() structure to easily compute summary statistics by cuts.
Add smooth transitions
To add smooth transitions we use Tweenr.
Everything is a tile
Somehow I just stumbled upon geom_tile(). Now everything is a tile.
Earlier we made a strip chart. If you don’t want to click here it is again (go to link for code):
Make it a tile
The tile chart is very similar to the strip, but instead of having one left/right dimension we’ll add an up/down dimension. We’ll construct a week number variable indicating the week of the year and display that on the x axis. Then on the y axis we’ll have years going down.
Now we can combine the tile chart with a column chart:
And we can animate it. First, let’s create a function for the column chart of 52-week differences diff.plot() and a function for the tile plot tile.plot(). Let’s examine the functions and see how they work when we give it a year, 2004 before the end of our sample:
This plot shows us what our plot will look like in after we get to year 2004. By adding in the full data with alpha=0 (so it is transparent) our axis will be fully expanded. We could do it by manually setting the axis, but I just did it this way.
Now we can loop through the data to create the animation: