Visualizations derive from R ggplot and plotly interactive graphics for the web. The R ggplotly package seamlessly connects ggplot and plotly, albeit not without discomfort. The graphs produced are accessible from a web browser in the links below.
The main drivers for the plots are frequencies computed with the crimeplus data.table. A flexible function, frequencisesdyn, allows the analyst to dynamically filter and dimensionalize frequency-producing queries.
The first look at the data focuses on homicide and violent crime in Chicago from 2001-2016 — or through January 2017, depending on the filter.
Alas, our homicide findings indeed confirm what’s been detailed in the news. The declining violent crime figures, though, offer hope, despite a 7.5% rise in 2016.
The remainder of the workbook details the R code to load and munge the data, produce frequencies, and graph the results.
Download a spreadsheet housing data from Chicago’s 77 communities. Load the spreadsheet into an R data.table.
Read the downloaded crime transaction file into an R data.table.
Merge the communities and crime transaction data.tables into crimeplus. Tweak the logical, factor, and date variables. The 6,277,445 final records cover the period Jan 1, 2001 to Feb 20, 2017.
Define a dynamic frequencies function. Set filter variables and exercise frequenciesdyn on the crimeplus data.table. The counts are returned very fast.
Logon to the plotly cloud. Define a ggplot/plotly function to display first homicide, then violent crime by year, by month, by hour of day, and finally by day of week. In addition to the data points and lines, each plot shows the overall mean frequency via a horizonal line, and traces a smoother to summarize direction. mkplot invokes frequenciesdyn to compute counts that feed the ggplot procedure.
Homicide and Violent Crime by Year.
Sadly, the homicide plot confirms the recent increases highlighted by a disturbing 2016. Hopefully, Chicago will experience a regression to the recent mean in coming years. But thankfully, the direction of overall violent crime is much more sanguine, down over 40% in 15 years. Maybe there is hope!
Homicide and Violent Crime by Month of Year.
Warm months are much more conducive to violent crime than cold ones.
Homicide and Violent Crime by Hour of Day.
Homicide is more common at night, while overall violent crime starts spiking in the afternoon.
Homicide and Violent Crime by Day of Week.
Not surprisingly, both homicide and violent crime peak on weekends.
Define a ggplot/plotly function to display first homicide, then violent crime by year — faceted by month. In addition to the data points and lines, each plot shows the overall mean frequency via a horizonal line, and traces a smoother to summarize direction. The mean frequency for each month is indicated by a dashed line.
The facet plots confirm and consolidate earlier observations, showing the sad uptick in homicide, along with the more hopeful overall decline in violent crime. The faceted monthly averages are telling, highlighting the seasonality in crime.