Mapping Current Average Price Per Sqft for Rentals by Zip in San Fran

[This article was first published on NERD PROJECT » R project posts, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

library("plyr") w=ddply(sanfran, .(zip), summarise, pricepersqft=mean(price/sqft))

Then it’s a matter of loading the shape file after downloading it from here.

library(maptools) library(RColorBrewer) library(classInt)


The 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 d=zip[order(zip$ZIP),] ##merge price per sqft with spatial data d@data$pricepersqft=r$pricepersqft ##subset out zips with missing data yy=d[!$pricepersqft),]

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 colors=brewer.pal(9, "YlOrRd") #set breaks for the 9 colors brks=classIntervals(zip$INCOME, n=9, style="quantile") brks=brks$brks #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:






To leave a comment for the author, please follow the link and comment on their blog: NERD PROJECT » R project posts. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)