knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "figures/README-" )
The nzpullover package makes available road policing data from the New Zealand Police in a convenient 'tidy' form. Offence categories include speed, alcohol, red light, restraints (seatbelts), mobile phone, fleeing drivers, and speeding police vehicles.
The original data published by the New Zealand Police is available as a spreadsheet here: http://www.police.govt.nz/about-us/publication/road-policing-driver-offence-data-january-2009-june-2018. Please note that this R package is not associated with the New Zealand Police.
The spreadsheet data has been made tidy by using the tidyxl and unpivotr packages. See the scripts in /data-raw. Zipped csv files are available in /inst/extdata.
The New Zealand Police refresh the data approximately quarterly. If this package lags behind, then please open an issue https://github.com/nacnudus/nzpullover/issues. The CRAN version will be updated annually.
You can install nzpullover from github with:
# install.packages("devtools") devtools::install_github("nacnudus/nzpullover")
Scroll to the bottom for lots more graphs.
library(nzpullover) library(tidyverse) library(forcats) plotdata <- excess %>% mutate(series = fct_recode(series, `Officer-issued` = "Officer-issued excess speed band", `Camera-issued` = "Camera-issued excess speed band")) %>% filter(series %in% c("Officer-issued", "Camera-issued")) %>% group_by(series, month, speed) %>% summarise(value = sum(value)) %>% group_by(series, month) %>% mutate(prop = value / sum(value), cumprop = cumsum(prop)) plotdata %>% ggplot(aes(speed, value, colour = month)) + geom_point(shape = "-", size = 20) + scale_colour_continuous(name = "", trans = "date") + scale_y_continuous(labels = scales::comma) + facet_wrap(~series, nrow = 1) + ggtitle("New Zealand summer speed limit infringements/offences", subtitle = "Frequency in each speed band") + xlab("Speed band (km/h over the limit)") + ylab("Frequency") plotdata %>% ggplot(aes(speed, cumprop, colour = month)) + geom_point(shape = "-", size = 20) + scale_colour_continuous(name = "", trans = "date") + scale_y_continuous(labels = scales::percent) + facet_wrap(~series, nrow = 1) + ggtitle("New Zealand summer speed limit infringements/offences", subtitle = "Cumulative proportion in each speed band") + xlab("Speed band (km/h over the limit)") + ylab("Cumulative proportion")
glimpse(driving_offences) glimpse(static_camera) glimpse(excess) glimpse(fleeing_area) glimpse(fleeing_district) glimpse(police_speeding) glimpse(police_speeding_band)
library(tidyverse) library(fs) library(nzpullover) theme_set(theme_grey() + theme_minimal()) all_months <- tibble(month = seq(min(driving_offences$month), max(driving_offences$month), by = "month")) timeseries <- function(series) { series_plot <- driving_offences %>% filter(series == rlang::UQ(series)) %>% group_by(month) %>% summarise(value = sum(value)) %>% right_join(all_months, by = "month") %>% ggplot(aes(month, value)) + geom_line() + geom_point() + scale_y_continuous(limits = c(0, NA), labels = scales::comma) + xlab("") + ylab("") + ggtitle(str_wrap(series)) print(series_plot) } driving_offences %>% distinct(series) %>% pull(series) %>% walk(timeseries)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.