Plot the FHFA House Price Index With R

FHFA House Price Index


R is a great tool for creating data visualizations like the one above.  The learning curve is a little bit steeper than a low code alternative such as Microsoft Excel.  However, R makes it easy to transform certain types of data such as a csv file with data points represented as column headers rather than row data.  For example, the data that is supplied by the Fair Housing Finance Administration is organized by month and regional market, meaning that there is a column for each regional market in the data.  The reshape library makes it easy to transform this data into three column csv file where the columns represent the month, index value, and regional market.

View on GitHub

In the example above, we start by calling the ggplot2 and reshape libraries.  Then we read the House Price Index csv data and add that data to the class ‘d’.  Afterwards we declare a new class called ‘month’ and format the month column from our csv as m/d/y.

Next we declare a class called ‘df’ and append a data frame to df which includes the data from our csv organized by column header.  Note that to make things simple, I altered the column headers in the csv to make it easier to add the data to the data frame.  After our data frame has been added to df, we use the melt method from the reshape library to transform the grid style data structure into a linear data structure that stores the month as time, market name as series, and index value as value.  Imagine taking an 10 column horizontal spreadsheet in Excel and turning it into a 3 column spreadsheet by copying the values from each column header and pasting them as new rows.  After pasting each row, you also add the column header to the third row.

After this we create a class that contains labels and a class that contains the colors for each label.  Libraries like ggplot will handle labels and colors automatically.  However, I wanted to have descriptive labels for the values represented in the plot.

Finally, we plot the data by assigning the ggplot function to ‘c’.  We also add in a title, x and y labels, and our label names and colors for the values shown in the plot.  In addition, we specify some additional theme parameters for our plot.

The source data can for this plot can be acquired from the FHFA.