Counts of some feature often parallel counts of population. As a result, a map that shows the raw counts may be difficult to distinguish from a map of population. For example
Putting the maps together at least permits seeing which states have relatively more or fewer police caused deaths, but conveys little more, such as the degree to which such deaths are relatively high or low.
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") # preloaded objects and functions
##
## 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
shootings <- read.csv("~/Desktop/gitmo/playpen/r/csv/shootings.csv", header = FALSE)
names(shootings) <- c("id", "deaths")
comment(shootings) <- c("Data as reported by http://bit.ly/1LJwlF3 from another source, accessed 2015-07-26. Disease death rates: Centers for Disease Control and Protection: Detailed Tables for the National Vital Statistics Report (NVSR) “Deaths: Final Data for 2013.” http://1.usa.gov/1gjDcbd accessed 2015-07-27")
shootings
## id deaths
## 1 WA 212.0
## 2 OR 189.0
## 3 CA 1233.0
## 4 ID 46.0
## 5 NV 236.0
## 6 MT 40.0
## 7 WY 12.0
## 8 UT 104.0
## 9 AZ 176.0
## 10 CO 104.0
## 11 NM 86.0
## 12 ND 5.0
## 13 SD 21.0
## 14 NE 27.0
## 15 KS 47.0
## 16 OK 82.0
## 17 TX 361.0
## 18 MN 95.0
## 19 IA 26.0
## 20 MO 110.0
## 21 AR 41.0
## 22 LA 73.0
## 23 WI 56.0
## 24 IL 177.0
## 25 KY 62.0
## 26 TN 94.0
## 27 MS 44.0
## 28 MI 87.0
## 29 IN 58.0
## 30 AL 72.0
## 31 OH 158.0
## 32 GA 204.0
## 33 FL 958.0
## 34 NY 383.0
## 35 PA 142.0
## 36 WV 26.0
## 37 VA 79.0
## 38 NC 221.0
## 39 SC 76.0
## 40 VT 17.0
## 41 MA 101.0
## 42 RI 0.1
## 43 CT 0.1
## 44 NJ 83.0
## 45 DE 0.1
## 46 MD 130.0
## 47 DC 0.1
## 48 ME 55.0
## 49 NH 23.0
## 50 AK 0.1
## 51 HI 0.1
The purpose of attaching the comment is to keep a record with the data of its provenance. The id field name was chosen to match the corresponding field name in the population file to be read in next.
popshoot <- merge(pop, shootings, by = "id")
popshoot$ratio <- round(popshoot$deaths/popshoot$pop*100000,.5)
## not run
# intervals(popshoot,ratio)
# Produces error message Error in cut.default(series_colName, breaks = natural.interval, include.lowest = TRUE) : 'breaks' are not unique
Because some of the entries have been rounded to zero, some manual adjustments are needed.
natural.interval <- classIntervals(popshoot$ratio, n = 6, style = 'jenks', lowest = FALSE)$brks
natural.interval
## [1] 0.0 0.0 1.5 2.2 3.5 4.8 8.3
natural.interval <- c(0.0,1.5,2.2,3.5,4.8,8.3)
popshoot$ratio.natural <- cut(popshoot$ratio, breaks = natural.interval, include.lowest = FALSE)
map <- merge(us50, popshoot, by = 'id')
b <- ggplot(data= map) + geom_map(map=map, aes(x=long, y=lat, map_id=id, group=group),fill="white", color="black", size=0.3) +
no_ylab +
no_xlab +
plain_theme
d <- b + geom_polygon(data = map, aes(x=long, y=lat, group=group, fill=map$ratio.natural), color = "dark grey", size = 0.3)
my_title="Example of count data converted to ratios"
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.Prepared by @technocrat 2015-07-28"
makewhite = centroids[c('NV'),]
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)
p = 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-2.2(atherosclerosis)","2.2-3.5(aortic aneurysm)","3.5-4.8(asthma and emphysema)","4.8-8.3(liver disease)")) + annotate("text", x = -120000, y = -2350000, label = sources, size = 3, hjust = 0, family = "Times", colour = "black") + ggtitle(my_title) + l + w
# not run
# p
The result of using a ratio of police caused deaths per hundred thousand population is a much different picture, showing strong regional differences.
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 next installment, to be announced by @technocrat on twitter, will illustrate use of screens to provide break-out maps highlighting further differences within subgroups.
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.