My company, Kwelia, is sitting on mountains of data, so I decided to try my hand at mapping. I have played around with JGR but it’s just too buggy, at least on my mac, so I went looking for other alternatives and found a good write up here. I decided on mapping prices per sqft for apartment rentals by zip codes in the bay area because we are launching our services there shortly.
First transforming the data was easy using ddply in the plyr package after I had queried all the right zip codes into a data frame from the database.
w=ddply(sanfran, .(zip), summarise, pricepersqft=mean(price/sqft))
Then it’s a matter of loading the shape file after downloading it from here.
ddply will sort the zip codes, so I transformed the zip spatial data into a regular data frame, merged it with “w”, added
pricepersqft to an ordered “zip” data frame, and finally subset out zips without data.
##transform to regular data frame
##merge with the ddply data
r=merge(a, w, all=TRUE)
##order zips in the spatial poly data
##merge price per sqft with spatial data
##subset out zips with missing data
Finally comes the plotting, which ,luckily, is almost exactly the same as the example that it I found above.
#select color palette and the number colors(prices per sqft) to represent on the map colors
#set breaks for the 9 colors
brks=classIntervals(zip$INCOME, n=9, style="quantile")
#plot the map
plot(zip, col=colors[findInterval(zip$INCOME, brks,all.inside=TRUE)], axes=F)
#add a title
title(paste ("SF Bay Area Price Per SQFT for rentals by Zip"))
#add a legend
legend(x=6298809, y=2350000, legend=leglabs(round(brks)), fill=colors, bty="n",x.intersp = .5, y.intersp = .5)
Here are the actual current averages of rental prices by zip: