|
import numpy as np |
|
import pandas as pd |
|
import altair as alt |
|
import streamlit as st |
|
import geopandas as gpd |
|
import json |
|
|
|
st.set_page_config(layout="wide") |
|
|
|
st.markdown(""" |
|
<style> |
|
select { |
|
font-size: 20px !important; |
|
} |
|
label { |
|
font-size: 20px !important; |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
@st.cache_data |
|
def load_data(): |
|
df1 = pd.read_csv("https://huggingface.co/datasets/Chloecky/traffic_crashes_chicago/resolve/main/Traffic_Crashes_-_Crashes_20250420.csv") |
|
df1 = df1.dropna(subset=['LATITUDE', 'LONGITUDE']) |
|
|
|
|
|
df2 = gpd.read_file("https://huggingface.co/datasets/Chloecky/traffic_crashes_chicago/resolve/main/Boundaries%20-%20Community%20Areas_20250504.geojson") |
|
|
|
return df1, df2 |
|
|
|
@st.cache_data |
|
def preprocess_data(): |
|
traffic, areas = load_data() |
|
|
|
traffic['CRASH_DATE'] = pd.to_datetime(traffic['CRASH_DATE']) |
|
traffic['YEAR'] = traffic['CRASH_DATE'].dt.year |
|
traffic = traffic[ |
|
(traffic['YEAR'].between(2018, 2024)) & |
|
(traffic['INJURIES_TOTAL'] > 0) & |
|
(traffic['LONGITUDE'] != 0) & (traffic['LATITUDE'] != 0) |
|
] |
|
traffic['DAY_TYPE'] = traffic['CRASH_DAY_OF_WEEK'].apply(lambda x: 'Weekend' if x in [1, 7] else 'Weekday') |
|
|
|
areas['geometry_area'] = areas.geometry |
|
traffic_gdf = gpd.GeoDataFrame( |
|
traffic, |
|
geometry=gpd.points_from_xy(traffic['LONGITUDE'], traffic['LATITUDE']), |
|
crs="EPSG:4326" |
|
) |
|
traffic_gdf2 = gpd.sjoin( |
|
traffic_gdf, |
|
areas[['area_numbe', 'community', 'geometry', 'geometry_area']], |
|
how='left', |
|
predicate='within' |
|
) |
|
|
|
polygon_unique = traffic_gdf2[['geometry_area', 'community', 'area_numbe', 'INJURIES_TOTAL', 'YEAR']].dropna().copy() |
|
polygon_unique = polygon_unique.groupby(['geometry_area', 'community', 'area_numbe', 'YEAR']).agg( |
|
INJURIES_TOTAL=('INJURIES_TOTAL', 'sum') |
|
).reset_index() |
|
polygon_unique = polygon_unique.set_geometry('geometry_area') |
|
geojson_data = json.loads(polygon_unique.to_json()) |
|
|
|
return traffic_gdf2, geojson_data |
|
|
|
st.title("🚦 Injured on the Way: An Overview of Chicago Traffic Injuries (2018-2024)") |
|
|
|
|
|
st.markdown( |
|
f"<span style='font-size:18px; color:black;'>By Keyu (Chloe) Cai, Yutong Zheng | Updated May 05, 2025</span>", |
|
unsafe_allow_html=True |
|
) |
|
|
|
st.markdown("---") |
|
st.subheader('Understand the Traffic Crash Data in City of Chicago with Us:') |
|
|
|
|
|
|
|
|
|
|
|
|
|
st.markdown(""" |
|
<div style='font-size:20px; line-height:1.6; text-align: justify;'> |
|
Traffic safety is a concern for every city, and Chicago is no exception. |
|
Every year, more than 100,000 crashes occur on its streets, many of which result in injuries or even fatalities. |
|
Understanding the patterns behind these incidents—when they happen, where they happen, and under what conditions—is essential for making informed decisions about infrastructure, traffic enforcement, and public awareness. |
|
We take a closer look at traffic injury data from Chicago between 2018 and 2024 to uncover trends that might help reduce future accidents. |
|
By turning raw data into clear visualizations, we hope to make these insights more accessible and useful for anyone who cares about safer roads. |
|
</div> |
|
""", unsafe_allow_html=True) |
|
|
|
st.markdown('---') |
|
|
|
st.markdown(""" |
|
<div style='font-size:22px; font-weight:bold; margin-bottom:10px;'>🚦 Where Injuries Happen Most in Chicago</div> |
|
<div style='font-size:20px; line-height:1.6; text-align: justify;'> |
|
The map (top-left heatmap) shows that traffic-related injuries are not evenly distributed across Chicago. From 2018 to 2024, areas like Austin (Community Area 25), Near North Side (8), Near West Side (28), and Greater Grand Crossing (69) have consistently seen higher numbers of injuries. |
|
These neighborhoods vary widely in terms of population, economic background, and city infrastructure. For example, some are crowded with residents or located near downtown with busy roads and heavy traffic, while others may face infrastructure challenges or limited traffic safety resources. |
|
At the same time, many other parts of the city show lower levels of injuries, especially in areas with fewer major intersections or lower vehicle and pedestrian volume. Overall, this pattern suggests that both urban activity and resource allocation may influence where and how often injuries happen on Chicago’s roads. |
|
</div> |
|
""", unsafe_allow_html=True) |
|
|
|
st.markdown("<div style='height: 30px;'></div>", unsafe_allow_html=True) |
|
|
|
st.markdown( |
|
""" |
|
<div style='font-size:22px; font-weight:bold; margin-bottom:10px;'>🚦 Hourly Crash Patterns: Weekdays vs Weekends</div> |
|
<div style='font-size:20px; line-height:1.6; text-align: justify;'> |
|
When looking at citywide data from 2018 to 2024, we see a clear pattern in when crashes with injuries tend to happen (see top-right line chart). |
|
During the early morning to early evening hours (roughly 6 AM to 8 PM), weekdays consistently see more crashes per hour than weekends. This likely reflects heavier traffic during commute times, school drop-offs, and busy weekday routines. |
|
In contrast, during late-night and very early morning hours, weekends tend to have more crashes, possibly tied to nightlife or late travel. |
|
While this pattern is clear accross the whole city, it’s important to note that different community areas may show their unique trends based on their local traffic, population, and neighborhood activity. |
|
</div> |
|
""", |
|
unsafe_allow_html=True |
|
) |
|
|
|
st.markdown("<div style='height: 30px;'></div>", unsafe_allow_html=True) |
|
|
|
st.markdown( |
|
""" |
|
<div style='font-size:22px; font-weight:bold; margin-bottom:10px;'>🚦 Fatal Injuries Are More Common in Darker Lighting Conditions</div> |
|
<div style='font-size:20px; line-height:1.6; text-align: justify;'> |
|
Between 2018 and 2024, crash data shows that deadly injuries are more likely to happen and fatal injury rates are generally higher in darker conditions (see bottom-left bar chart). |
|
The colors are assigned based on lighting brightness — the darker the lighting condition, the deeper the blue color shade. |
|
The three most dangerous lighting conditions with the highest fatal injury rates are usually <b>“Darkness, Lighted Road,” “Darkness,”</b> and <b>“Dusk.”</b> |
|
Although their exact ranking changes a bit each year, these three consistently lead, highlighting that poor lighting can be risky even when streetlights are present. |
|
On the other hand, conditions like <b>“Daylight”</b> and <b>“Dawn”</b> tend to have lower fatal injury rates. |
|
These findings align with common traffic safety concerns—drivers may have longer reaction times or reduced awareness in low-light settings. |
|
</div> |
|
""", |
|
unsafe_allow_html=True |
|
) |
|
|
|
st.markdown("<div style='height: 30px;'></div>", unsafe_allow_html=True) |
|
|
|
st.markdown( |
|
""" |
|
<div style='font-size:22px; font-weight:bold; margin-bottom:10px;'>🚦 Crash Damage Levels Over the Years</div> |
|
<div style='font-size:20px; line-height:1.6; text-align: justify;'> |
|
From 2018 to 2024, crashes that caused <b>over $1,500 in damage</b> have consistently made up the largest portion of all injury-related crashes in Chicago (see bottom-right grouped bar chart). |
|
While the overall number of such cases dropped slightly in 2020—possibly due to reduced traffic during the pandemic—but then numbers quickly rose again in the following years and even reached new highs in 2024. |
|
In comparison, crashes with less cost (under $1,500) have remained relatively stable or even decreased slightly over time. |
|
This trend could suggest more serious crashes happening or better reporting of higher-cost accidents. |
|
It’s also worth noting that while this is the overall city trend, when we look at different community areas, some show steadier numbers while others have more fluctuation in damage levels. |
|
Factors like road design, local driving behavior, and traffic volume may all play a role. |
|
</div> |
|
""", |
|
unsafe_allow_html=True |
|
) |
|
|
|
st.markdown('---') |
|
|
|
st.subheader('Explore More Through Our Interactive Dashboard!') |
|
st.markdown("<span style='font-size:18px; color:gray;'>" \ |
|
"Use the dropdown in the bottom-left to select a different year. " \ |
|
"You can also click on different areas in the heatmap to update the other charts based on that specific community.</span>", |
|
unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
traffic_gdf2, geojson_data = preprocess_data() |
|
|
|
alt.data_transformers.disable_max_rows() |
|
selection = alt.selection_point(fields=["area_numbe"]) |
|
|
|
year_selector = alt.selection_single( |
|
fields=["YEAR"], |
|
bind=alt.binding_select( |
|
options=sorted(traffic_gdf2['YEAR'].unique()), |
|
name='Select Year: ' |
|
) |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chart = alt.Chart(alt.Data(values=geojson_data["features"])).mark_geoshape(stroke='white', strokeWidth=0.8).encode( |
|
color=alt.condition( |
|
selection, |
|
alt.Color('properties.INJURIES_TOTAL:Q', |
|
scale=alt.Scale(scheme='yellowgreenblue'), |
|
legend=alt.Legend(orient='left', title='Total Injuries', offset=20, titlePadding=15)), |
|
alt.value('lightgray') |
|
), |
|
tooltip=[ |
|
alt.Tooltip('properties.area_numbe:N', title='Area Number'), |
|
alt.Tooltip('properties.community:N', title='Community Name'), |
|
alt.Tooltip('properties.INJURIES_TOTAL:Q', title='Total Injuries') |
|
] |
|
).transform_calculate( |
|
area_numbe='datum.properties.area_numbe', |
|
YEAR='datum.properties.YEAR' |
|
).add_params( |
|
selection, |
|
year_selector |
|
).transform_filter( |
|
year_selector |
|
).properties( |
|
width=350, |
|
height=350, |
|
|
|
title=alt.TitleParams( |
|
text='Heatmap of Total Injuries by Location in City of Chicago', |
|
anchor='middle' |
|
) |
|
) |
|
|
|
|
|
line = alt.Chart(traffic_gdf2.drop(columns=['geometry_area'])).transform_filter( |
|
selection, |
|
year_selector |
|
).transform_aggregate( |
|
crash_count='count()', |
|
groupby=['CRASH_HOUR', 'DAY_TYPE'] |
|
).transform_calculate( |
|
adjusted_count="datum.DAY_TYPE == 'Weekday' ? datum.crash_count / 5 : datum.crash_count / 2" |
|
).mark_line(point=True).encode( |
|
x=alt.X('CRASH_HOUR:O', title='Hour of Day'), |
|
y=alt.Y('adjusted_count:Q', title='Average Number of Injury-Related Crashes', axis=alt.Axis(grid=False)), |
|
color=alt.Color('DAY_TYPE:N', scale=alt.Scale( |
|
domain=['Weekday', 'Weekend'], |
|
range=['#225ea8', '#c7e9b4'] |
|
), legend=alt.Legend(title='Day Type', titlePadding=15)), |
|
tooltip=[ |
|
alt.Tooltip('CRASH_HOUR:O', title='Hour of Day'), |
|
alt.Tooltip('DAY_TYPE:N', title='Day Type'), |
|
alt.Tooltip('adjusted_count:Q', title='Average Count', format=',d') |
|
] |
|
).properties( |
|
width=350, |
|
height=350, |
|
|
|
title=alt.TitleParams( |
|
text='Hourly Distribution of Injury-Related Crashes: Weekday vs Weekend', |
|
anchor='middle' |
|
) |
|
) |
|
|
|
|
|
bar1 = alt.Chart(traffic_gdf2.drop(columns=['geometry_area'])).transform_filter( |
|
selection, |
|
year_selector |
|
).transform_calculate( |
|
is_fatal="datum.INJURIES_FATAL > 0 ? 1 : 0" |
|
).transform_aggregate( |
|
total='count()', |
|
fatal='sum(is_fatal)', |
|
groupby=['LIGHTING_CONDITION'] |
|
).transform_calculate( |
|
fatal_rate='datum.fatal / datum.total' |
|
).mark_bar().encode( |
|
x=alt.X('LIGHTING_CONDITION:N', sort='-y', title='Lighting Condition'), |
|
y=alt.Y('fatal_rate:Q', axis=alt.Axis(format='%', grid=False), title='Fatal Injury Rate'), |
|
color=alt.Color('LIGHTING_CONDITION:N', |
|
scale=alt.Scale( |
|
domain=['DARKNESS', 'DARKNESS, LIGHTED ROAD', 'DAWN', 'DUSK', 'DAYLIGHT', 'UNKNOWN'], |
|
range=['#084C88', '#2A6FB6', '#4FA3D9', '#7EC8E3', '#BFEFFF', '#E0F7FA'] |
|
|
|
), |
|
legend=alt.Legend(orient='left', title='Lighting Condition', titlePadding=15)), |
|
tooltip=[ |
|
alt.Tooltip('LIGHTING_CONDITION:N', title='Lighting Condition'), |
|
alt.Tooltip('fatal_rate:Q', title='Fatal Injury Rate', format='.2%') |
|
] |
|
).properties( |
|
width=400, |
|
height=350, |
|
|
|
title=alt.TitleParams( |
|
text='Fatal Injury Rate of Different Lighting Conditions', |
|
anchor='middle' |
|
) |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grouped_bar = alt.Chart(traffic_gdf2.drop(columns=['geometry_area'])).mark_bar().encode( |
|
x=alt.X('YEAR:O', title='Year'), |
|
y=alt.Y('count()', title='Count of Injury-Related Crashes', axis=alt.Axis(grid=False)), |
|
color=alt.Color('DAMAGE:N', |
|
scale=alt.Scale( |
|
domain=['$500 OR LESS', '$501 - $1,500', 'OVER $1,500'], |
|
|
|
range=['#c7e9b4', '#0e8fc3', '#084C88'] |
|
), |
|
title='Damage Level', legend=alt.Legend(title='Damage Level', titlePadding=15)), |
|
xOffset='DAMAGE:N', |
|
tooltip=[ |
|
alt.Tooltip('YEAR:O', title='Year'), |
|
alt.Tooltip('DAMAGE:N', title='Damage Level'), |
|
alt.Tooltip('count()', title='Count') |
|
] |
|
).transform_filter( |
|
selection |
|
).properties( |
|
width=450, |
|
height=350, |
|
|
|
title=alt.TitleParams( |
|
text='Annual Distribution of Crash Damage Levels (2018–2024)', |
|
anchor='middle' |
|
) |
|
) |
|
|
|
top_row = (chart|line) |
|
bottom_row = (bar1|grouped_bar).resolve_scale(color='independent') |
|
final_chart = (top_row & bottom_row).configure_axis( |
|
labelFontSize=16, |
|
titleFontSize=20 |
|
).configure_title( |
|
fontSize=24 |
|
).configure_legend( |
|
labelFontSize=16, |
|
titleFontSize=18 |
|
) |
|
|
|
st.altair_chart(final_chart, use_container_width=False) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.markdown('---') |
|
|
|
st.subheader('Contextual Visualization from Other Sources') |
|
|
|
st.image('https://miro.medium.com/v2/resize:fit:640/format:webp/1*2wHu-JlqN-KHQ9mvzlog9Q.png', |
|
caption='Viz 1: Density of Injured and Killed per Borough, by Area in New York City, 2013-2019', |
|
width = 500) |
|
st.image('https://www.researchgate.net/profile/Onur-Sevli/publication/363958427/figure/fig1/AS:11431281087201775@1664467072927/The-number-of-Fatal-Serious-and-Other-injuries-by-year-Data-from-San-Antonio-city-is.png', |
|
caption='Viz 2: Fatal/Serious vs Other Injuries in San Antonio City, 2011-2021', |
|
width = 600) |
|
st.image('Viz3.png', |
|
caption='Viz 3: Traffic Fatalities, by Rural/Urban Classification, 2013-2022 (United States)', |
|
width = 600) |
|
st.image('Viz4.png', |
|
caption='Viz 4: Fatality Rates per 100 Million VMT, by State, 2022', |
|
width = 600) |
|
|
|
st.markdown('---') |
|
|
|
st.subheader('Check out the Raw Data and Related Sources If Interested:') |
|
|
|
st.markdown(""" |
|
<div style='font-size:22px; margin-top:15px; font-weight:bold;'>Datasets are hosted on HuggingFace for our application:</div> |
|
|
|
<div style='font-size:20px;'> |
|
<ul> |
|
<li> |
|
<b>Data 1:</b> |
|
<a href="https://huggingface.co/datasets/Chloecky/traffic_crashes_chicago/resolve/main/Traffic_Crashes_-_Crashes_20250420.csv" target="_blank">https://huggingface.co/datasets/Chloecky/traffic_crashes_chicago/resolve/main/Traffic_Crashes_-_Crashes_20250420.csv</a> |
|
</li> |
|
|
|
<li> |
|
<b>Data 2:</b> |
|
<a href="https://huggingface.co/datasets/Chloecky/traffic_crashes_chicago/resolve/main/Boundaries%20-%20Community%20Areas_20250504.geojson" target="_blank">https://huggingface.co/datasets/Chloecky/traffic_crashes_chicago/resolve/main/Boundaries%20-%20Community%20Areas_20250504.geojson</a> |
|
</li> |
|
</ul> |
|
</div> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.markdown(""" |
|
<div style='font-size:22px; margin-top:15px; font-weight:bold;'>Citations:</div> |
|
|
|
<div style='font-size:20px;'> |
|
<ul> |
|
<li> |
|
<b>Viz 1:</b> Canete, J. G. (2023, June 8). <i>Simple analysis of the New York motor vehicle collision dataset</i>. Medium. |
|
<a href="https://joycegemcanete.medium.com/simple-analysis-of-the-new-york-motor-vehicle-collision-dataset-c6f883646560" target="_blank">https://joycegemcanete.medium.com/simple-analysis-of-the-new-york-motor-vehicle-collision-dataset-c6f883646560</a> |
|
</li> |
|
|
|
<li> |
|
<b>Viz 2:</b> Çelik, A., & Sevli, O. (2022). <i>Predicting traffic accident severity using machine learning techniques.</i> Turkish Journal of Forensic Sciences, 11(2), 79–83. |
|
<a href="https://doi.org/10.46810/tdfd.1136432" target="_blank">https://doi.org/10.46810/tdfd.1136432</a> |
|
</li> |
|
|
|
<li> |
|
<b>Viz 3:</b> National Highway Traffic Safety Administration. (2024, June). <i>Overview of motor vehicle traffic crashes in 2022</i> (Report No. DOT HS 813 560). U.S. Department of Transportation. |
|
<a href="https://crashstats.nhtsa.dot.gov/Api/Public/ViewPublication/813560" target="_blank">https://crashstats.nhtsa.dot.gov/Api/Public/ViewPublication/813560</a> |
|
</li> |
|
|
|
<li> |
|
<b>Viz 4:</b> National Highway Traffic Safety Administration. (2024, September). <i>Summary of motor vehicle traffic crashes: 2022 data</i> (Report No. DOT HS 813 643). U.S. Department of Transportation. |
|
<a href="https://crashstats.nhtsa.dot.gov/Api/Public/ViewPublication/813643" target="_blank">https://crashstats.nhtsa.dot.gov/Api/Public/ViewPublication/813643</a> |
|
</li> |
|
|
|
<li> |
|
<b>Data 1:</b> City of Chicago. (n.d.). <i>Traffic Crashes - Crashes. </i> Chicago Data Portal. Retrieved April 20, 2025, from |
|
<a href="https://data.cityofchicago.org/Transportation/Traffic-Crashes-Crashes/85ca-t3if" target="_blank">https://data.cityofchicago.org/Transportation/Traffic-Crashes-Crashes/85ca-t3if</a> |
|
</li> |
|
|
|
<li> |
|
<b>Data 2:</b> City of Chicago. (n.d.). <i>Boundaries - Community Areas. </i> Chicago Data Portal. Retrieved May 4, 2025, from |
|
<a href="https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-Community-Areas/igwz-8jzy" target="_blank">https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-Community-Areas/igwz-8jzy</a> |
|
</li> |
|
</ul> |
|
</div> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|