Surbhi Chauhan - S4061307
2024-10-26
This project presents an interactive Shiny web application for analyzing crime rates in Victoria, Australia, with the primary goal of providing a comprehensive, accessible, and data-driven storytelling experience. Built using R and hosted as a web-based dashboard, this application empowers users to explore key crime statistics, visualize trends, and understand the distribution of offenses over time and by type.
Leveraging publicly available crime data, the dashboard provides insights into different aspects of crime in Victoria through three main visualizations:
Trend Plot: A time-series line chart that illustrates the trends in crime rates over time for specific offense types. This view allows users to track fluctuations and identify patterns for chosen offenses across different years. The accompanying data table provides the underlying data in a structured format for transparency and further analysis.
Bar Chart: A horizontal bar chart that showcases the top 10 offense types with the highest average crime rates. This chart uses visually distinct colors to aid interpretation and focuses attention on the offense types that have the most significant impact on the overall crime rate in Victoria.
Pie Chart: A pie chart that displays the distribution of top offense types for a selected year, enabling users to understand the proportionate makeup of different crimes within a particular time frame. This visualization helps in identifying which offenses contribute most to the overall crime count in any given year.
Each of these visualizations is structured to enhance the viewer’s comprehension and to support meaningful engagement with the data. The application is hosted on [specify platform, e.g., Shinyapps.io] and is accessible through a user-friendly interface that allows for intuitive interaction with the data.
This project not only aims to provide a visual narrative of crime in Victoria but also highlights the importance of using open data for informed decision-making. By empowering users with tools to explore and understand crime trends, this application demonstrates the practical applications of data visualization and analysis in public policy, law enforcement, and community awareness.
The data used in this application is sourced from Victoria’s Crime Statistics Agency, representing the most recent records available for different types of criminal offenses, grouped by year and offense type. All references are included in APA 7th Edition format in the accompanying report, along with a link to the online-hosted application and the full source code used in the analysis and visualization.
# Load necessary libraries
library(readxl)
library(dplyr)
library(ggplot2)
library(plotly)
library(leaflet)
library(shiny)
library(writexl)
library(tidyr)
library(rsconnect)file_path <- "/Users/surbhichauhan/Desktop/Data Visualisation /Data_Tables_Recorded_Offences_Visualisation_Year_Ending_June_2024_Table_01.xlsx"
crime_data <- read_excel(file_path)
head(crime_data)## # A tibble: 6 × 7
## Year `Year ending` `Offence Division` `Offence Subdivision`
## <dbl> <chr> <chr> <chr>
## 1 2024 June A Crimes against the person A10 Homicide and related offe…
## 2 2024 June A Crimes against the person A10 Homicide and related offe…
## 3 2024 June A Crimes against the person A10 Homicide and related offe…
## 4 2024 June A Crimes against the person A10 Homicide and related offe…
## 5 2024 June A Crimes against the person A20 Assault and related offen…
## 6 2024 June A Crimes against the person A20 Assault and related offen…
## # ℹ 3 more variables: `Offence Subgroup` <chr>, `Offence Count` <dbl>,
## # `Rate per 100,000 population` <dbl>
# Rename columns for consistency
colnames(crime_data) <- c("Year", "Year_Ending", "Offense_Division", "Offense_Subdivision", "Offense_Type", "Offense_Count", "Rate_per_100k")
# Ensure columns are correctly converted for selection and summarization
crime_data <- crime_data %>%
mutate(
Year = as.factor(Year),
Offense_Type = as.factor(Offense_Type)
) %>%
drop_na()# Define UI for the application
ui <- navbarPage("Crime Rate Analysis in Victoria",
# Page 1: Trend Plot with Table
tabPanel("Trend Plot",
sidebarLayout(
sidebarPanel(
selectInput("offense_type", "Select Offense Type:",
choices = unique(crime_data$Offense_Type),
selected = unique(crime_data$Offense_Type)[1])
),
mainPanel(
plotlyOutput("trend_plot"), # Trend plot output
tableOutput("trend_data_table") # Table output for raw data
)
)
),
# Page 2: Bar Chart
tabPanel("Bar Chart",
mainPanel(
plotlyOutput("bar_chart") # Bar chart output
)
),
# Page 3: Pie Chart
tabPanel("Pie Chart",
sidebarLayout(
sidebarPanel(
selectInput("selected_year", "Select Year:",
choices = unique(crime_data$Year),
selected = unique(crime_data$Year)[1])
),
mainPanel(
plotlyOutput("pie_chart") # Pie chart output
)
)
)
)
# Define server logic
server <- function(input, output) {
# Trend plot
output$trend_plot <- renderPlotly({
plot_data <- crime_data %>%
filter(Offense_Type == input$offense_type)
if (nrow(plot_data) > 0) {
plot_ly(plot_data, x = ~Year, y = ~Rate_per_100k, type = 'scatter', mode = 'lines+markers',
line = list(width = 2), marker = list(size = 6)) %>%
layout(title = paste("Trend of Crime Rates Over Time for", input$offense_type),
xaxis = list(title = "Year"),
yaxis = list(title = "Rate per 100,000"))
} else {
plot_ly() %>% layout(title = "No data available for selected Offense Type")
}
})
# Table for trend data
output$trend_data_table <- renderTable({
crime_data %>%
filter(Offense_Type == input$offense_type) %>%
select(Year, Year_Ending, Offense_Division, Offense_Subdivision, Offense_Type, Offense_Count, Rate_per_100k) # Include all relevant columns
})
# Bar Chart Plot
output$bar_chart <- renderPlotly({
bar_data <- crime_data %>%
group_by(Offense_Type) %>%
summarize(Average_Rate = mean(Rate_per_100k, na.rm = TRUE), .groups = "drop") %>%
arrange(desc(Average_Rate)) %>%
slice(1:10) # Top 10 offense types by average rate
plot_ly(bar_data, x = ~Average_Rate, y = ~reorder(Offense_Type, Average_Rate),
type = 'bar', orientation = 'h', marker = list(color = c("#FF6347", "#6A5ACD", "#20B2AA", "#FF4500", "#DA70D6",
"#87CEFA", "#FFD700", "#8A2BE2", "#32CD32", "#FF69B4"))) %>%
layout(
title = "Top 10 Offense Types by Average Crime Rate",
xaxis = list(title = "Average Rate per 100,000"),
yaxis = list(title = "Offense Type"),
showlegend = FALSE
)
})
# Pie chart plot
output$pie_chart <- renderPlotly({
pie_data <- crime_data %>%
filter(Year == input$selected_year) %>%
group_by(Offense_Type) %>%
summarize(Total_Offenses = sum(Offense_Count, na.rm = TRUE), .groups = "drop") %>%
arrange(desc(Total_Offenses)) %>%
mutate(Offense_Type = ifelse(row_number() > 10, "Other", as.character(Offense_Type))) %>%
group_by(Offense_Type) %>%
summarize(Total_Offenses = sum(Total_Offenses), .groups = "drop")
plot_ly(pie_data, labels = ~Offense_Type, values = ~Total_Offenses, type = 'pie') %>%
layout(title = paste("Top 10 Offenses Distribution for Year", input$selected_year))
})
}
# Run the application
shinyApp(ui = ui, server = server)##
## Listening on http://127.0.0.1:5078
## References:
Data Source Crime Statistics Agency. (2024). Recorded offences - Data Tables, Year Ending June 2024. Retrieved from https://www.crimestatisticsagency.vic.gov.au/crime-statistics/latest-crime-data/recorded-offences
R and Shiny Documentation RStudio. (n.d.). Shiny. Retrieved from https://shiny.rstudio.com/
Plotly for R Plotly. (n.d.). Plotly R Library. Retrieved from https://plotly.com/r/
Dplyr Package Wickham, H., François, R., Henry, L., & Müller, K. (2023). dplyr: A Grammar of Data Manipulation. R package version 1.1.3. Retrieved from https://cran.r-project.org/web/packages/dplyr/index.html
Data Visualization Principles Knaflic, C. N. (2015). Storytelling with Data: A Data Visualization Guide for Business Professionals. Wiley.
The crime rate analysis dashboard developed for this project provides an interactive and informative view into crime trends over time, particularly focusing on offense types and distribution in Victoria. By leveraging the power of R, Shiny, and Plotly, we created a platform where users can easily explore crime trends, compare the most prevalent offense types, and observe yearly distributions.
This interactive visualization dashboard serves as an effective tool for understanding and analyzing crime data, allowing stakeholders such as policymakers, law enforcement agencies, and the general public to make data-driven decisions. The design approach—using trend plots, bar charts, and pie charts—enhances readability, engagement, and clarity of the data story being conveyed.
While the current dashboard provides a strong foundation for crime data analysis, future enhancements could include additional breakdowns by demographic or geographic factors (if data permits), predictive analytics, and integration with other public safety data sources. This dashboard is a significant step toward making crime data more accessible, understandable, and impactful for various audiences.