To show differences between broad groups, consider highlighting with breakouts

setwd("~/Desktop/gitmo/playpen/r") # your directory here

download.file("https://gist.githubusercontent.com/technocrat/7719f51d0ae0e42edeb2/raw/f4c3157f17b3822416637a987d57bba678cb7839/popBase.R", "popBase.R", method = "libcurl")
source("R/popBase.R")
## 
## Attaching package: 'dplyr'
## 
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## 
## 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 
## 
## Loading required package: bitops
sources <- "Sources: Police caused deaths Data as reported by http://bit.ly/1LJwlF3 from another\n source, accessed 2015-07-26. Disease death rates: Centers for Disease Control \nand Protection: Detailed Tables for the National Vital Statistics Report\n(NVSR) “Deaths: Final Data for 2013. (national rates) http://1.usa.gov/1gjDcbd\naccessed 2015-07-26. 2012 election results: http://1.usa.gov/1go1Jfj\naccessed 2015-07-29. Prepared by @technocrat 2015-07-29" 

deaths <- read.csv('csv/shootings.csv', header = FALSE)
names(deaths) <- c("id", "deaths")
popdeath <- merge(pop, deaths, by = 'id')
popdeath$ratio <- (popdeath$deaths/popdeath$pop)*100000
popdeath$ratio <- factor(popdeath$ratio)
head(popdeath)
##   id fips      pop      state           pop.equal        pop.quantile
## 1 AK    2   736732     Alaska [5.84e+05,6.95e+06] [5.84e+05,1.15e+06]
## 2 AL    1  4849377    Alabama [5.84e+05,6.95e+06] (4.41e+06,6.23e+06]
## 3 AR    5  2966369   Arkansas [5.84e+05,6.95e+06] (2.88e+06,4.41e+06]
## 4 AZ    4  6731484    Arizona [5.84e+05,6.95e+06] (6.23e+06,9.93e+06]
## 5 CA    6 38802500 California (3.24e+07,3.88e+07] (9.93e+06,3.88e+07]
## 6 CO    8  5355866   Colorado [5.84e+05,6.95e+06] (4.41e+06,6.23e+06]
##               pop.std        pop.natural deaths              ratio
## 1 [5.84e+05,7.71e+06] [5.84e+05,3.6e+06]    0.1 0.0135734568336926
## 2 [5.84e+05,7.71e+06] (3.6e+06,7.06e+06]   72.0   1.48472680098908
## 3 [5.84e+05,7.71e+06] [5.84e+05,3.6e+06]   41.0   1.38216115392252
## 4 [5.84e+05,7.71e+06] (3.6e+06,7.06e+06]  176.0   2.61457948945582
## 5 (3.62e+07,3.88e+07] (2.7e+07,3.88e+07] 1233.0   3.17763030732556
## 6 [5.84e+05,7.71e+06] (3.6e+06,7.06e+06]  104.0   1.94179615397398
load('helpers/redblue')
redblue
##    id president
## 1  AL         R
## 2  AK         R
## 3  AZ         R
## 4  AR         R
## 5  CA         D
## 6  CO         D
## 7  CT         D
## 8  DE         E
## 9  DC         D
## 10 FL         D
## 11 GA         R
## 12 HI         D
## 13 ID         R
## 14 IL         D
## 15 IN         R
## 16 IA         D
## 17 KS         R
## 18 KY         R
## 19 LA         R
## 20 ME         D
## 21 MD         D
## 22 MA         D
## 23 MI         D
## 24 MN         D
## 25 MS         R
## 26 MO         R
## 27 MT         R
## 28 NE         R
## 29 NV         D
## 30 NH         D
## 31 NJ         D
## 32 NM         D
## 33 NY         D
## 34 NC         R
## 35 ND         R
## 36 OH         D
## 37 OK         R
## 38 OR         D
## 39 PA         D
## 40 RI         D
## 41 SC         R
## 42 SD         R
## 43 TN         R
## 44 TX         R
## 45 UT         R
## 46 VT         D
## 47 VA         D
## 48 WA         D
## 49 WV         R
## 50 WI         D
## 51 WY         R
popdeath <- merge(popdeath, redblue, by = 'id')
#not run in RStudio
#popdeath <- intervals(popdeath,ratio)
head(popdeath)
##   id fips      pop      state           pop.equal        pop.quantile
## 1 AK    2   736732     Alaska [5.84e+05,6.95e+06] [5.84e+05,1.15e+06]
## 2 AL    1  4849377    Alabama [5.84e+05,6.95e+06] (4.41e+06,6.23e+06]
## 3 AR    5  2966369   Arkansas [5.84e+05,6.95e+06] (2.88e+06,4.41e+06]
## 4 AZ    4  6731484    Arizona [5.84e+05,6.95e+06] (6.23e+06,9.93e+06]
## 5 CA    6 38802500 California (3.24e+07,3.88e+07] (9.93e+06,3.88e+07]
## 6 CO    8  5355866   Colorado [5.84e+05,6.95e+06] (4.41e+06,6.23e+06]
##               pop.std        pop.natural deaths              ratio
## 1 [5.84e+05,7.71e+06] [5.84e+05,3.6e+06]    0.1 0.0135734568336926
## 2 [5.84e+05,7.71e+06] (3.6e+06,7.06e+06]   72.0   1.48472680098908
## 3 [5.84e+05,7.71e+06] [5.84e+05,3.6e+06]   41.0   1.38216115392252
## 4 [5.84e+05,7.71e+06] (3.6e+06,7.06e+06]  176.0   2.61457948945582
## 5 (3.62e+07,3.88e+07] (2.7e+07,3.88e+07] 1233.0   3.17763030732556
## 6 [5.84e+05,7.71e+06] (3.6e+06,7.06e+06]  104.0   1.94179615397398
##   president
## 1         R
## 2         R
## 3         R
## 4         R
## 5         D
## 6         D
redbluemap <- merge(us50, popdeath, by = 'id')
redstates <- filter(redbluemap, president == 'R')
bluestates <- filter(redbluemap, president == 'D')
p = ggplot(data= redbluemap) + 
    geom_map(map=redbluemap, aes(x=long, y=lat, map_id=id, group=group), fill = "light grey", color="dark grey", size=0.3) + 
    no_ylab + 
    no_xlab + 
    plain_theme 
    
