Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(stats19)
- crashes_2017 = get_stats19(year = 2017, type = "Accidents", ask = FALSE)
- crashes_sf = format_sf(crashes_2017)
- west_yorkshire =
- police_boundaries[police_boundaries$pfa16nm == "West Yorkshire", ]
- crashes_wy = crashes_sf[west_yorkshire, ]
- casualties_2017 = get_stats19(year = 2017, type = "Casualties", ask = FALSE)
- vehicles_2017 = get_stats19(year = 2017, type = "Vehicles", ask = FALSE)
- library(tidyr)
- library(dplyr)
- sel = casualties_2017$accident_index %in% crashes_wy$accident_index
- casualties_wy = casualties_2017[sel, ]
- cas_types = casualties_wy %>%
- select(accident_index, casualty_type) %>%
- group_by(accident_index) %>%
- summarise(
- Total = n(),
- walking = sum(casualty_type == "Pedestrian"),
- cycling = sum(casualty_type == "Cyclist"),
- passenger = sum(casualty_type == "Car occupant")
- )
- cj = left_join(crashes_wy, cas_types)
- base::setdiff(names(cj), names(crashes_wy))
- crashes_types = cj %>%
- filter(accident_severity != "Slight") %>%
- mutate(type = case_when(
- walking > 0 ~ "Walking",
- cycling > 0 ~ "Cycling",
- passenger > 0 ~ "Passenger",
- TRUE ~ "Other"
- ))
- library(sf)
- crashes_pedestrians = crashes_types %>%
- filter(walking > 0)
- # convert to lon lat CRS
- crashes_pedestrians_lonlat = st_transform(
- crashes_pedestrians, crs = 4326)
- n <- nrow(crashes_pedestrians_lonlat)
- m <- grDevices::colorRamp(c('red', 'green'))( (1:n)/n )
- crashes_pedestrians_lonlat$col <-
- colourvalues::colour_values(
- crashes_pedestrians_lonlat$accident_severity,
- palette = m, include_alpha = F)
- geojson <- geojsonsf::sf_geojson(
- crashes_pedestrians_lonlat[,c("col","accident_severity")],
- factors_as_string = FALSE)
- # write(geojson, "R/crashes.geojson")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement