Turning Up the Heat
PBSmapping allows us to see which in polygons/tracts our foreclosures were plotted. Using this data we can represent the intensity of foreclosure events as a heatmap.
> addressPolys<-findPolys(addressEvents,myShapeFile)> addressPolysEID PID SID Bdry 1 106 1 1 0 2 218 3 1 0 3 118 4 1 0 4 40 13 1 0 5 155 13 1 0 6 294 14 1 0 7 342 14 1 0 8 361 14 1 0 9 332 17 1 0 10 343 17 1 0 11 369 17 1 0 (snip)
Factors When You Need Them
Each EID (event id aka foreclosure) is associated with a PID
(polygon id aka tract). To plot our heatmap we need to count instances
of PIDs in addressPolys for each
tract on the map.
> length(levels(as.factor(myShapeFile$PID)))[1] 381> length(levels(as.factor(addressPolys$PID)))[1] 196
We can see there are 381 census tracts in Philadelphia but only 196 have foreclosure events. For the purpose of coloring our polygons we need to insure remainder are explicitly set to 0 foreclosures.
The table function in R can be used to make this sort of
contingency table. We need a variable, myTrtFC, to hold the number forclosures in
each tract/PID:
> myTrtFC<-table(factor(addressPolys$PID,levels=levels(as.factor(myShapeFile$PID))))> myTrtFC1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 0 1 1 0 0 0 0 0 0 0 0 2 3 0 0 3 1 1 1 (snip)
(To enforce our new levels we must use a constructor (factor) instead of a variable conversion
(as.factor))
Filling with Color Gradients
R has some decent built-in color gradient functions (?rainbow to learn more)—we ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access