This is a leaflet exercise/ an assignment for Data Science Specialization /Coursera.com/. I chose a data set, which represents the Crime Report of the City of London for one month, specifically 02/2017. The data set can be downloaded from here: https://data.police.uk/data/
# look at the data
table(london_crime2$Crime.type)
Anti-social behaviour Bicycle theft
56 17
Burglary Criminal damage and arson
19 26
Drugs Other crime
24 8
Other theft Possession of weapons
122 1
Public order Robbery
20 2
Shoplifting Theft from the person
74 44
Vehicle crime Violence and sexual offences
11 59
# required packages
library(dplyr)
library(leaflet)
# load the data
london_crime <- read.csv("./2017-02-city-of-london-street.csv")
# take only the required columns: Latitude, Longitude, Crime.type
# make new column and count the different crimes
london_crime2 <- london_crime %>%
select(Crime.type, Latitude, Longitude) %>%
mutate(n = as.numeric(table(london_crime$Crime.type)[london_crime$Crime.type])) %>%
na.omit()
# we are going to need this one later
outline <- london_crime2[chull(london_crime2$Latitude, london_crime2$Longitude), ]
# make the map and add the markers for the different crimetypes
london_crime2 %>%
leaflet() %>%
addTiles() %>%
addMarkers(
lng = london_crime2[london_crime2$Crime.type == "Drugs",]$Longitude,
lat = london_crime2[london_crime2$Crime.type == "Drugs",]$Latitude,
popup = london_crime2[london_crime2$Crime.type == "Drugs",]$Crime.type,
# clusterOptions = markerClusterOptions(),
group = "Drugs"
) %>%
addMarkers(
lng = london_crime2[london_crime2$Crime.type == "Anti-social behaviour",]$Longitude,
lat = london_crime2[london_crime2$Crime.type == "Anti-social behaviour",]$Latitude,
popup = london_crime2[london_crime2$Crime.type == "Anti-social behaviour",]$Crime.type,
# clusterOptions = markerClusterOptions(),
group = "Anti-social behaviour"
) %>%
addMarkers(
lng = london_crime2[london_crime2$Crime.type == "Bicycle theft",]$Longitude,
lat = london_crime2[london_crime2$Crime.type == "Bicycle theft",]$Latitude,
popup = london_crime2[london_crime2$Crime.type == "Bicycle theft",]$Crime.type,
# clusterOptions = markerClusterOptions(),
group = "Bicycle theft"
) %>%
addMarkers(
lng = london_crime2[london_crime2$Crime.type == "Burglary",]$Longitude,
lat = london_crime2[london_crime2$Crime.type == "Burglary",]$Latitude,
popup = london_crime2[london_crime2$Crime.type == "Burglary",]$Crime.type,
# clusterOptions = markerClusterOptions(),
group = "Burglary"
) %>%
addMarkers(
lng = london_crime2[london_crime2$Crime.type == "Criminal damage and arson",]$Longitude,
lat = london_crime2[london_crime2$Crime.type == "Criminal damage and arson",]$Latitude,
popup = london_crime2[london_crime2$Crime.type == "Criminal damage and arson",]$Crime.type,
# clusterOptions = markerClusterOptions(),
group = "Criminal damage and arson"
) %>%
addMarkers(
lng = london_crime2[london_crime2$Crime.type == "Possession of weapons",]$Longitude,
lat = london_crime2[london_crime2$Crime.type == "Possession of weapons",]$Latitude,
popup = london_crime2[london_crime2$Crime.type == "Possession of weapons",]$Crime.type,
# clusterOptions = markerClusterOptions(),
group = "Possession of weapons"
) %>%
addMarkers(
lng = london_crime2[london_crime2$Crime.type == "Public order",]$Longitude,
lat = london_crime2[london_crime2$Crime.type == "Public order",]$Latitude,
popup = london_crime2[london_crime2$Crime.type == "Public order",]$Crime.type,
# clusterOptions = markerClusterOptions(),
group = "Public order"
) %>%
addMarkers(
lng = london_crime2[london_crime2$Crime.type == "Shoplifting",]$Longitude,
lat = london_crime2[london_crime2$Crime.type == "Shoplifting",]$Latitude,
popup = london_crime2[london_crime2$Crime.type == "Shoplifting",]$Crime.type,
# clusterOptions = markerClusterOptions(),
group = "Shoplifting"
) %>%
addMarkers(
lng = london_crime2[london_crime2$Crime.type == "Robbery",]$Longitude,
lat = london_crime2[london_crime2$Crime.type == "Robbery",]$Latitude,
popup = london_crime2[london_crime2$Crime.type == "Robbery",]$Crime.type,
# clusterOptions = markerClusterOptions(),
group = "Robbery"
) %>%
addMarkers(
lng = london_crime2[london_crime2$Crime.type == "Theft from the person",]$Longitude,
lat = london_crime2[london_crime2$Crime.type == "Theft from the person",]$Latitude,
popup = london_crime2[london_crime2$Crime.type == "Theft from the person",]$Crime.type,
# clusterOptions = markerClusterOptions(),
group = "Theft from the person"
) %>%
addMarkers(
lng = london_crime2[london_crime2$Crime.type == "Vehicle crime",]$Longitude,
lat = london_crime2[london_crime2$Crime.type == "Vehicle crime",]$Latitude,
popup = london_crime2[london_crime2$Crime.type == "Vehicle crime",]$Crime.type,
# clusterOptions = markerClusterOptions(),
group = "Vehicle crime"
) %>%
addMarkers(
lng = london_crime2[london_crime2$Crime.type == "Violence and sexual offences",]$Longitude,
lat = london_crime2[london_crime2$Crime.type == "Violence and sexual offences",]$Latitude,
popup = london_crime2[london_crime2$Crime.type == "Violence and sexual offences",]$Crime.type,
# clusterOptions = markerClusterOptions(),
group = "Violence and sexual offences"
) %>%
addMarkers(
lng = london_crime2[london_crime2$Crime.type == "Other crime",]$Longitude,
lat = london_crime2[london_crime2$Crime.type == "Other crime",]$Latitude,
popup = london_crime2[london_crime2$Crime.type == "Other crime",]$Crime.type,
# clusterOptions = markerClusterOptions(),
group = "Other crime"
) %>%
addMarkers(
lng = london_crime2[london_crime2$Crime.type == "Other theft",]$Longitude,
lat = london_crime2[london_crime2$Crime.type == "Other theft",]$Latitude,
popup = london_crime2[london_crime2$Crime.type == "Other theft",]$Crime.type,
# clusterOptions = markerClusterOptions(),
group = "Other theft"
) %>%
addPolygons(data = outline, lng = outline$Longitude, lat = outline$Latitude,
fill = T, weight = 1, color = "#240b36") %>%
addLayersControl(
overlayGroups = c("Drugs","Anti-social behaviour","Bicycle theft", "Burglary", "Criminal damage and arson", "Possession of weapons", "Public order", "Shoplifting", "Robbery", "Theft from the person", "Vehicle crime", "Violence and sexual offences", "Other crime", "Other theft"),
options = layersControlOptions(collapsed = F))