The Crime Scene in Chicago

Chicago is known to be riddled with crime, particularly with regards to weapons related crimes and homicide. It seems that these issues year over year have not improved and some radical changes need to be made.

Google’s BigQuery service contains a dataset of all recorded crime in Chicago maintained by the city. I have worked with this data before, and it’s a topic that i’m personally very interested in. For this research, I will be looking into each crime that has occured from 2013-2018 that is accompanied by a pair of coordinates. Has Chicago improved in tackling weapons violence and homicide? Has it got much worse? What are the overall trends? This information can be geo-plotted and analyzed to understand Chicago’s crime rates on these specific crimes over the last 5 years.

Taking a Look Into The Dataset

library(readr)
chicago_crime <- read.csv('crime.csv')
head(chicago_crime)

When taking a sample view of the data, there are some very useful variables. The variables of interest in this assingment will be ‘primary_type’, ‘year’, and ‘crime_location’. ‘Primary_type’ tells what the main type of crime committed was, and ‘crime_location’ will be utilized with ‘year’ to geo-plot each of the weapons-related crimes and homicides over the last 5 years.

Finding the Correct Crimes to Analyze

unique(chicago_crime$primary_type)
##  [1] GAMBLING                          SEX OFFENSE                      
##  [3] INTERFERENCE WITH PUBLIC OFFICER  BATTERY                          
##  [5] ASSAULT                           STALKING                         
##  [7] ARSON                             DECEPTIVE PRACTICE               
##  [9] CRIMINAL TRESPASS                 INTIMIDATION                     
## [11] OBSCENITY                         THEFT                            
## [13] NARCOTICS                         CRIM SEXUAL ASSAULT              
## [15] ROBBERY                           KIDNAPPING                       
## [17] MOTOR VEHICLE THEFT               PUBLIC PEACE VIOLATION           
## [19] CRIMINAL DAMAGE                   OFFENSE INVOLVING CHILDREN       
## [21] OTHER OFFENSE                     BURGLARY                         
## [23] NON-CRIMINAL                      PROSTITUTION                     
## [25] PUBLIC INDECENCY                  NON - CRIMINAL                   
## [27] HUMAN TRAFFICKING                 HOMICIDE                         
## [29] WEAPONS VIOLATION                 OTHER NARCOTIC VIOLATION         
## [31] LIQUOR LAW VIOLATION              CONCEALED CARRY LICENSE VIOLATION
## [33] NON-CRIMINAL (SUBJECT SPECIFIED) 
## 33 Levels: ARSON ASSAULT BATTERY ... WEAPONS VIOLATION

To see what type of crimes we need to filter out of the dataset, a unique view is applied to the primary_type column. ‘Weapons Violation’ and ‘Homicide’ will be the values used to plot.

Data Preparation

library(dplyr)
library(tidyverse)
chicago_weap_hom <- chicago_crime %>%
  select(year, primary_type, crime_location) %>%
  filter(!is.na(crime_location)) %>%
  separate(crime_location, c('lat','long'), sep=",") %>%
  filter(primary_type %in% c('WEAPONS VIOLATION','HOMICIDE'))

chicago_weap_hom$long=as.numeric(chicago_weap_hom$long)
chicago_weap_hom$lat=as.numeric(chicago_weap_hom$lat)

To prep the data for the plots, I select the required columns outlined previously and then filter out crimes with missing coordinates. Next, the pair of coordinates is seperated into latitude and longitude and then we filter out the crimes not being analyzed.

Adding in the State of Illinois From the US Map

library(mapdata)
library(maps)
library(ggplot2)
usa <- map_data("usa")
states <- map_data("state")
chicago <- subset(states, region=="illinois")
chicago_plot <- ggplot(chicago, aes(long,lat, group=group)) + coord_fixed(1)+ geom_polygon(color='black', fill='gray') 
chicago_plot

Separating the State into Counties

counties <- map_data("county")
illi_counties <- subset(counties, region=='illinois')
chicago_plot + geom_polygon(data=illi_counties, fill=NA,color="white") + geom_polygon(fill=NA,color="black") + theme_bw()

Isolating Cook County, IL (The County Chicago is In)

chicago_plot + geom_polygon(data=illi_counties, fill=NA,color="white") + geom_polygon(fill=NA,color="black") + coord_cartesian(xlim = c(xlim=-87.5,-88.3),ylim=c(42.2,41.4)) + theme_bw()

Plotting Each Homicide in Chicago

homicide_plot <- chicago_plot + geom_polygon(data=illi_counties, fill=NA,color="white") + geom_polygon(fill=NA,color="black") + coord_cartesian(xlim = c(xlim=-87.5,-88.3),ylim=c(42.2,41.4)) + geom_point(data=filter(chicago_weap_hom,primary_type =='HOMICIDE'),aes(long,lat), pch=0, size=.5, color='red', inherit.aes = FALSE) + theme_bw()
homicide_plot

Weapons Violations Year-over-Year

library(gganimate)
weapons_plot <- chicago_plot + geom_polygon(data=illi_counties, fill=NA,color="white") + geom_polygon(fill=NA,color="black") + coord_cartesian(xlim = c(xlim=-87.5,-88.3),ylim=c(42.2,41.4)) + geom_point(data=filter(chicago_weap_hom,primary_type =='WEAPONS VIOLATION'), aes(long,lat),pch=1, size=2, color = 'blue', inherit.aes = FALSE) + labs(title= 'Weapons Violations in Chicago: {closest_state}') + transition_states(year, transition_length = 10, state_length = 20) + theme_bw()

weapons_anim <- animate(weapons_plot)
weapons_anim

Homicides Year-over-Year

homicide_plot <- chicago_plot + geom_polygon(data=illi_counties, fill=NA,color="white") + geom_polygon(fill=NA,color="black") + coord_cartesian(xlim = c(xlim=-87.5,-88.3),ylim=c(42.2,41.4)) + geom_point(data=filter(chicago_weap_hom,primary_type =='HOMICIDE'),aes(long,lat), pch=1, size=3, color='red', inherit.aes = FALSE) + labs(title= 'Homicides in Chicago: {closest_state}') + transition_states(year, transition_length = 10, state_length = 20) + theme_bw()

homicide_anim <- animate(homicide_plot)
homicide_anim