For the 48-state plus District of Columbia case and saved functions and objects used here, see the walkthrough installment. For the 50-state version plus District of Columbia with Alaska and Hawaii scaled and relocated, see the inset installment.
x = c("ggplot2", "rgdal", "maptools", "mapproj", "rgeos")
lapply(x, library, character.only = 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"
)
}
#save("remove.territories", file = "helpers/remove.territories")
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, scale=max(apply(bbox(dc), 1, diff)) / .999999) # can't make smaller than 1.0
#
# Scaling of this object is not yet implemented
#
####################################################
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)
us50 <- fortify(us_aea, region="STUSPS")
us50 = remove.territories(us50)
#save('helpers/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
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] formatR_1.2 evaluate_0.7 rmarkdown_0.7 labeling_0.3
## [21] stringi_0.5-5 scales_0.2.5 foreign_0.8-65 proto_0.3-10
Enlarging the District of Columbia and adding state names
I am unable to answer questions relating to the use of this code under Windows.
See sources in the walkthrough installment.
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.