# Introduction

LAST WEEK I SHOWED YOU 10 TASTY WAYS TO VISUALIZE MORTGAGE RATES, but I’ve got another delicious one for you. Y’all like ribbon candy?

As before we’ll create this chart with R.

## 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. See my earlier post for some additional information on the data we’ll use.

Here’s the first few rows of our data:

 date rate drate name year month week 30-year Fixed Mortgage Rate (%) 1 Apr 02,1971 7.33 1971 4 14 2 Apr 09,1971 7.31 -0.02 down 1971 4 15 3 Apr 16,1971 7.31 0 no change 1971 4 16 4 Apr 23,1971 7.31 0 no change 1971 4 17 5 Apr 30,1971 7.29 -0.02 down 1971 4 18 6 May 07,1971 7.38 0.09 up 1971 5 19 7 May 14,1971 7.42 0.04 up 1971 5 20 8 May 21,1971 7.44 0.02 up 1971 5 21 9 May 28,1971 7.46 0.02 up 1971 5 22 10 Jun 04,1971 7.52 0.06 up 1971 6 23 Source: Freddie Mac Primary Mortgage Market Survey

The data are weekly observations on mortgage rates running from April 2, 1971 through December 8, 2016. Now let’s take these data and make our visualization.

## Data prep

We need to do a tiny bit of data preparation which is made quite easy by the data.table() structure.

# making rolling 52 week min and max
pmms30yr[, rmax.52:=rollapply(rate,52,max,na.rm=T,fill=NA,align="right")]
pmms30yr[, rmin.52:=rollapply(rate,52,min,na.rm=T,fill=NA,align="right")]
#compute 52-week max & min and rate
pmms30yr[,rup:=min(rate,rmin.52),by=date]
pmms30yr[,rdown:=max(rate,rmax.52),by=date]

Now that we have the data ready, we can create our lovely ribbon plot:

#create plot:
ggplot(data=pmms30yr,aes(x=date,y=rate))+
geom_ribbon(aes(ymin=rup,ymax=rate),fill=viridis(10)[2],alpha=0.6)+
geom_ribbon(aes(ymin=rdown,ymax=rate),fill=viridis(10)[8],alpha=0.6)+
geom_line(size=1.05)+
theme_minimal()+
geom_rug(sides="b",aes(color=(rate-rmin.52)/(rmax.52-rmin.52)))+
scale_color_viridis(name="Rate as %\nof min/max\n0% at min,\n50%=halfway,\n100% at max",
direction=-1,label=percent,end=0.8)+
theme(legend.position=c(0.22,0.15),legend.direction="horizontal")+
labs(x="", y="",
title="30-year fixed mortgage rate (%)",
subtitle="Shaded area 52-week trailing min/max, purple from 52-week min to current, green from current to 52-week max.\nStrip at bottom shows weekly rate as percent of 52-week min/max (0% at min, 100% at max).",
caption="@lenkiefer Source: Freddie Mac Primary Mortgage Market Survey")+
theme(plot.title=element_text(size=16),
plot.subtitle=element_text(face="italic",size=8))+
theme(plot.caption=element_text(hjust=0))+
theme(plot.margin=unit(c(0.25,0.25,0.25,0.25),"cm"))+
scale_x_date(limits=c(as.Date("1972-01-01"),as.Date("2016-12-08")))+
coord_cartesian(ylim=c(02.5,20))

## What is this?

This is a composite chart consisting of:

1. A line
2. Two ribbons
3. A marginal rugplot at the bottom

### The line

The line is just a standard time series line created with geom_line() showing weekly average mortgage rates. There’s nothing particularly special about it, but it’s the anchor that lets us make sense of the rest of the plot.

### Two ribbons

The ribbons, shaded purple and green, show a trailing 52-week min and max for the interest rate. Together they show the range for the interest rate in the past year. Let’s look at just the last row of our data set:

 date rate drate name year month week rmax.52 rmin.52 rup rdown 30-year Fixed Mortgage Rate (%) 2392 Jan 26,2017 4.19 0.1 up 2017 1 4 4.32 3.41 3.41 4.32 Source: Freddie Mac Primary Mortgage Market Survey

Here we see that for the week of December 8, 2016 the average mortgage rate was 4.13 percent. The 52-week max was also 4.13 percent. The 52-week min was 3.41 percent. This means that over the 52-week window ending December 8, 2016 mortgage rates ranged as low as 3.41 percent and as high as 4.13 percent. Because the last observation is equal to the 52-week max there’s no green shaded area for this week, but it’s all purple in the plot, extending down from 4.13 percent to 3.41 percent.

We can plot just the line and ribbons for the weeks in 2016:

We see a lot of green for most of 2016, indicating that for much of the year mortgage rates were substantially below where they were 52-weeks ago.

### The rug really ties the whole plot together.

What’s the point of the rug? Well, it really ties the plot together.

Consider the plot below which shows just rates for 2016, but adds in the rug plot:

The rug is colored to correspond to the where the black line is in relation to the edges of the shaded areas. If the current rate (black line) is at the max so that there is no green area, then the tick will be dark purple. If the current rate is at the 52-week min so that there is no purple area then the tick will be yellow.

Zooming out, we can see that the rug shows us at a glance how rates have been trending on a year-over-year basis.

Now we’ve triple-encoded the range of rates in this chart. The line captures the trend, the purple ribbon indicates rates are rising relative to a year ago, while green shading indicates rates have been falling over the past year. Likewise the ribbon at the bottom, which is conveniently placed near the x axis, displays the current rate relative to 52-week min and max.