18 December 2016

# Introduction

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

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.

 date rate year month week 30-year Fixed Mortgage Rate (%) 2377 Oct 13,2016 3.47 2016 10 41 2378 Oct 20,2016 3.52 2016 10 42 2379 Oct 27,2016 3.47 2016 10 43 2380 Nov 03,2016 3.54 2016 11 44 2381 Nov 10,2016 3.57 2016 11 45 2382 Nov 17,2016 3.94 2016 11 46 2383 Nov 23,2016 4.03 2016 11 47 2384 Dec 01,2016 4.08 2016 12 48 2385 Dec 08,2016 4.13 2016 12 49 2386 Dec 15,2016 4.16 2016 12 50 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).

# Distribution bars

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.

To add smooth transitions we use Tweenr.

# Everything is a tile

Somehow I just stumbled upon geom_tile(). Now everything is a tile.