Mortgage Rate Kandinsky

THINGS ARE ABOUT TO GET A BIT MORE ABSTRACT IN THIS SPACE. Today we make some Kandinsky-style images with R.

This summer I was fortunate to spend some time at the Pompidou Centre in Paris. The Pompidou Centre houses the largest collection of modern art in Europe. I really enjoyed their collection of abstract and minimalist paintings.

Well, turns out we can make our own abstract-style art using a Kandinsky R package from Giora Simchoni. Read about the package here at Giora’s blog.

The package takes any data frame and turns it into Kandisky-style painting.

Data and code

For these paintings I’m going to use the history of U.S. average weekly 30-year fixed rate mortgage rates from the Freddie Mac Primary Mortgage Market Survey. See my earlier post for 10 amazing ways to visualize rates and here for even more amazing visualizations.

I’ve saved a simple text file with two columns, one with the date and one with the historical rate called rate30yrfrm.txt download.

Just save them in a directory (I called mine “data”) and load the libraries.

#####################################################################################
# load libraries
library(data.table) # i want to use fread and other data.table utilities later
library(kandinsky)  # available on Github at https://github.com/gsimchoni/kandinsky
#####################################################################################

#####################################################################################
# load data:
#####################################################################################

dt<-fread("data/rate30yrfrm.txt")
dt$date<-as.Date(dt$date, format="%m/%d/%Y")  # conver to date

#####################################################################################
#  draw plot
#####################################################################################
kandinsky(dt)
## 
## Attaching package: 'purrr'
## The following object is masked from 'package:data.table':
## 
##     transpose

That’s pretty fun. Let’s try it for just the year 2016. Let’s also add a label.

kandinsky(dt[year(date)==2016])
  grid.text(label=paste("30-year fixed mortgage rates in 2016",
                        "\n@lenkiefer, Made using R package Kandinsky"),
            gp=gpar(fontsize=12),
            x=.95,y=0.05,just="right")

That’s pretty fun. What if we want to animate it? You know, using tweenr?

See this post about tweenr for an introduction to tweenr, and more examples here and here.

In this post we’ll do things slightly differently. Instead of using the animation package, I’m going to save the image files and then call a program, imagemagick, outside of R to make the gif.

We are going to take a subset of our data, just the weeks in 2017. Then we’ll take 4 week blocks and draw a Kandinsky plot. We’ll tween between 4 week intervals and see how they evolve.

#####################################################################################
library(tidyverse)  # we'll need some tidyverse functions
library(tweenr)
#####################################################################################

#####################################################################################
# Data stuff: 
df<-filter(dt,year(date)>2016)  #just get data for 2017
N<-nrow(df)  # get the number of weeks in our data

# Create a function to subset our data
myf4 <- function(i) {
  # take a four week interval starting at week i
  return( df[i:(i+3),] %>% select(date,rate))  
  }

# use lapply to generate the list of data sets:
# stagger from weeks 1 to N-3 by 4
my.list<-lapply(seq(1,N-3,4),myf4)  

tf <- tween_states(my.list, tweenlength= 2, statelength=3,
                   ease=rep('cubic-in-out',24),
                   nframes=120)
# Turn the tweened data fram into a data.table
tf<-data.table(tf)
#####################################################################################

#####################################################################################

pathtoyourfolder <- ""
# be sure to set pathtoyourfolder to some folder where you can store images

#####################################################################################

#####################################################################################
# Loop for animation
for (ii in 1:max(tf$.frame)) {
  file_path = paste0(pathtoyourfolder, "/plot-",5000+ii ,".png")
  # I add 5000 to the index so that image 10 comes after (not before) image 9
  # (need to pad with some leading numbers)
  png(file_path) 
  kandinsky(tf[.frame==ii])
  grid.text(label=paste("30-year rates from",
                        tf[.frame==ii,][1,]$date,"to",
                        tf[.frame==ii,][4,]$date,
                        "\n@lenkiefer, Made using R package Kandinsky"),
            gp=gpar(fontsize=12),
            x=.95,y=0.05,just="right")
  dev.off()
  print(ii)
}
#####################################################################################

For this to work, you’ll need imagemagick on your machine. Navigate to the directory where you saved your images and run the following command in the console:

magick convert -delay 10 loop -0 *.png kandinsky.gif

That should compile all your images (.png files) into a single gif.

kandinsky

How could it work for you?

You can feed all different types of datasets into the kandinsky function. Choose some that are near and dear to you and try them out.

 Share!