Prepare as before

setwd("~/Desktop/gitmo/playpen/r")
x = c("ggplot2", "rgdal", "maptools", "mapproj", "rgeos")
lapply(x, library, character.only = TRUE)
## Loading required package: sp
## rgdal: version: 1.0-4, (SVN revision 548)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 1.11.2, released 2015/02/10
##  Path to GDAL shared files: /Users/rc/Library/R/3.2/library/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
##  Path to PROJ.4 shared files: /Users/rc/Library/R/3.2/library/rgdal/proj
##  Linking to sp version: 1.1-1 
## Checking rgeos availability: TRUE
## Loading required package: maps
## rgeos version: 0.3-11, (SVN revision 479)
##  GEOS runtime version: 3.4.2-CAPI-1.8.2 r3921 
##  Linking to sp version: 1.1-0 
##  Polygon checking: TRUE
## [[1]]
## [1] "ggplot2"   "stats"     "graphics"  "grDevices" "utils"     "datasets" 
## [7] "methods"   "base"     
## 
## [[2]]
##  [1] "rgdal"     "sp"        "ggplot2"   "stats"     "graphics" 
##  [6] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[3]]
##  [1] "maptools"  "rgdal"     "sp"        "ggplot2"   "stats"    
##  [6] "graphics"  "grDevices" "utils"     "datasets"  "methods"  
## [11] "base"     
## 
## [[4]]
##  [1] "mapproj"   "maps"      "maptools"  "rgdal"     "sp"       
##  [6] "ggplot2"   "stats"     "graphics"  "grDevices" "utils"    
## [11] "datasets"  "methods"   "base"     
## 
## [[5]]
##  [1] "rgeos"     "mapproj"   "maps"      "maptools"  "rgdal"    
##  [6] "sp"        "ggplot2"   "stats"     "graphics"  "grDevices"
## [11] "utils"     "datasets"  "methods"   "base"
remove.territories = function(.df) {
    subset(.df, 
        .df$id != "AS" &
        .df$id != "MP" &
        .df$id != "GU" & 
        .df$id != "PR" &
        .df$id != "VI" 
    )
}
plain_theme = theme(axis.text=element_blank()) + 
    theme(panel.background = element_blank(), 
        panel.grid = element_blank(), 
        axis.ticks = element_blank())
no_ylab = ylab("") 
no_xlab = xlab("")
setwd("~/Desktop/gitmo/playpen/r")
load("data/cb5")
us = cb5
us_aea = spTransform(us, CRS("+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"))
us_aea@data$id = rownames(us_aea@data)
alaska = us_aea[us_aea$STATEFP=="02",]
alaska = elide(alaska, rotate=-50)
alaska = elide(alaska, scale=max(apply(bbox(alaska), 1, diff)) / 2.3)
alaska = elide(alaska, shift=c(-2100000, -2500000))
proj4string(alaska) = proj4string(us_aea)
hawaii = us_aea[us_aea$STATEFP=="15",]
hawaii = elide(hawaii, rotate=-35)
hawaii = elide(hawaii, shift=c(5400000, -1400000))
proj4string(hawaii) = proj4string(us_aea)
us_aea = us_aea[!us_aea$STATEFP %in% c("02", "15"),]
us_aea = rbind(us_aea, alaska, hawaii)
rhode_island = us_aea[us_aea$STATEFP=="44",]
rhode_island = elide(rhode_island, shift=c(0,-200000))
proj4string(rhode_island) = proj4string(us_aea)
delaware = us_aea[us_aea$STATEFP=="10",]
delaware = elide(delaware, shift=c(200000,0))
proj4string(delaware) = proj4string(us_aea)
dc = us_aea[us_aea$STATEFP=="11",]
dc = elide(dc, shift=c(250000,-125000))
proj4string(dc) = proj4string(us_aea)
us_aea = us_aea[!us_aea$STATEFP %in% c("44", "10", "11"),]
us_aea = rbind(us_aea, delaware, rhode_island, dc)

Obtain the coordinates of states’ centers

centroids = data.frame(us_aea$STUSPS, coordinates(us_aea))
names(centroids) = c("id", "clong", "clat")

Remove overseas territories

centroids = remove.territories(centroids)

Continue as before

us50 <- fortify(us_aea, region="STUSPS")
us50 = remove.territories(us50)
p = ggplot(data=us50) + 
    geom_map(map=us50, aes(x=long, y=lat, map_id=id, group=group), ,fill="white", color="black", size=0.3) + 
    no_ylab + 
    no_xlab + 
    plain_theme
p + geom_text(data=centroids, aes(clong, clat, label = id), size=2)

Adjust positions of states by trial and error

rownames(centroids) = centroids$id
centroids['LA',]$clong = 729000
centroids['FL',]$clong = 1800000
centroids['DE',]$clong = 2400000
centroids['RI',]$clong = 2400000
centroids['MD',]$clong = 1950000
centroids['MD',]$clat = -360000
centroids['MA',]$clat = 100000
centroids['NJ',]$clong = 2130000
centroids['NJ',]$clat = -255000

Replot

c = p + geom_text(data=centroids, aes(clong, clat, label = id), size=2)
c

sessionInfo()
## R version 3.2.1 (2015-06-18)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X 10.10.4 (Yosemite)
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] rgeos_0.3-11    mapproj_1.2-3   maps_2.3-10     maptools_0.8-36
## [5] rgdal_1.0-4     sp_1.1-1        ggplot2_1.0.1  
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.11.6      knitr_1.10.5     magrittr_1.5     MASS_7.3-42     
##  [5] munsell_0.4.2    colorspace_1.2-6 lattice_0.20-33  stringr_1.0.0   
##  [9] plyr_1.8.3       tools_3.2.1      grid_3.2.1       gtable_0.1.2    
## [13] htmltools_0.2.6  yaml_2.1.13      digest_0.6.8     reshape2_1.4.1  
## [17] evaluate_0.7     rmarkdown_0.7    labeling_0.3     stringi_0.5-5   
## [21] scales_0.2.5     foreign_0.8-65   proto_0.3-10

Next up

Color

Disclaimer

I am unable to answer questions relating to the use of this code under Windows.

Credits

Algorithms

Scale and shift

Data

See sources in the walkthrough installment.

R Packages

  • Roger Bivand and Nicholas Lewin-Koh (2015). maptools: Tools for Reading and Handling Spatial Objects. R package version 0.8-36. http://CRAN.R-project.org/package=maptools
  • Roger Bivand and Colin Rundel (2015). rgeos: Interface to Geometry Engine - Open Source (GEOS). R package version 0.3-11. http://CRAN.R-project.org/package=rgeos
  • Roger Bivand, Tim Keitt and Barry Rowlingson (2015). rgdal: Bindings for the Geospatial Data Abstraction Library. R package version 1.0-4 rgdal
  • Doug McIlroy. Packaged for R by Ray Brownrigg, Thomas P Minka and transition to Plan 9 codebase by Roger Bivand. (2015). mapproj: Map Projections. R package version 1.2-3. http://CRAN.R-project.org/package=mappro
  • Erich Neuwirth (2014). RColorBrewer: ColorBrewer Palettes. R package version 1.1-2. RColorBrewer
  • R Core Team (2015). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. [grid]
  • H. Wickham. ggplot2: elegant graphics for data analysis. Springer New York, 2009. ggplot2

License

Copyright (c) 2015, Richard Careaga

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.