makewhite = centroids[c('MT', 'NV', 'FL', 'ME', 'OR', 'NM'),]
l = geom_text(data=centroids, aes(clong, clat, label = id), color = "black", size=2)
w = geom_text(data=makewhite, aes(clong, clat, label = id), color = "white", size=2)

combined_title <- "Estimated Police Caused Deaths (unstated period)\nin States and District of Columbia"

combined <- p + geom_polygon(data = redbluemap, aes(x=long, y=lat, group=group, fill = ratio), color = "dark grey", size = 0.3)

combined <- combined + scale_fill_brewer(palette = "Reds", name = "Police Caused Deaths\nper 100,000 population\n(comparable to)", labels=c("Under 1.5(influenza)", "1.5(atherosclerosis)", "1.5-2.2(atherosclerosis)","2.2-3.5(aortic aneurysm)","3.5-4.8(asthma and emphysema)","4.8-8.3(liver disease)")) + ggtitle(combined_title) + l + w + annotate("text", x = -120000, y = -2350000, label = sources, size = 3, hjust = 0, family = "Times", colour = "black")

# not run in RStudio
#combined + l + w

r_title <- "Estimated Police Caused Deaths (unstated period)\nin States voting for Romney in 2012"

r <- p + geom_polygon(data = redstates, aes(x=long, y=lat, group=group, fill = 
ratio), color = "dark grey", size = 0.3)

r <- r + scale_fill_brewer(palette = "Reds", name = "Police Caused Deaths\nper 100,000 population\n(comparable to)", labels=c("Under 1.5(influenza)","1.5-2.2(atherosclerosis)","2.2-3.5(aortic aneurysm)","3.5-4.8(asthma and emphysema)","4.8-8.3(liver disease)")) + ggtitle(r_title) + l + w + annotate("text", x = -120000, y = -2350000, label = sources, size = 3, hjust = 0, family = "Times", colour = "black")


makewhite = centroids[c('MT'),]
w = geom_text(data=makewhite, aes(clong, clat, label = id), color = "white", size=2)

# Not run in RStudio
# r + l + w

d_title <- "Estimated Police Caused Deaths (unstated period)\nin States and District of Columbia voting for Obama"

d <- p + geom_polygon(data = bluestates, aes(x=long, y=lat, group=group, fill = ratio), color = "dark grey", size = 0.3)

d  <- d + scale_fill_brewer(palette = "Reds", name = "Police Caused Deaths\nper 100,000 population\n(comparable to)", labels=c("Under 1.5(influenza)", "1.5(atherosclerosis)", "1.5-2.2(atherosclerosis)","2.2-3.5(aortic aneurysm)","3.5-4.8(asthma and emphysema)","4.8-8.3(liver disease)")) + ggtitle(d_title) + annotate("text", x = -120000, y = -2350000, label = sources, size = 3, hjust = 0, family = "Times", colour = "black")

makewhite = centroids[c('NV', 'FL', 'ME', 'OR', 'NM'),]
w = geom_text(data=makewhite, aes(clong, clat, label = id), color = "white", size=2)

