Crime Rate Visualization

USA, 1973

Digital Rabbit
Aspiring Data Scientist

Crime Rate Visualization

  • A fast way to see which states have highest crime rates
  • Crime impacts quality of life
  • Crime rates can inform a person’s decision to move to another state
  • This visualization uses historical data (1973)

The Dataset

  • Modified the USArrest dataset provided with R
  • Scaled the rate for each crime to a range from 0 to 100
  • Included data for percentage of state population living in urban areas
  • Urban areas often mean more crime
ArrestData <- read.csv("arrestData.csv")
head(ArrestData)
##         State scaledAssault scaledMurder scaledRape scaledUrban
## 1     alabama         65.41        74.70     35.917       44.07
## 2     arizona         85.27        43.98     61.240       81.36
## 3    arkansas         49.66        48.19     31.525       30.51
## 4  california         79.11        49.40     86.047      100.00
## 5    colorado         54.45        42.77     81.137       77.97
## 6 connecticut         22.26        15.06      9.819       76.27

Choropleth Map

  • Created using the maps library
  • Regions are shaded in proportion to data values
  • Higher relative crime rates use deeper colors
  • Sample map output (not associated with data)

plot of chunk unnamed-chunk-2

Beware When Using Map

  • The map function expects 63 entries for USA map
  • Includes some region names for a handful of states (e.g. massachusetts:martha's vineyard)
  • Extract the names expected by map using this code:
mapnames <- data.frame(state = map("state",plot=FALSE)[4]$names)
mapnames
##                              state
## 1                          alabama
## 2                          arizona
## 3                         arkansas
## 4                       california
## 5                         colorado
## 6                      connecticut
## 7                         delaware
## 8             district of columbia
## 9                          florida
## 10                         georgia
## 11                           idaho
## 12                        illinois
## 13                         indiana
## 14                            iowa
## 15                          kansas
## 16                        kentucky
## 17                       louisiana
## 18                           maine
## 19                        maryland
## 20 massachusetts:martha's vineyard
## 21              massachusetts:main
## 22         massachusetts:nantucket
## 23                  michigan:north
## 24                  michigan:south
## 25                       minnesota
## 26                     mississippi
## 27                        missouri
## 28                         montana
## 29                        nebraska
## 30                          nevada
## 31                   new hampshire
## 32                      new jersey
## 33                      new mexico
## 34              new york:manhattan
## 35                   new york:main
## 36          new york:staten island
## 37            new york:long island
## 38           north carolina:knotts
## 39             north carolina:main
## 40             north carolina:spit
## 41                    north dakota
## 42                            ohio
## 43                        oklahoma
## 44                          oregon
## 45                    pennsylvania
## 46                    rhode island
## 47                  south carolina
## 48                    south dakota
## 49                       tennessee
## 50                           texas
## 51                            utah
## 52                         vermont
## 53             virginia:chesapeake
## 54           virginia:chincoteague
## 55                   virginia:main
## 56      washington:san juan island
## 57         washington:lopez island
## 58         washington:orcas island
## 59       washington:whidbey island
## 60                 washington:main
## 61                   west virginia
## 62                       wisconsin
## 63                         wyoming

mapping.r

  • Performs the set up work for the map and calls the map function
percent_map <- function(var, color, legend.title) {    
    # generate a vector of 100 gradients fill colors for map
    shades <- colorRampPalette(c("white", color))(100)   

    # transform floating-point data to integers; this data is either 
    # crime rate or urban population percents
    percents <- as.integer(cut(var, 100, include.lowest = TRUE, ordered = TRUE))  

    # create a vector that represents data as a shade from the shade vectore
    fills <- shades[percents]  

    # plot choropleth map
     map("state", fill = TRUE, col = fills, lty = 1, lwd= 1) 

    # overlay state borders
     map("state", col = "gray", fill = FALSE, add = TRUE)  

    # add a legend
    inc <- 25
    legend.text <- c(paste0("< 25"),
                     paste0(inc),
                     paste0(2 * inc),
                     paste0(3 * inc),
                     paste0(4 * inc))     
    # place the legend, add color patches and a title
    legend("bottomleft", 
           legend = legend.text, 
           fill = shades[c(1, 25, 50, 75, 100)], 
           title = legend.title)    
}