This visualization reflects reported incidents of crime that ocurred in the city of Chicago in 2018. The data source is the Chicago Police Department’s CLEAR (Citizen Law Enforcement Analysis and Reporting) system.
The dataset was downloaded from the Chicago Data Portal.
library(tidyverse)
library(leaflet)
library(lubridate)
## Loading data
chicago.r <- read_csv("PATH/.csv") %>%
filter(Year == 2018, !is.na(Latitude))
## Data Wrangling
chicago <- chicago.r %>%
select(ID, `Case Number`, Date, `Primary Type`, Description, `Location Description`, Arrest
,`X Coordinate`, `Y Coordinate`, Year, Latitude, Longitude, Location, Block) %>%
mutate(DateTime = mdy_hms(Date, tz = "America/Chicago")) %>%
mutate(Month = month(DateTime)
,Date = ymd(as.Date(DateTime))
,Hour = hour(DateTime))
chicago.crimes <- chicago %>%
select(`Primary Type`, Arrest,`Location Description`, Latitude, Longitude) %>%
filter(`Primary Type` %in% c("ASSAULT", "CRIM SEXUAL ASSAULT", "KIDNAPPING", "HUMAN TRAFFICKING"
,"NARCOTICS", "WEAPONS VIOLATION", "SEX OFFENSE", "PROSTITUTION"
,"LIQUOR LAW VIOLATION", "OFFENSE INVOLVING CHILDREN", "MOTOR VEHICLE THEFT"))
# chicago.crimes %>%
# group_by(`Primary Type`) %>%
# mutate(total.type = n()) %>%
# group_by(`Primary Type`, Arrest) %>%
# mutate(perc.arrest = n() / total.type) %>%
# ungroup() %>%
# unique() %>%
# arrange(`Primary Type`, Arrest)
Highest ratios when the perpetrator is not arrested:
chicago.crimes %>%
leaflet() %>%
addProviderTiles(providers$Stamen.Toner) %>%
addCircleMarkers(lng = ~Longitude, lat = ~Latitude
,color = ~ifelse(Arrest == T, "blue", "red")
,radius = ~4, label = ~`Location Description`
,group = ~`Primary Type`) %>%
addLayersControl(overlayGroups = ~`Primary Type`
,options = layersControlOptions(collapsed = FALSE))