[This article was first published on r.iresmi.net, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Day 20 & 21 of 30DayMapChallenge: « OpenStreetMap » and « Conflict » (previously).
Mapping the accidents between bicycles and cars in 2023 in France. We have had a few sad accidents recently showing a growing attention on cyclist security and the conflicts on the road.
We’ll use the Annual databases of road traffic injuries on an OSM background.
< section id="config" class="level1">Config
library(dplyr) library(tidyr) library(readr) library(janitor) library(sf) library(glue) library(leaflet)
Data
The data guide is available (in french).
# vehicules-2023.csv vehicles <- read_csv2( "https://www.data.gouv.fr/fr/datasets/r/146a42f5-19f0-4b3e-a887-5cd8fbef057b", name_repair = make_clean_names) # caract-2023.csv caract <- read_csv2( "https://www.data.gouv.fr/fr/datasets/r/104dbb32-704f-4e99-a71e-43563cb604f2", name_repair = make_clean_names) # usagers-2023.csv user <- read_csv2( "https://www.data.gouv.fr/fr/datasets/r/68848e2a-28dd-4efc-9d5f-d512f7dbe66f", name_repair = make_clean_names) severity <- tribble( ~grav, ~severity, 1, "Unharmed", 2, "Killed", 3, "Injured hospitalized", 4, "Slightly injured") |> mutate(severity = factor( severity, labels = c("Killed", "Injured hospitalized", "Slightly injured", "Unharmed")))
Find out
# accidents where car and bikes are involved bike_car_acc <- vehicles |> filter(catv %in% c(1, 7)) |> # 1 bike ; 7 : car count(num_acc, catv) |> pivot_wider(names_from = catv, values_from = n, names_prefix = "catv_") |> filter(catv_7 > 0 & catv_1 > 0) |> pull(num_acc) # bikers injuries bikers <- vehicles |> filter(num_acc %in% bike_car_acc, catv == 1) |> left_join(user, join_by(num_acc, id_vehicule)) |> left_join(severity, join_by(grav)) |> count(num_acc, severity) bikers_display <- bikers |> mutate(outcome = glue("{severity} ({n})")) |> arrange(severity) |> summarise(.by = num_acc, outcome = glue_collapse(outcome, sep = "<br />")) # accident locations bike_accidents <- caract |> filter(num_acc %in% bike_car_acc) |> st_as_sf(coords = c("long", "lat"), crs = "EPSG:4326") |> left_join(bikers_display, join_by(num_acc))
That’s 2858 accidents and 772 bikers killed.
< section id="map" class="level1">Map
bike_accidents |> leaflet() |> addTiles(attribution = r"( <a href="https://r.iresmi.net/">r.iresmi.net</a>. data: Ministère de l'intérieur 2023; map: <a href="https://www.openstreetmap.org/copyright/">OpenStreetMap</a>)") |> addCircleMarkers(popup = ~ glue("<b>{an}-{mois}-{jour}</b><br /><br /> biker status:<br /> {outcome}"), clusterOptions = markerClusterOptions())
To leave a comment for the author, please follow the link and comment on their blog: r.iresmi.net.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.