Police in Britain not only register every single crime they encounter, and include coordinates, but also distribute their data free on the web. For this project, data downloaded from all forces for the 2021 calendar year for all of Britain is used.
The original dataset provided by the British Police is in csv format. The data was downloaded from https://data.police.uk/data/.
library(readr)
data <- read_csv("C:/Users/John/Documents/MS3083/HW5/Great Britain Crime 2021.zip")
## Multiple files in zip: reading '2021-01/2021-01-avon-and-somerset-street.csv'
## Rows: 10426 Columns: 12
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (9): Crime ID, Month, Reported by, Falls within, Location, LSOA code, LS...
## dbl (2): Longitude, Latitude
## lgl (1): Context
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Here is the structure of the data.
str(data)
## spec_tbl_df [10,426 x 12] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ Crime ID : chr [1:10426] NA NA NA "7273451a39f7c26bf981e755bf203eed58abbb97b78cf5ec02b213841031a210" ...
## $ Month : chr [1:10426] "2021-01" "2021-01" "2021-01" "2021-01" ...
## $ Reported by : chr [1:10426] "Avon and Somerset Constabulary" "Avon and Somerset Constabulary" "Avon and Somerset Constabulary" "Avon and Somerset Constabulary" ...
## $ Falls within : chr [1:10426] "Avon and Somerset Constabulary" "Avon and Somerset Constabulary" "Avon and Somerset Constabulary" "Avon and Somerset Constabulary" ...
## $ Longitude : num [1:10426] -2.51 -2.49 -2.52 -2.52 -2.49 ...
## $ Latitude : num [1:10426] 51.4 51.4 51.4 51.4 51.4 ...
## $ Location : chr [1:10426] "On or near Caernarvon Close" "On or near Conference/Exhibition Centre" "On or near A4175" "On or near Stockwood Hill" ...
## $ LSOA code : chr [1:10426] "E01014399" "E01014399" "E01014399" "E01014399" ...
## $ LSOA name : chr [1:10426] "Bath and North East Somerset 001A" "Bath and North East Somerset 001A" "Bath and North East Somerset 001A" "Bath and North East Somerset 001A" ...
## $ Crime type : chr [1:10426] "Anti-social behaviour" "Anti-social behaviour" "Anti-social behaviour" "Burglary" ...
## $ Last outcome category: chr [1:10426] NA NA NA "Status update unavailable" ...
## $ Context : logi [1:10426] NA NA NA NA NA NA ...
## - attr(*, "spec")=
## .. cols(
## .. `Crime ID` = col_character(),
## .. Month = col_character(),
## .. `Reported by` = col_character(),
## .. `Falls within` = col_character(),
## .. Longitude = col_double(),
## .. Latitude = col_double(),
## .. Location = col_character(),
## .. `LSOA code` = col_character(),
## .. `LSOA name` = col_character(),
## .. `Crime type` = col_character(),
## .. `Last outcome category` = col_character(),
## .. Context = col_logical()
## .. )
## - attr(*, "problems")=<externalptr>
This dataset provides a series of useful information regarding the crime: its locations (longitude and latitude in degrees), the address (if available), the type of crime and the court outcome (if available). For this project, only the coordinates and the type of crime are used.
For some incidents the coordinates are not provided. These are removed from the data:
data <- data[!is.na(data$Longitude)&!is.na(data$Latitude),]
This eliminates 505 entries from the file, and leaves the dataset with 9,921 rows.
There are so many crimes at different coordinates that it doesn’t make sense to plot every occurance. This is why plots of clusters of markers with addMarkers(clusterOptions = markerClusterOptions()) are used. You can zoom in to each cluster, the clusters will separate until you can see the individual markers. The Crime Type is added as a popup for each marker. Click on any blue marker to determine what crime was committed.
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.1.3
data %>%
leaflet() %>%
addTiles() %>%
addMarkers(popup=data$`Crime type` , clusterOptions=markerClusterOptions())
## Assuming "Longitude" and "Latitude" are longitude and latitude, respectively
In this project open crime data was displayed using Leaflet, one of the most popular Javascript libraries for creating interactive maps. But with the Leaflet R package, an interactive map was created with just a few lines of code.