#load libraries ---------
library(tidyverse)
library(here)
library(ggbeeswarm)
… and then loading my dataset.
#read in data ----------
plotcrashes <- read_csv(here("Data/HW3", "polsoncrashes_new.csv")) # Polson,MT Vehicle crashes 2018-2022
Next, I will take a glimpse at my data in order to understand
how the the data is structured and organized. Understanding the
variables is necessary in order to ask meaningful questions about the
data to gain meaningful insights.
#take a glimpse at your data
glimpse(plotcrashes)
## Rows: 485
## Columns: 95
## $ crash_id <chr> "T015-00635", "T015-00558"…
## $ corridor_name <chr> "C000005", "C024204", "C07…
## $ reference_post_and_offset <chr> "054+0.980", "000+0.006", …
## $ city_name_from_crash_report <chr> NA, NA, "Polson", NA, "Pol…
## $ county_name_from_crash_report <chr> "Lake", "Lake", "Lake", "L…
## $ crash_date <chr> "11/23/2022", "12/27/2018"…
## $ month <dbl> 11, 12, 5, 5, 5, 2, 12, 11…
## $ day <dbl> 23, 27, 17, 12, 7, 7, 14, …
## $ year <dbl> 2022, 2018, 2018, 2018, 20…
## $ crash_time <time> 01:45:00, 10:11:00, 17:24…
## $ day_of_week <chr> "WED", "THU", "THU", "SAT"…
## $ collision_type <chr> "Fixed Object", "Rear-End"…
## $ junction_relation <chr> "Non-Junction", "Intersect…
## $ crash_severity <chr> "No apparent Injury (prope…
## $ relation_to_traffic_way <chr> "Roadside Right", "On Road…
## $ weather <chr> "Clear", "Snow", "Cloudy",…
## $ road_surface_condition <chr> "Ice/Frost", "Snow", "Dry"…
## $ lighting_condition <chr> "Dark-Not Lighted", "Dayli…
## $ num_fatalities <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ num_inuries <dbl> 0, 0, 1, 1, 0, 0, 0, 0, 0,…
## $ num_of_vehicles <dbl> 1, 2, 1, 1, 2, 1, 2, 2, 2,…
## $ num_occupants <dbl> 1, 2, 1, 1, 2, 1, 2, 2, 2,…
## $ num_of_pedestrians <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ non_motorist <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ first_harmful_event_type <chr> "Collision With Fixed Obje…
## $ first_harmful_event_within_interchange_area <chr> "No", "No", "No", "No", "N…
## $ first_harmful_event_detail <chr> "Fence", "Motor Vehicle In…
## $ contributing_circumstances_environment_1 <chr> "Weather Conditions", "Wea…
## $ contributing_circumstances_environment_2 <chr> "None", "Weather Condition…
## $ contributing_circumstances_environment_3 <chr> "None", "Weather Condition…
## $ contributing_circumstances_road_1 <chr> "Road Surface Condition (W…
## $ contributing_circumstances_road_2 <chr> "None", "Road Surface Cond…
## $ contributing_circumstances_road_3 <chr> "None", "Road Surface Cond…
## $ officer_description <chr> "HIGHWAY 93 S OF N RESERVO…
## $ crash_intersecting_roadway <chr> "NORTH RESERVOIR ROAD", "U…
## $ crash_intersection_distance_from_crash <dbl> 200, 0, 0, 0, 0, 0, 0, 18,…
## $ intersection_direction_from_crash <chr> "S", "N", NA, NA, NA, NA, …
## $ intersection_type <chr> "Not At Intersection", "T-…
## $ traffic_control_device_type <chr> "Railway Signal Lights And…
## $ pedestrian_bicycle <chr> "No", "No", "No", "No", "N…
## $ wild_animal_related <chr> "No", "No", "No", "No", "N…
## $ school_bus_related <chr> "No", "No", "No", "No", "N…
## $ hit_and_run_id <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ mdt_impaired_driver <chr> "No", "No", "Yes", "Yes", …
## $ site_study_indicator <chr> "No", "No", "No", "No", "N…
## $ site_study_request <chr> "No", "No", "No", "No", "N…
## $ site_study_recommendations <chr> NA, NA, NA, NA, NA, NA, NA…
## $ rail_road_crossing_id <lgl> NA, NA, NA, NA, NA, NA, NA…
## $ railway_grade_crossing_related <chr> "No", "No", "No", "No", "N…
## $ train_vehicle_collision_related <chr> "No", "No", "No", "No", "N…
## $ commercial_vehicle_involved <chr> "No", "No", "No", "No", "N…
## $ commerical_Vehicles <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ work_zone_related_crashes <chr> "No", "No", "No", "No", "N…
## $ work_zone_activity_type <chr> NA, NA, NA, NA, NA, NA, NA…
## $ work_zone_related_location_of_crash <chr> NA, NA, NA, NA, NA, NA, NA…
## $ work_zone_worker_present <chr> NA, NA, NA, NA, NA, NA, NA…
## $ work_zone_law_enforcement_present <chr> NA, NA, NA, NA, NA, NA, NA…
## $ district_from_crash_report <chr> NA, "Missoula", "Missoula"…
## $ financial_district_from_crash_report <chr> "Missoula", "Missoula", "M…
## $ reservation <chr> "Flathead", "Flathead", "F…
## $ govt_owner <chr> "State Highway Agency", "I…
## $ system_class <chr> "Ninhs", "Off System", "Of…
## $ location_type <chr> "On System", NA, "City", N…
## $ functional_class <chr> "Principal Arterial - Othe…
## $ aadt_crashes <dbl> 10826, 128, 60, 128, 60, 6…
## $ percent_growth <dbl> 1, 0, 0, 0, 0, 0, 1, 0, 0,…
## $ truck_aadt <dbl> 328, 0, 0, 0, 0, 0, 0, 0, …
## $ percentage_truck <dbl> 4.4, 0.0, 0.0, 0.0, 0.0, 0…
## $ speed_limit <dbl> 65, 70, 25, 35, 25, 25, 25…
## $ roadway_bikeway_facility <chr> "Marked Bicycle Lane", "Se…
## $ urban_rural <chr> "Rural", "Rural", "Rural",…
## $ grade <chr> "Road is straight: level",…
## $ number_of_lanes <dbl> 4, 2, 2, 2, 2, 2, 2, 2, 2,…
## $ shoulder_width <dbl> 8, 0, 0, 0, 8, 8, 8, 1, 4,…
## $ terrain_surface <chr> "Rolling_Tar And Gravel", …
## $ road_surface_type <chr> "Tar And Gravel", "Tar And…
## $ terrain_type <chr> "Rolling", NA, NA, NA, NA,…
## $ township_code <chr> "22N", "22N", NA, NA, NA, …
## $ range_code <chr> "20W", "20W", NA, NA, NA, …
## $ section_code <dbl> 25, 25, NA, NA, NA, NA, 4,…
## $ accumulated_mile_offset <dbl> 54.98028, 0.00639, 0.11322…
## $ x_coordinate <dbl> 253729.8, 253783.6, 251131…
## $ y_coordinate <dbl> 387439.8, 387508.1, 392130…
## $ gn_intersection_id <dbl> 0, 44150, 0, 0, 0, 0, 0, 0…
## $ gn_intersection_name <chr> NA, "N-5 & N-5", NA, NA, N…
## $ gn_corridor_1 <chr> NA, "C000005A", NA, NA, NA…
## $ gn_ref_post_corridor_1 <chr> NA, "055+0.499", NA, NA, N…
## $ gn_functional_class_1 <dbl> NA, 3, NA, NA, NA, NA, NA,…
## $ gn_corridor_2 <chr> NA, "C000005D", NA, NA, NA…
## $ gn_ref_post_corridor_2 <chr> NA, "055+0.025", NA, NA, N…
## $ gn_functional_class_2 <dbl> NA, 3, NA, NA, NA, NA, NA,…
## $ logaadt_crashes <chr> "9.289705927", "4.85203026…
## $ aadt_crashdiff <dbl> NA, -10698, -68, 68, -68, …
## $ morecrash <lgl> TRUE, FALSE, FALSE, FALSE,…
## $ morecrash_collision <lgl> TRUE, FALSE, FALSE, FALSE,…
#create pie chart
plotcrashes %>%
ggplot(aes(x = "", y = crash_severity, fill = crash_severity)) +
geom_bar(stat="identity") +
coord_polar("y", start=0) +
theme_void() +
labs(title = "Crash Severity Pie Chart",
fill = "Crash Severity")
###### exported .png from RStudio inserted
into report! —
#plot severity of crashes by day
plotcrashes %>%
ggplot(aes(x = day_of_week, y = num_of_vehicles, color = crash_severity)) +
geom_jitter() +
coord_flip() +
facet_wrap(~ crash_severity) +
labs(title = "Crashes/Number of Vehicle",
subtitle = "broken down by severity",
x = "Day of the Week",
y = "Total # of Vehicles Involved")
#create a bar chart
plotcrashes %>%
ggplot(aes(x = year, color = crash_severity, fill = crash_severity)) +
geom_bar() +
labs(title = "Bar Chart of Vehicle Crashes",
legend = "Crash Severity",
x = "Year",
y = "Count") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))