getting-started-with-ukpolice.Rmd
ukpolice is an R package that facilitates retrieving data from the UK police database.. The data provided by the API contains public sector information licensed under the Open Government Licence v3.0.
ukp_crime
ukp_crime()
draws crimes from within a one mile radius of the location.
library(ukpolice)
crime_data <- ukp_crime(lat = 52.629729, lng = -1.131592)
#> No encoding supplied: defaulting to UTF-8.
head(crime_data)
#> # A tibble: 6 x 12
#> category persistent_id date lat long street_id street_name context
#> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> <chr>
#> 1 anti-soc… "" 2018… 52.6 -1.13 882346 On or near … ""
#> 2 anti-soc… "" 2018… 52.6 -1.13 883351 On or near … ""
#> 3 anti-soc… "" 2018… 52.6 -1.13 883408 On or near … ""
#> 4 anti-soc… "" 2018… 52.6 -1.13 883313 On or near … ""
#> 5 anti-soc… "" 2018… 52.6 -1.11 882420 On or near … ""
#> 6 anti-soc… "" 2018… 52.6 -1.13 882336 On or near … ""
#> # ... with 4 more variables: id <chr>, location_type <chr>,
#> # location_subtype <chr>, outcome_status <chr>
When no date is specified, it uses the latest month available, which can be found using ukp_last_update()
.
When date is specified, it must be in the format “YYYY-MM”. Currently ukp_crime()
only allows for searching of that current month.
crime_data_date <- ukp_crime(lat = 52.629729,
lng = -1.131592,
date = "2016-03")
#> No encoding supplied: defaulting to UTF-8.
head(crime_data_date)
#> # A tibble: 6 x 12
#> category persistent_id date lat long street_id street_name context
#> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> <chr>
#> 1 anti-soc… "" 2016… 52.6 -1.14 882313 On or near … ""
#> 2 anti-soc… "" 2016… 52.6 -1.12 883287 On or near … ""
#> 3 anti-soc… "" 2016… 52.6 -1.15 883538 On or near … ""
#> 4 anti-soc… "" 2016… 52.6 -1.13 883415 On or near … ""
#> 5 anti-soc… "" 2016… 52.6 -1.14 883525 On or near … ""
#> 6 anti-soc… "" 2016… 52.6 -1.14 883433 On or near … ""
#> # ... with 4 more variables: id <chr>, location_type <chr>,
#> # location_subtype <chr>, outcome_status <chr>
This is still a little buggy at the moment as it returns blank columns for variables like persistent_id
and context
, location_subtype
, and outcome_status
. This issue is currently logged at issue #11.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
crime_data <- ukp_crime(lat = 52.629729, lng = -1.131592)
#> No encoding supplied: defaulting to UTF-8.
crime_data %>%
count(category) %>%
ggplot(aes(x = reorder(category, n),
y = n)) +
geom_col() +
labs(x = "Crime Type",
y = "Number of Crimes",
title = paste0("Crimes commited in ",crime_data_date$date[1])) +
coord_flip() +
theme_minimal()
ukp_crime_poly()
finds all crimes within the polygon provided by a dataframe with columns names “lat” and “long”.
poly_df_3 <- data.frame(lat = c(52.268, 52.794, 52.130),
long = c(0.543, 0.238, 0.478))
poly_df_3
#> lat long
#> 1 52.268 0.543
#> 2 52.794 0.238
#> 3 52.130 0.478
ukp_data_poly_3 <- ukp_crime_poly(poly_df_3)
#> No encoding supplied: defaulting to UTF-8.
head(ukp_data_poly_3)
#> # A tibble: 6 x 12
#> category persistent_id date lat long street_id street_name context
#> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> <chr>
#> 1 anti-soc… "" 2018… 52.3 0.496 1141362 On or near … ""
#> 2 anti-soc… "" 2018… 52.3 0.497 1141337 On or near … ""
#> 3 anti-soc… "" 2018… 52.3 0.476 1140354 On or near … ""
#> 4 anti-soc… "" 2018… 52.3 0.413 557698 On or near … ""
#> 5 anti-soc… "" 2018… 52.3 0.413 564411 On or near … ""
#> 6 anti-soc… "" 2018… 52.2 0.484 561970 On or near … ""
#> # ... with 4 more variables: id <chr>, location_type <chr>,
#> # location_subtype <chr>, outcome_status <chr>
ukp_neighbourhood()
, retrieves a list of neighbourhoods for a force, https://data.police.uk/docs/method/neighbourhoods/
This returns a tibble with columns id
and name
.
ukp_neighbourhood("leicestershire")
#> No encoding supplied: defaulting to UTF-8.
#> # A tibble: 67 x 2
#> id name
#> <chr> <chr>
#> 1 NC04 City Centre
#> 2 NC66 Cultural Quarter
#> 3 NC67 Riverside
#> 4 NC68 Clarendon Park
#> 5 NE09 Belgrave South
#> 6 NE10 Belgrave North
#> 7 NE11 Rushey Mead
#> 8 NE12 Humberstone
#> 9 NE13 Northfields, Tailby and Morton
#> 10 NE14 Thurncourt
#> # ... with 57 more rows
id
is a Police force specific team identifier, (note that this identifier is not unique and may also be used by a different force).name
is the name for the neighbourhood.