TIME TO TRY OUT ANOTHER HOUSE PRICE VISUALIZATION.
In this post we’ll try out a new way to visualize recent house price trends with R.
Just this wekeend I saw a new package geofacet for organizing ggplot2 facets along a geographic grid. It allows use to construct a small multiple graph that roughly looks like the United States. (Thanks to [@yoniceedee](https://twitter.com/yoniceedee) for recommending geofacet).
Let’s try it out using the same house price data we visualized recently. Details about the data are in that post, but we’ll be using the Freddie Mac House Price Index to once again visualize state house price trends.
Let’s get to it.
We’ll start this post with our data in hand. Just follow along here to get the data. We’ll begin with a data frame called
df.state that looks like so.
|Our data frame df.state|
|Source: Freddie Mac House Price Index|
The key variable we’ll need is
hpa which captures the 12-month percent change in the house price index. Conveniently, we already have a state identifier to use with
facet_geo (though we’ll have to rename it state).
Facet my geo
With our data in hand, it’s pretty easy to create our plot. Our data goes from 1975 to March 2017, so we’ll subset it to focus on more recent trends. We’ll plot 12-month percent changes in house prices (variable
hpa in our data) since 2000. We’ll also restrict our attention to March of each year (so we can end with the latest data in March 2017).
### Run this to get library: # devtools::install_github("hafen/geofacet") library(ggplot2) library(viridis)
## Loading required package: viridisLite
library(scales) library(geofacet) ## Subset data and drop US averages df.state2<-filter(df.state, year(date)>1999 & month(date)==3 & !(geo %in% c("United States not seasonally adjusted", "United States seasonally adjusted" ))) # set up date limits for plot xlim<-c(min(df.state2$date),max(df.state2$date)) # create state variable df.state2$state<-df.state2$geo # create plot: ggplot(df.state2, aes(x=date, y=hpa,fill=hpa)) + # geom col for little bars geom_col()+ # use facet_geo facet_geo(~ state, grid = "us_state_grid2")+ # my go to theme theme_minimal()+ # the colors! scale_fill_viridis(option="C",limits=c(-0.35,0.35), label=percent,name="12-month\n% change")+ # set up x (date) and y (HPA) axes scale_x_date(limits=xlim,breaks=xlim,date_labels="%y")+ scale_y_continuous(label=percent,limits=c(-0.35,0.35), breaks=seq(-0.3,.3,.3))+ # labels, title, caption labs(x="",y="", title="House Price Appreciation", subtitle="12-month percent change in March", caption="@lenkiefer Source: Freddie Mac House Price Index")+ # adjust theme theme(plot.caption=element_text(hjust=0), # need to shrink axis text axis.text.x=element_text(size=7), plot.subtitle=element_text(face="italic"), legend.position="top")
Pretty neat. I think I’ll be trying this more in the future.
It also works pretty great for an animation. Here’s an animated version of our plot: