Example thematic map in R

The first step is to call in the two libraries we'll need and get the county-level data and map files (these come from IPUMS):

  1. Minnesota Population Center. North Atlantic Population Project: Complete Count Microdata. Version 2.0 [Machine-readable database]. Minneapolis: Minnesota Population Center, 2008. https://www.nappdata.org/napp/
  2. Steven Ruggles et al. Integrated Public Use Microdata Series: Version 5.0 [Machine-readable database]. Minneapolis: University of Minnesota, 2010.https://www.nappdata.org/napp/
  3. Minnesota Population Center. National Historical Geographic Information System: Version 2.0. Minneapolis, MN: University of Minnesota 2011. http://www.nhgis.org
library(classInt)
## Loading required package: class
## Loading required package: e1071
library(sp)
load(url("http://dl.dropbox.com/u/9256203/US1880.Rdata"), .GlobalEnv)
ls()
## [1] "cnty" "orig" "st"

The data file contains the number of instances a person in the row county had a parent born in the column location. The data and the maps are from 1880. Only persons 40 years old and older are considered; which gives some sense of how population migrated between 1820-1880. We convert the raw count into percentages:

q <- as.matrix(orig)
q <- q/rowSums(q)
colnames(q)
##  [1] "Alabama"                            
##  [2] "Arkansas"                           
##  [3] "California"                         
##  [4] "Connecticut"                        
##  [5] "Delaware"                           
##  [6] "District.of.Columbia"               
##  [7] "Florida"                            
##  [8] "Georgia"                            
##  [9] "Illinois"                           
## [10] "Indiana"                            
## [11] "Iowa"                               
## [12] "Kentucky"                           
## [13] "Louisiana"                          
## [14] "Maine"                              
## [15] "Maryland"                           
## [16] "Massachusetts"                      
## [17] "Michigan"                           
## [18] "Minnesota"                          
## [19] "Mississippi"                        
## [20] "Missouri"                           
## [21] "New.Hampshire"                      
## [22] "New.Jersey"                         
## [23] "New.Mexico"                         
## [24] "New.York"                           
## [25] "North.Carolina"                     
## [26] "Ohio"                               
## [27] "Pennsylvania"                       
## [28] "Rhode.Island"                       
## [29] "South.Carolina"                     
## [30] "Tennessee"                          
## [31] "Texas"                              
## [32] "Vermont"                            
## [33] "Virginia"                           
## [34] "Washington"                         
## [35] "West.Virginia"                      
## [36] "Wisconsin"                          
## [37] "United.States..NS"                  
## [38] "Africa"                             
## [39] "Africa..NS"                         
## [40] "West.Indies"                        
## [41] "Bahamas"                            
## [42] "Cuba"                               
## [43] "Mexico"                             
## [44] "Chile"                              
## [45] "Canada"                             
## [46] "New.Brunswick..Nova.Scotia..and.PEI"
## [47] "Ontario.and.Upper.Canada"           
## [48] "Quebec"                             
## [49] "Newfoundland.and.Labrador"          
## [50] "China"                              
## [51] "Czech.Republic"                     
## [52] "Hungary"                            
## [53] "Poland"                             
## [54] "Russia"                             
## [55] "Denmark"                            
## [56] "Finland"                            
## [57] "Ireland"                            
## [58] "Norway"                             
## [59] "Sweden"                             
## [60] "England"                            
## [61] "Isle.of.Man"                        
## [62] "Scotland"                           
## [63] "Wales"                              
## [64] "Italy"                              
## [65] "Portugal"                           
## [66] "Azores"                             
## [67] "Spain"                              
## [68] "Austria"                            
## [69] "Belgium"                            
## [70] "France"                             
## [71] "Alsace"                             
## [72] "Lorraine"                           
## [73] "Germany"                            
## [74] "Mecklenburg.Schwerin"               
## [75] "Hamburg"                            
## [76] "Bremen"                             
## [77] "Other.Germany"                      
## [78] "Luxembourg"                         
## [79] "Netherlands"                        
## [80] "Switzerland"                        
## [81] "Europe..NS"

Next we arbitrarily pick the first column to plot. The first column is “Alabama” so our map will show, for the residents of each county, the percent of their parents born in Alabama. We use the Fisher algorithm to define breakpoints for the color ranges.

ii <- colnames(q)[1]
ki <- match(cnty@data$fips, rownames(q))
brks <- classIntervals(q[, ii], 6, style = "fisher")$brks
farv <- colorRampPalette(c("yellow", "red"))(length(brks) - 1)
nuk <- findInterval(q[ki, ii], brks, all.inside = TRUE)

We use a histogram to take a look at the distribution of values and the breakpoints:

hist(q[, ii], breaks = 30, col = "cyan", border = "cyan", xlim = range(q[, ii]), 
    xlab = paste("Percent of parents from", ii), main = NA)
abline(v = brks, col = "red", lty = 2)

plot of chunk unnamed-chunk-4

Finally, we plot the county map. To make it easier to read, we remove the county outlines, and we overlay with state outlines.

plot(cnty, col = farv[nuk], lty = 0)
plot(st, add = TRUE, lty = 1, lwd = 0.2)
mtext(ii)
ja <- paste(round(brks[-length(brks)], 4), " - ", round(brks[-1], 4), "; N=", 
    table(nuk), sep = "")
legend("bottomleft", legend = ja, fill = farv[1:max(nuk, na.rm = TRUE)], cex = 0.7)

plot of chunk unnamed-chunk-5