# Not run in RStudio
# d + l + w
table_of_data <- data.frame(popdeath$id,popdeath$pop,popdeath$deaths, popdeath$ratio)
names(table_of_data) <- c("State", "Population", "Deaths", "Per 100,000")
table_of_data
##    State Population Deaths         Per 100,000
## 1     AK     736732    0.1  0.0135734568336926
## 2     AL    4849377   72.0    1.48472680098908
## 3     AR    2966369   41.0    1.38216115392252
## 4     AZ    6731484  176.0    2.61457948945582
## 5     CA   38802500 1233.0    3.17763030732556
## 6     CO    5355866  104.0    1.94179615397398
## 7     CT    3596677    0.1 0.00278034418992865
## 8     DC     658893    0.1  0.0151769710711754
## 9     DE     935614    0.1   0.010688168411332
## 10    FL   19893297  958.0    4.81569244152943
## 11    GA   10097343  204.0     2.0203334679232
## 12    HI    1419561    0.1 0.00704443134180215
## 13    IA    3107126   26.0   0.836786149000716
## 14    ID    1634464   46.0    2.81437829159896
## 15    IL   12880580  177.0    1.37416172253113
## 16    IN    6596855   58.0   0.879206834165674
## 17    KS    2904021   47.0    1.61844559663997
## 18    KY    4413457   62.0    1.40479447290412
## 19    LA    4649676   73.0    1.57000186679674
## 20    MA    6745408  101.0    1.49731491408674
## 21    MD    5976407  130.0    2.17521999422061
## 22    ME    1330089   55.0    4.13506163873245
## 23    MI    9909877   87.0   0.877912006375054
## 24    MN    5457173   95.0    1.74082808076636
## 25    MO    6063589  110.0    1.81410712368533
## 26    MS    2994079   44.0    1.46956710227085
## 27    MT    1023579   40.0    3.90785664809458
## 28    NC    9943964  221.0    2.22245374178748
## 29    ND     739482    5.0   0.676148979961649
## 30    NE    1881503   27.0    1.43502295770987
## 31    NH    1326813   23.0     1.7334771365671
## 32    NJ    8938175   83.0   0.928601196552988
## 33    NM    2085572   86.0     4.1235689777193
## 34    NV    2839099  236.0    8.31249632365761
## 35    NY   19746227  383.0    1.93961104569496
## 36    OH   11594163  158.0    1.36275468957958
## 37    OK    3878051   82.0    2.11446419863999
## 38    OR    3970239  189.0     4.7604187052719
## 39    PA   12787209  142.0    1.11048470389434
## 40    RI    1055173    0.1 0.00947711891794047
## 41    SC    4832482   76.0    1.57269080360775
## 42    SD     853175   21.0     2.4613942040027
## 43    TN    6549352   94.0    1.43525649560445
## 44    TX   26956958  361.0    1.33917187540226
## 45    UT    2942902  104.0    3.53392671587433
## 46    VA    8326289   79.0   0.948802041341587
## 47    VT     626562   17.0    2.71321912276838
## 48    WA    7061530  212.0    3.00218224662361
## 49    WI    5757564   56.0   0.972633565167491
## 50    WV    1850326   26.0    1.40515779381579
## 51    WY     584153   12.0    2.05425633352906
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] RCurl_1.95-4.7  bitops_1.0-6    rgeos_0.3-11    mapproj_1.2-3  
##  [5] maps_2.3-10     maptools_0.8-36 rgdal_1.0-4     sp_1.1-1       
##  [9] dplyr_0.4.2     classInt_0.1-22 ggplot2_1.0.1  
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.0      knitr_1.10.5     magrittr_1.5     MASS_7.3-42     
##  [5] munsell_0.4.2    lattice_0.20-33  colorspace_1.2-6 R6_2.1.0        
##  [9] stringr_1.0.0    plyr_1.8.3       tools_3.2.1      parallel_3.2.1  
## [13] grid_3.2.1       gtable_0.1.2     e1071_1.6-4      DBI_0.3.1       
## [17] htmltools_0.2.6  class_7.3-13     lazyeval_0.1.10  yaml_2.1.13     
## [21] digest_0.6.8     assertthat_0.1   reshape2_1.4.1   evaluate_0.7    
## [25] rmarkdown_0.7    stringi_0.5-5    scales_0.2.5     foreign_0.8-65  
## [29] proto_0.3-10

The combined map makes it difficult to focus on any difference between Red and Blue States

Breaking out groups separately allows easier detection of any pattern

Next up

Taking suggestions @technocrat on twitter.

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]
  • Karthik Ram, Hadley Wickham, Clark Richards. wesanderson R package
  • 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.