# The housing bubble by city

March 17, 2011
By

(This article was first published on Decision Science News » R, and kindly contributed to R-bloggers)

DIFFERENT CITIES TELL DIFFERENT STORIES

Inspired by a tutorial called 5 Minute Analysis in R: Case-Shiller Indices, we decided to redo the analysis with 1) More up to date data and 2) Hadley Wickham’s super ggplot2 graphics package.

The above plot shows the general trend in home prices, relative to January 2000, in various cities, but is otherwise a mess.

Much like Olympic sports coverage, we next focus in on a few exceptional stories. It is much easier to read.

Miami sailed high and fell far. Detroit rose modestly and but dropped more than it went up.  Dallas held steady. DC is enjoying a bit of renewed growth, but are it and New York yet to fall?

The graphs were surprisingly easy to create in R, and the full code is below. If you’d like to do your own analysis, the Standard & Poor’s Web site has data that you can download yourself. We chose the “seasonally adjusted” data set.

New to us in the code below was the “droplevels‘ command which was added to R’s version 12. It dispenses with unused factor levels that you sometimes end up with when subsetting data. Extra levels are often harmless, but in ggplot2 they show up in the legend.  Without droplevels, the second graph would have the legend of the first graph.

``` library(ggplot2) ## Read in data, available from: #www.standardandpoors.com/indices/sp-case-shiller-home-price-indices/en/us/?indexId=spusa-cashpidff--p-us---- dat=read.csv("CSHomePrice_History.csv") mdf=melt(dat,id.vars="YEAR") mdf\$Date=as.Date(paste("01-",mdf\$YEAR,sep=""),"%d-%b-%y") names(mdf)=c("MonthYear","City","IndexValue","Date") ggplot(data=mdf,aes(x=Date,y=IndexValue)) + geom_line(aes(color=City),size=1.25) + scale_x_date("Year", minor="years") + scale_y_continuous("Case Schiller Index") sm=subset(mdf,City %in% c('NY.New.York','FL.Miami','CA.Los Angeles','MI.Detroit', 'TX.Dallas','IL.Chicago','DC.Washington')) sm\$City=droplevels(sm\$City) ggplot(data=sm,aes(x=Date,y=IndexValue)) + geom_line(aes(color=City),size=1.5) + scale_x_date("Year", minor="years") + scale_y_continuous("Case Schiller Index") ```

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

Tags: , , , , , , ,