Visualizing Crime Patterns in Chicago: An Insightful 2015 Retrospective

From Numbers to Maps: Decoding Chicago’s Crime Data

Dive into an analytical exploration of Chicago’s 2015 crime data using interactive maps and dynamic tables, bringing numbers to life.
gt
tables
news
Author

Alex Labuda

Published

January 30, 2024

2015 Chicago Crimes: Mere data or deep insights?

[WORK IN PROGRESS]

Welcome to a deep dive into the world of data visualization and analysis, focusing on one of the most pressing urban issues: crime. In this blog post, we’re going to explore the intricate patterns of crime in Chicago for the year 2015. Leveraging the power of Leaflet for interactive mapping and the versatility of {gt} tables, we’ll uncover the hidden stories behind the numbers.

This journey isn’t just about numbers and statistics; it’s about understanding the geographic distribution and frequency of various types of crimes in one of America’s largest cities. We’ll visualize this data in a way that’s both insightful and accessible, even if you’re not a data scientist. From the most common crimes to the hotspots of criminal activities, our analysis will provide a comprehensive look at the crime landscape of Chicago in 2015.

So, whether you’re a data enthusiast, a policy maker, a Chicago resident, or simply curious, join us as we map out the contours of crime in the Windy City, discovering patterns and insights that could inform future strategies and solutions.

The Most Common Crimes in Chicago (2015)

Show the code
# Calculate the maximum n value with an offset
max_n_offset <- chicago_crime_full_tbl %>% 
  count(primary_type, sort = TRUE) %>% 
  head(10) %>% 
  summarise(max_n = max(n) * 1.10) %>% 
  pull(max_n)

chicago_crime_full_tbl |> 
  # bar plot of most frequent crimes
  count(primary_type, sort = TRUE) |>
  head(10) |>
  ggplot(aes(x = fct_reorder(primary_type, n), y = n)) +
  # Add data labels
  geom_text(aes(label = scales::comma(n)), hjust = -0.1, size = 3) +
  scale_y_continuous(
    labels = scales::comma, 
    limits = c(0, max_n_offset)
    ) +
  geom_col(fill = "#D22B2B", alpha = 0.7, linewidth = 0.2) +
  coord_flip() +
  labs(
    title = NULL,
    x     = NULL,
    y     = "Number of Crimes"
  ) +
  theme(
    panel.grid = element_blank(),
    plot.title = element_text(size = 13, hjust = -0.3),
    axis.title.x = element_text(size = 11, vjust = -0.2)
  )

How do the top 10 crimes compare over the years?

Hover to reveal data points!

You can hover over the nanoplots below to see the values of the data points. If you hover over the left-most part of the chart area you can also see the range of values in the plot.

Show the code
chicago_crime_wide_tbl |>
  arrange(desc(Total)) |>
  gt(rowname_col = "primary_type") |>
  tab_header(
    title    = md("**TOP 10** Crime types in Chicago"),
    subtitle = "By year (2012 - 2015)"
    ) |>
  tab_stubhead(label = md("**Crime Type**")) |>
  cols_nanoplot(
    columns       = -starts_with(c("primary_type", "Total")),
    new_col_name  = "nanoplots",
    new_col_label = md("*2012 - 2015*"),
    options = nanoplot_options(
      data_line_stroke_color = "#D22B2B",
      data_area_fill_color   = "#D22B2B",
      data_point_fill_color  = "#D22B2B"
      )
  ) |>
  # comma format total column
  fmt_number(
    columns = Total,
    decimals = 0
  ) |>
  cols_align(align = "center", columns = nanoplots) |>
  opt_align_table_header(align = "left") |>
  tab_options(heading.padding = px(3)) |> 
  tab_options(table.width = pct(60))
TOP 10 Crime types in Chicago
By year (2012 - 2015)
Crime Type Total 2012 - 2015
THEFT 235,522
71.5K 46.0K 46.0K 71.5K 61.4K 56.7K
BATTERY 187,275
54.0K 35.0K 35.0K 54.0K 49.4K 48.8K
CRIMINAL DAMAGE 108,565
30.9K 21.3K 21.3K 30.9K 27.8K 28.6K
NARCOTICS 103,612
34.1K 19.0K 19.0K 34.1K 28.9K 21.6K
ASSAULT 63,720
18.0K 11.9K 11.9K 18.0K 16.9K 17.0K
OTHER OFFENSE 62,087
18.0K 9.90K 9.90K 18.0K 16.9K 17.3K
BURGLARY 60,035
17.9K 13.1K 14.5K 17.9K 14.6K 13.1K
DECEPTIVE PRACTICE 50,113
14.9K 8.06K 8.06K 13.2K 14.9K 13.9K
MOTOR VEHICLE THEFT 42,220
12.6K 9.75K 9.75K 12.6K 9.90K 10.0K
ROBBERY 39,933
11.8K 8.69K 8.69K 11.8K 9.80K 9.63K

Mapping Chicago’s Crime Hotspots

Show the code
# LEAFLET -----------------------------------------------------------------

leaflet(crimes_preprocess_tbl) %>% 
  addTiles() %>% 
  # weight circle size by n
  addCircleMarkers(
    lng = ~longitude, 
    lat = ~latitude, 
    radius = ~n * 0.5, 
    color = "red", 
    stroke = FALSE, 
    fillOpacity = 0.28,
    popup = ~text
  ) |> 
  # add dark providertile
  addProviderTiles("CartoDB.DarkMatter")

Share