29 January 2017

RECENTLY I HAVE BEEN EXPLORING FLEXDASHBOARDS to visualize data. In this post I want to focus on a tool I’ve found particularly useful, plotly.

Plotly enables you to make interactive html widgets that you can embed in your webpage or view from within R. I’ve been having a lot of fun converting existing visualizations I have made with ggplot2 into plotly visualizations using ggplotly.

In this post, let me share some of what I’ve been doing.

## The Plan

I’m going to include the code and discussion for several graphs I’ve been using. I will use updated data that we used in our Cross talk dashboard. These data cover weekly mortgage rates and house prices.

### The data

We’ve used these data before, but if you want to follow along they are:

Now that if you store the data in a folder called data, you can prep the data with the following code:

# Mortgage rate plots

I’ve made many mortgage rates plots (see here for 10 visualizations and here for a flexdashboard).

## Simple line plot

Converting this simple plot to plotly is quite easy. We simply have to use `ggplotly()` with the graph name (g1) as the function argument. If we want to add a range slider to select dates, we can add it with pipes via `%>% rangeslider()`.

The graph above is a static screenshot, but you can see the interactive version by clicking here.

## Distribution dot plot

I like to look at the distribution of observations.

In this plot we group each year and then jitter the x position of each dot. By comparing the mass of points, we can get a feeling about how the distribution of weekly rates has shifted by year.

With plotly, we can easily add animation to this plot to see how the distribution has evolved over time. We have to modify our original code slightly:

Once again, the graph above is a static screenshot, but you can see the interactive version by clicking here.

# House Price visualizations

Let’s explore the house price data. Last year, I made several [Visual Meditations on House Prices](/2016/05/08/visual-meditations-on-house-prices) exploring different ways to look at house price trends. Let’s revisit one of my favorite meditations and render it with plotly.

## State dot plot

Let’s explore trends in house prices, with a dot plot:

Once again, with slith modifications we can render this chart (and animate it over years) with plotly.

Once again, the graph above is a static screenshot, but you can see the interactive version by clicking here.

## Metro distribution

Let’s explore one more plot. This one will show the distribution of annual house price percentage changes over the more than 300 metro areas tracked in the Freddie Mac House Price Index.

We want to see how the distribution of house price percent changes across metro areas has changed over time. For September of each year in our sample we will find a kernel density function over metro house price percent changes. Then we’ll examine the data using a static ggplot2 graph. Finally, we’ll animate the plot using plotly.

### Fit a static plot

The code below takes our house price data and fits a kernel density function. We also create a couple of function `myf()` and `myf2()` to subset the data and stack a bunch of data frames using `rbind()`.

We can see from this graph that house price appreciation was was more dispersed from 2005-2010 than in recent years. Initially from 2005-2006 many metros had very high positive house price appreciation. Then, in the recession house prices fell quite a lot in mnay metros, but not all. In recent years house prices have tended to be positive, but we don’t see as many extremes as in 2005-2006.

The small multiple from using `facet_wrap()` with ggplot2 gives us one way of seeing this pattern. But an animation might also help us to better understand.

### Animated density with plotly

We can modify this code slighlty to make a plotly animated chart. The following code using ggplotly to animate the house price densities we estimated above:

Once again, the graph above is a static screenshot, but you can see the interactive version by clicking here.

# What’s Next?

I have been having a lot of fun with plotly recently. I’ve been using it with the flexdashboards I’ve been exploring, but it’s also nice to use on its own. Lots more is left to explore.