Skip to Content
Data Mashups in R
book

Data Mashups in R

by Xiao-Yi Li, Jeremy Leipzig
June 2009
Beginner to intermediate
29 pages
42m
English
O'Reilly Media, Inc.
Content preview from Data Mashups in R

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)
  > addressPolys
       EID 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))))
  > myTrtFC
  1  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

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Data Mashups in R

Data Mashups in R

Jeremy Leipzig, Xiao-Yi Li
Graphing Data with R

Graphing Data with R

John Jay Hilfiger

Publisher Resources

ISBN: 9780596805302Errata