# Aggregate data by hour and severityseverity_by_hour <- data %>%group_by(Hour, Injury.Severity) %>%summarise(Count =n()) %>%ungroup()
`summarise()` has grouped output by 'Hour'. You can override using the
`.groups` argument.
# Plot using ggplot2ggplot(severity_by_hour, aes(x = Hour, y = Count, fill = Injury.Severity)) +geom_bar(stat ="identity", position ="stack") +labs(title ="Accident Severity by Time of Day", x ="Hour", y ="Count") +theme(axis.text.x =element_text(angle =45, hjust =1))
# Aggregate data by collision typecollision_types <- data %>%group_by(Collision.Type) %>%summarise(Count =n()) %>%arrange(desc(Count))# Plot using ggplot2ggplot(collision_types, aes(x =reorder(Collision.Type, -Count), y = Count, fill = Collision.Type)) +geom_bar(stat ="identity") +labs(title ="Collision Type Distribution", x ="Collision Type", y ="Count") +theme(axis.text.x =element_text(angle =45, hjust =1))
# Aggregate data by substance abuse and severitysubstance_abuse_impact <- data %>%filter(Driver.Substance.Abuse !="") %>%group_by(Driver.Substance.Abuse, Injury.Severity) %>%summarise(Count =n()) %>%ungroup()
`summarise()` has grouped output by 'Driver.Substance.Abuse'. You can override
using the `.groups` argument.
# Plot using ggplot2ggplot(substance_abuse_impact, aes(x = Driver.Substance.Abuse, y = Count, fill = Injury.Severity)) +geom_bar(stat ="identity", position ="dodge") +labs(title ="Impact of Driver Substance Abuse on Accident Severity", x ="Substance Abuse", y ="Count") +theme(axis.text.x =element_text(angle =45, hjust =1))
# Read the dataset (adjust the file name as needed)data <-read.csv("Crash_Reporting_-_Drivers_Data_20240407.csv")# Remove rows with missing coordinatesdata_clean <- data %>%filter(!is.na(Latitude) &!is.na(Longitude))# Initialize leaflet mapmap <-leaflet(data_clean) %>%addTiles() %>%addCircleMarkers(~Longitude, ~Latitude,radius =5,color ="red",stroke =FALSE,fillOpacity =0.7,popup =~paste("Location:", Location, "<br>Collision Type:", Collision.Type, "<br>Injury Severity:", Injury.Severity) ) %>%setView(lng =mean(data_clean$Longitude, na.rm =TRUE),lat =mean(data_clean$Latitude, na.rm =TRUE),zoom =10 )# Print the mapmap