Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. library(stats19)
  2.  
  3. crashes_2017 = get_stats19(year = 2017, type = "Accidents", ask = FALSE)
  4. crashes_sf = format_sf(crashes_2017)
  5. west_yorkshire =
  6. police_boundaries[police_boundaries$pfa16nm == "West Yorkshire", ]
  7. crashes_wy = crashes_sf[west_yorkshire, ]
  8. casualties_2017 = get_stats19(year = 2017, type = "Casualties", ask = FALSE)
  9. vehicles_2017 = get_stats19(year = 2017, type = "Vehicles", ask = FALSE)
  10. library(tidyr)
  11. library(dplyr)
  12.  
  13. sel = casualties_2017$accident_index %in% crashes_wy$accident_index
  14. casualties_wy = casualties_2017[sel, ]
  15. cas_types = casualties_wy %>%
  16. select(accident_index, casualty_type) %>%
  17. group_by(accident_index) %>%
  18. summarise(
  19. Total = n(),
  20. walking = sum(casualty_type == "Pedestrian"),
  21. cycling = sum(casualty_type == "Cyclist"),
  22. passenger = sum(casualty_type == "Car occupant")
  23. )
  24. cj = left_join(crashes_wy, cas_types)
  25.  
  26. base::setdiff(names(cj), names(crashes_wy))
  27.  
  28. crashes_types = cj %>%
  29. filter(accident_severity != "Slight") %>%
  30. mutate(type = case_when(
  31. walking > 0 ~ "Walking",
  32. cycling > 0 ~ "Cycling",
  33. passenger > 0 ~ "Passenger",
  34. TRUE ~ "Other"
  35. ))
  36. library(sf)
  37. crashes_pedestrians = crashes_types %>%
  38. filter(walking > 0)
  39. # convert to lon lat CRS
  40. crashes_pedestrians_lonlat = st_transform(
  41. crashes_pedestrians, crs = 4326)
  42. n <- nrow(crashes_pedestrians_lonlat)
  43. m <- grDevices::colorRamp(c('red', 'green'))( (1:n)/n )
  44. crashes_pedestrians_lonlat$col <-
  45. colourvalues::colour_values(
  46. crashes_pedestrians_lonlat$accident_severity,
  47. palette = m, include_alpha = F)
  48. geojson <- geojsonsf::sf_geojson(
  49. crashes_pedestrians_lonlat[,c("col","accident_severity")],
  50. factors_as_string = FALSE)
  51. # write(geojson, "R/crashes.geojson")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement