Heatmapping Washington, DC Rental Price Changes using OpenStreetMaps

Percentage change of median price per square foot from July 2012 to July 2013:


Percentage change of median price from July 2012 to July 2013:


Last November I made a  choropleth of median rental prices in the San Francisco Bay Area using data from my company, Kwelia.  I have wanted to figure out how to plot a similar heat map over an actual map tile, so I once again took some Kwelia data to plot both percentage change of median price and percentage change of price per sqft from July 2012 to this past month (yep, we have realtime data.)

How it’s made:

While the google maps API through R is very good, I decided to use the OpenStreetMap package because I am a complete supporter of open source projects (which is why I love R).

First, you have to download the shape files, in this case I used census tracts from the Us Census tigerlines.   Then you need to read to read it into R using the maptools package like this and merge your data to the shape file:

zip=readShapeSpatial( "tl_2010_11001_tract10.shp" )

##merge data with shape file
 zip$geo_id=paste("1400000US", zip$GEOID10, sep="")
 zip$ppsqftchange <- dc$changeppsqft[match(zip$geo_id,dc$geo_id , nomatch = NA )]
 zip$pricechange <- dc$changeprice[match(zip$geo_id,dc$geo_id , nomatch = NA )]

Then you pull down the map tile from the OpenStreetMaps. I used the max and mins from the actual shape file to get the four corners of the tile to pull down the two above maps (“waze” and “stamen-toner”)

map = openproj(openmap(c(lat= max(as.numeric(as.character(zip$INTPTLAT10))),   lon= min(as.numeric(as.character(zip$INTPTLON10)))),
 c(lat= min(as.numeric(as.character(zip$INTPTLAT10))),   lon= max(as.numeric(as.character(zip$INTPTLON10)))),type="stamen-toner"))

Finally, plotting the project. The one thing different from plotting the choropleths from the Bay area is adjusting the transparency of the colors. To adjust the transparency you need to add two extra numbers (00 is fully transparent and 99 is solid) to the end of the colors as you will see in the  annotations.

##grab nine colors
 colors=brewer.pal(9, "YlOrRd")
 ##make nine breaks in the value
 brks=classIntervals(zip1$pricechange, n=9, style="quantile")$brks
 ##apply the breaks to the colors
 cols <- colors[findInterval(zip1$pricechange, brks, all.inside=TRUE)]
 ##changing the color to an alpha (transparency) of 60%
 cols <- paste0( cols, "60")
 is.na(cols) <- grepl("NA", cols)
 ##changing the color to an alpha (transparency) of 60%
 colors <- paste0( colors, "60")

 ##plot the open street map
 ##add the shape file with the percentage changes to the osm 
 plot( zip , col = cols , axes=F , add=TRUE)
 ##adding the ledgend with breaks at 75%(cex) and without border(bty)
 legend('right', legend= leglabs( round(brks , 1 ) ) , fill = colors , bty="n", cex=.75)

9 thoughts on “Heatmapping Washington, DC Rental Price Changes using OpenStreetMaps

  1. I have an interest in building a similar map, yet had some trouble replicating what you did. It seems some parts of the R code are left out (perhaps intentially). May I see a few rows of the dc dataframe?

      3 11 001 002302 11001002302 23.02 Census Tract 23.02 G5020
      4 11 001 002400 11001002400 24 Census Tract 24 G5020
      3 S 2010128 5298 +38.9341104 -077.0091127 1400000US11001002302
      4 S 556361 0 +38.9417826 -077.0224057 1400000US11001002400
      ppsqftchange pricechange
      3 7.414105 -15.384615
      4 -8.410196 -5.823129

    2. geo_id ppsqft2012 price2012 ppsqft2013 price2013 changeppsqft
      1 1400000US11001000100 2.871212 3200 2.941176 2400.0 2.436753
      3 1400000US11001000202 2.875641 3500 3.123036 3199.5 8.603136
      1 -25.000000
      3 -8.585714

  2. I can’t figure out where the DC data frame is coming from? Is there a way you can post the entire R script?

  3. These are really nice graphs, I appreciate the effort you put into them. Small suggestion: could you use a color scheme that more clearly differentiates the positive changes from the negative changes? The color gradation of the current choropleth leaves me squinting to see which areas are moving up versus down. All in all, though, great job!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s