Crime analysis plays a crucial role in law enforcement and public safety by providing insights into crime patterns, trends, and characteristics within specific geographic areas. By analyzing crime data, law enforcement agencies and policymakers can identify areas of concern, allocate resources efficiently, and develop targeted strategies for crime prevention and response. This project focuses on analyzing and visualizing crime data from Los Angeles (LA) and Chicago, two major cities in the United States, to gain valuable insights into crime distributions, types, and geographical variations.
Chicago Crime Dataset: Contains crime records from 2001 to the present. But will be using only 2020 and above. Key variables include ID, Case Number, Date and Time, Primary Type, Description, Location Description, Arrest status, Domestic incident, Beat, District, Ward, Community Area, FBI Code, Latitude, and Longitude.
Los Angeles (LA) Crime Dataset: Includes crime data from 2020 to the present. Important variables consist of DR Number, Reporting Date, Occurrence Date and Time, Crime Code, Crime Description, Location Details, Arrest status, Victim details (e.g., age, sex, descent), Premise Code, Weapon used, and Geographic coordinates.
chicago_data <- read.csv(file = '~/Desktop/Crimes_-_2001_to_Present.csv')
la_data <- read.csv(file = '~/Desktop/Crime_Data_from_2020_to_Present.csv')
Chicago Pie Chart: Displays the distribution of arrests in Chicago crimes.
LA Pie Chart: Shows the distribution of various status descriptions in LA crime dataset.
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(leaflet)
chicago_counts <- chicago_data %>%
filter(!is.na(Arrest)) %>%
count(Arrest)
pie_chart1 <- plot_ly(data = chicago_counts, labels = ~Arrest, values = ~n, type = "pie")
pie_chart1 <- pie_chart1 %>%
layout(
title = "Distribution of Arrests in Chicago Crimes",
showlegend = TRUE,
legend = list(orientation = "h", x = 0.5, y = -0.2) # Adjust legend position
)
status_counts <- la_data %>%
count(`Status.Desc`)
pie_chart2 <- plot_ly(status_counts, labels = ~`Status.Desc`, values = ~n, type = "pie")
pie_chart2 <- pie_chart2 %>%
layout(
title = "Distribution of Arrest and other in LA Crime Dataset",
showlegend = TRUE
)
pie_chart2
pie_chart1
2. Bar Charts: High-Crime Categories in Chicago and Los Angeles
Chicago Bar Chart: Highlights high-crime categories in Chicago (counts > 14000).
LA Bar Chart: Illustrates high-crime categories in Los Angeles (counts > 14000).
chicago_crimes_2020 <- chicago_data %>%
filter(Year >= 2020)
chicago_counts <- chicago_crimes_2020 %>%
count(Primary.Type)
high_chicago_counts <- chicago_counts %>%
filter(n > 14000)
la_counts <- la_data %>%
count(`Crm.Cd.Desc`)
high_la_counts <- la_counts %>%
filter(n > 14000)
chicago_plot <- plot_ly(high_chicago_counts, x = ~Primary.Type, y = ~n, type = "bar") %>%
layout(
title = "High-Crime Categories in Chicago (Counts > 14000)",
xaxis = list(title = "Primary Type", tickangle = 45),
yaxis = list(title = "Count"),
margin = list(b = 150) # Add margin for x-axis labels
)
la_plot <- plot_ly(high_la_counts, x = ~`Crm.Cd.Desc`, y = ~n, type = "bar") %>%
layout(
title = "High-Crime Categories in Los Angeles (Counts > 14000)",
xaxis = list(
title = "Crime Description",
tickangle = 45, # Adjust angle of x-axis labels for better readability
tickfont = list(size = 10) # Reduce font size of x-axis labels
),
yaxis = list(title = "Count"),
margin = list(b = 150) # Add margin for x-axis labels
)
subplot <- subplot(
chicago_plot, la_plot,
nrows = 1, shareY = TRUE, titleX = TRUE
)
subplot %>%
layout(
title = "Comparison of High-Crime Categories in Chicago and Los Angeles (Counts > 14000)",
showlegend = FALSE # Hide legend as bars are labeled directly
)
3. Bar Chart: Crime Count by Area Name in Los Angeles
Visualizes the crime count by area name in Los Angeles.
area_counts <- la_data %>%
group_by(`AREA.NAME`) %>%
summarise(Count = n()) %>%
arrange(desc(Count))
num_areas <- nrow(area_counts)
custom_palette <- rainbow(num_areas)
p <- plot_ly(area_counts, x = ~`AREA.NAME`, y = ~Count, type = "bar", color = ~`AREA.NAME`, colors = custom_palette)
p <- p %>% layout(title = "Crime Count by Area Name in Los Angeles",
xaxis = list(title = "Area Name"),
yaxis = list(title = "Crime Count"))
p
4. Crime Map: Geospatial Visualization of Crime Counts in LA
Used a leaflet map to visualize crime counts by area in Los Angeles.
Each area is represented by circle markers whose size and color indicate the crime count.
la_data_filtered <- la_data[complete.cases(la_data$LAT, la_data$LON), ]
crime_counts <- la_data_filtered %>%
group_by(`AREA.NAME`) %>%
summarise(CrimeCount = n(),
AverageLAT = mean(LAT),
AverageLON = mean(LON))
la_map <- leaflet(data = crime_counts) %>%
addTiles() %>%
setView(lng = -118.2437, lat = 34.0522, zoom = 10)
palette <- colorNumeric(palette = "Reds", domain = crime_counts$CrimeCount)
la_map <- la_map %>%
addCircleMarkers(
lng = ~AverageLON,
lat = ~AverageLAT,
radius = ~sqrt(CrimeCount) * 0.1,
color = ~palette(CrimeCount),
fillColor = ~palette(CrimeCount),
fillOpacity = 0.7,
stroke = FALSE,
label = ~paste("Area Name: ", `AREA.NAME`),
labelOptions = labelOptions(
direction = "auto",
noHide = TRUE,
textOnly = TRUE,
offset = c(0, 10)
),
popup = ~paste("Area Name: ", `AREA.NAME`, "<br>Crime Count: ", CrimeCount),
data = crime_counts
) %>%
addLegend(
position = "bottomright",
pal = palette,
values = ~CrimeCount,
title = "Crime Count",
opacity = 0.7
)
la_map <- la_map %>%
addProviderTiles("OpenStreetMap") %>%
addControl(
position = "topright",
html = '<div style="background-color: rgba(255, 255, 255, 0.8); padding: 10px; border-radius: 5px;"><strong>Los Angeles Crime Map</strong><br>Visualizing crime counts by area.</div>'
)
la_map
Pie Charts:
Choice of Graph: Pie charts effectively show the proportion of arrests,
status descriptions within a dataset.
Utilized to showcase proportions of arrests and status descriptions,
allowing for quick insights into the distribution of these categories
within each dataset.
Color Choice: Colors are used to distinguish categories and make the
charts visually appealing.
Visualization Components
* Type: Pie chart
* Data: Filtered Chicago crime dataset focusing on arrests
* Labels: Arrest status (e.g., “Arrested”, “Not Arrested”)
* Values: Counts of each arrest status
* Title: “Distribution of Arrests in Chicago Crimes”
* Legend: Horizontal orientation positioned below the chart
Bar Charts:
Graph Type: Bar charts are suitable for comparing counts across
different categories (crime types, crime descriptions) in a clear and
straightforward manner.
Color and Labeling: Colors are used to differentiate categories, and
labels are adjusted for readability.
Ideal for comparing and contrasting high-crime categories across cities,
providing a clear visualization of significant crime trends.
Visualization Components
* Type: Vertical bar chart
* Data: Los Angeles And Chicago crime dataset
* X-axis: Crime descriptions (e.g., “BATTERY - SIMPLE ASSAULT”, “VEHICLE
- STOLEN”, etc.) with angled labels and reduced font size
* Y-axis: Counts of each crime description
* Title: “High-Crime Categories in Los Angeles (Counts >
14000)”
* X-axis Label Styling: Angled tick labels with reduced font size for
better presentation
* Y-axis Label: “Count”
* Margin: Added margin for x-axis labels
Crime Map:
Map Choice: A leaflet map is chosen for its interactive and geospatial
capabilities, allowing visualization of crime distribution across LA
areas.
Color Palette: The color palette (Reds) is used to represent crime
counts, with darker shades indicating higher counts.
Marker Size and Popups: Marker size (radius) reflects crime counts, and
popups provide detailed information on crime counts and area
names.
Leveraging geospatial visualization techniques to map crime counts
across LA areas, aiding in understanding spatial patterns and hotspot
identification.
Visualization Components
* Type: Interactive leaflet map
* Data: Los Angeles crime dataset with latitude and longitude
coordinates
* Markers: Circle markers representing crime counts per area, scaled by
the square root of crime count for visual impact
* Marker Color: Color-coded by crime count using a red color palette
(“Reds”)
* Marker Size: Scaled by the square root of crime count to visualize
variation in crime intensity
* Popup: Area name and crime count displayed on marker click
* Legend: Color legend positioned at the bottom right, representing
crime count levels
* Map Tiles: OpenStreetMap tiles for base map display
* Title: “Los Angeles Crime Map”
* Control: Top-right control panel providing context and information
about the visualization
Through these visualizations, we can aim to facilitate data-driven decision-making for law enforcement agencies, policymakers, and researchers by providing meaningful insights into crime dynamics and trends in two major metropolitan areas. The choice of visualization methods and color schemes is intended to enhance clarity, interpretability, and engagement with the data, ultimately supporting efforts to improve public safety and crime prevention strategies.