This assignment involves data formats and access such as JavaScript Object Notation (JSON) and GEOJSON. JSON includes an object { } and an array [ ] that can pair values. Using JSON can help obtain web API which is a programmable interface to service. The architectural design of the web services:

To access a web API you can either send request or require authentication. This assignment will be using the UK Police API for Liverpool (https://data.police.uk/api/crimes-street/all-crime?lat=52.629729&lng=-1.131592&date=2013-01) which does not require authentication. The UK Police limits the amount per month of a variable you are able to obtain. UK Police API includes:

It is implemented by using JSON web services using HTTP GET and POST requests. The street-level crimes which can either have a 1-mile radius of a single point or within a custom area, in this case, the Liverpool Center area.

This assigment is also available at http://rpubs.com/ernie430/Liverpool-Crime


First step is to load libraries that will be helpful throughout the assignment.

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")
library("ggmap")
## Google Maps API Terms of Service: http://developers.google.com/maps/terms.
## Please cite ggmap if you use it: see citation("ggmap") for details.
library("RColorBrewer")
library("rgdal")
## Warning: package 'rgdal' was built under R version 3.3.2
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.3.2
## rgdal: version: 1.2-5, (SVN revision 648)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.1.2, released 2016/10/24
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/proj
##  Linking to sp version: 1.2-4
library("RCurl")
## Loading required package: bitops
library("jsonlite")
## Warning: package 'jsonlite' was built under R version 3.3.2
library("httr")
library("XML")
## Warning: package 'XML' was built under R version 3.3.2
library("leaflet")
## Warning: package 'leaflet' was built under R version 3.3.2

Since the UK Police doesn’t let the server obtain more than one month because of the amount of crimes that the API is able to return. So each month was created and acquired from January of 2015 to December of 2015

Crime_Jan <- jsonlite::fromJSON(txt="https://data.police.uk/api/crimes-street/all-crime?lat=53.405936&lng=-2.9665463&date=2015-01")
Crime_Feb <- jsonlite::fromJSON(txt="https://data.police.uk/api/crimes-street/all-crime?lat=53.405936&lng=-2.9665463&date=2015-02")
Crime_Mar <- jsonlite::fromJSON(txt="https://data.police.uk/api/crimes-street/all-crime?lat=53.405936&lng=-2.9665463&date=2015-03")
Crime_Apr <- jsonlite::fromJSON(txt="https://data.police.uk/api/crimes-street/all-crime?lat=53.405936&lng=-2.9665463&date=2015-04")
Crime_May <- jsonlite::fromJSON(txt="https://data.police.uk/api/crimes-street/all-crime?lat=53.405936&lng=-2.9665463&date=2015-05")
Crime_Jun <- jsonlite::fromJSON(txt="https://data.police.uk/api/crimes-street/all-crime?lat=53.405936&lng=-2.9665463&date=2015-06")
Crime_Jul <- jsonlite::fromJSON(txt="https://data.police.uk/api/crimes-street/all-crime?lat=53.405936&lng=-2.9665463&date=2015-07")
Crime_Aug <- jsonlite::fromJSON(txt="https://data.police.uk/api/crimes-street/all-crime?lat=53.405936&lng=-2.9665463&date=2015-08")
Crime_Sep <- jsonlite::fromJSON(txt="https://data.police.uk/api/crimes-street/all-crime?lat=53.405936&lng=-2.9665463&date=2015-09")
Crime_Oct <- jsonlite::fromJSON(txt="https://data.police.uk/api/crimes-street/all-crime?lat=53.405936&lng=-2.9665463&date=2015-10")
Crime_Nov <- jsonlite::fromJSON(txt="https://data.police.uk/api/crimes-street/all-crime?lat=53.405936&lng=-2.9665463&date=2015-11")
Crime_Dec <- jsonlite::fromJSON(txt="https://data.police.uk/api/crimes-street/all-crime?lat=53.405936&lng=-2.9665463&date=2015-12")

Looking at the data using str() function, we will be able to see that the location of latitude and longitude were as.characters and needs to be changed to as.numeric.

str(Crime_Jan)
## 'data.frame':    1155 obs. of  9 variables:
##  $ category        : chr  "anti-social-behaviour" "anti-social-behaviour" "anti-social-behaviour" "anti-social-behaviour" ...
##  $ location_type   : chr  "Force" "Force" "Force" "Force" ...
##  $ location        :'data.frame':    1155 obs. of  3 variables:
##   ..$ latitude : chr  "53.402353" "53.405648" "53.400636" "53.405648" ...
##   ..$ street   :'data.frame':    1155 obs. of  2 variables:
##   .. ..$ id  : int  910560 910569 910475 910569 910103 910309 910546 910573 910278 910093 ...
##   .. ..$ name: chr  "On or near Duke Street Lane" "On or near Shopping Area" "On or near Back Mulberry Street" "On or near Shopping Area" ...
##   ..$ longitude: chr  "-2.985464" "-2.987014" "-2.966834" "-2.987014" ...
##  $ context         : chr  "" "" "" "" ...
##  $ outcome_status  :'data.frame':    1155 obs. of  2 variables:
##   ..$ category: chr  NA NA NA NA ...
##   ..$ date    : chr  NA NA NA NA ...
##  $ persistent_id   : chr  "410f456cd10c951b857c7558344b2aba1a1b131a31e5f0c6333fe0e9eba2ef1a" "8053a65e4bfafec56bd77f446a09c9f2dba9bb6c1214442e142571ce1c08ec05" "0b0e723df7c038052c5a2e8e7f5dff23fa1848dffe0d58b9dd43b27db7ff02d4" "a997af93ea53513a56c9ca072bd1c753326cb2c6ae592cd4927737b525c11a63" ...
##  $ id              : int  38738324 38738321 38738068 38738043 38738038 38738033 38738025 38738782 38738777 38738768 ...
##  $ location_subtype: chr  "" "" "" "" ...
##  $ month           : chr  "2015-01" "2015-01" "2015-01" "2015-01" ...
Crime_Jan$location$longitude <- as.numeric(as.character(Crime_Jan$location$longitude))
Crime_Jan$location$latitude <- as.numeric(as.character(Crime_Jan$location$latitude))

Crime_Feb$location$longitude <- as.numeric(as.character(Crime_Feb$location$longitude))
Crime_Feb$location$latitude <- as.numeric(as.character(Crime_Feb$location$latitude))

Crime_Mar$location$longitude <- as.numeric(as.character(Crime_Mar$location$longitude))
Crime_Mar$location$latitude <- as.numeric(as.character(Crime_Mar$location$latitude))

Crime_Apr$location$longitude <- as.numeric(as.character(Crime_Apr$location$longitude))
Crime_Apr$location$latitude <- as.numeric(as.character(Crime_Apr$location$latitude))

Crime_May$location$longitude <- as.numeric(as.character(Crime_May$location$longitude))
Crime_May$location$latitude <- as.numeric(as.character(Crime_May$location$latitude))

Crime_Jun$location$longitude <- as.numeric(as.character(Crime_Jun$location$longitude))
Crime_Jun$location$latitude <- as.numeric(as.character(Crime_Jun$location$latitude))

Crime_Jul$location$longitude <- as.numeric(as.character(Crime_Jul$location$longitude))
Crime_Jul$location$latitude <- as.numeric(as.character(Crime_Jul$location$latitude))

Crime_Aug$location$longitude <- as.numeric(as.character(Crime_Aug$location$longitude))
Crime_Aug$location$latitude <- as.numeric(as.character(Crime_Aug$location$latitude))

Crime_Sep$location$longitude <- as.numeric(as.character(Crime_Sep$location$longitude))
Crime_Sep$location$latitude <- as.numeric(as.character(Crime_Sep$location$latitude))

Crime_Oct$location$longitude <- as.numeric(as.character(Crime_Oct$location$longitude))
Crime_Oct$location$latitude <- as.numeric(as.character(Crime_Oct$location$latitude))

Crime_Nov$location$longitude <- as.numeric(as.character(Crime_Nov$location$longitude))
Crime_Nov$location$latitude <- as.numeric(as.character(Crime_Nov$location$latitude))

Crime_Dec$location$longitude <- as.numeric(as.character(Crime_Dec$location$longitude))
Crime_Dec$location$latitude <- as.numeric(as.character(Crime_Dec$location$latitude))

Now I can start the visualization of the crime per month in Liverpool during the year or 2015.

January

Crime_Jan %>%
  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 Liverpool 2015-01 ", Crime_Jan$date[1])) +
  coord_flip() +
  theme_minimal()

Anti-Social behavior is the majority of the crime for Jan. with around 300 reports

Violent crime with approx 200,

Shoplifiting and Other theft with 150

Jan_Cluster <- leaflet(data = Crime_Jan)
Jan_Cluster <- addTiles(Jan_Cluster)
Jan_Cluster <- addMarkers(Jan_Cluster,~location$longitude,~location$latitude,clusterOptions = markerClusterOptions(),popup = ~category)
Jan_Cluster

Feburary

Crime_Feb %>%
  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 Liverpool 2015-02 ", Crime_Feb$date[1])) +
  coord_flip() +
  theme_minimal()

Anti-Social behavior is the majority of the crime for Feb with an increase of 60 more reports totaling around 360

Violent crime with 200,

Shoplifiting with 150 Other-theft decreased by 25 totaling 125

Feb_Cluster <- leaflet(data = Crime_Feb)
Feb_Cluster <- addTiles(Feb_Cluster)
Feb_Cluster <- addMarkers(Feb_Cluster,~location$longitude,~location$latitude,clusterOptions = markerClusterOptions(),popup = ~category)

Feb_Cluster

March

Crime_Mar %>%
  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 Liverpool 2015-03 ", Crime_Mar$date[1])) +
  coord_flip() +
  theme_minimal()

Anti-Social behavior is the majority of the crime for March with another increase of 15 more reports totaling around 375

Violent crime increased by 50 with a total of 250 reports

Shoplifiting increased by 50 totaling to 200 reports Other-theft same reports of 125

Mar_Cluster <- leaflet(data = Crime_Mar)
Mar_Cluster <- addTiles(Mar_Cluster)
Mar_Cluster <- addMarkers(Mar_Cluster,~location$longitude,~location$latitude,clusterOptions = markerClusterOptions(),popup = ~category)
Mar_Cluster

April

Crime_Apr %>%
  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 April 2015-04 ", Crime_Apr$date[1])) +
  coord_flip() +
  theme_minimal()

Anti-Social behavior is the majority of the crime for April with 375 reports

Violent crime decreased by 25 with a total of 225 reports

Shoplifiting decreased a little by 10 reports totaling 180 reports

Other-theft decreased to 100 reports

Apr_Cluster <- leaflet(data = Crime_Apr)
Apr_Cluster <- addTiles(Apr_Cluster)
Apr_Cluster <- addMarkers(Apr_Cluster,~location$longitude,~location$latitude,clusterOptions = markerClusterOptions(),popup = ~category)
Apr_Cluster

May

Crime_May %>%
  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 Liverpool 2015-05 ", Crime_May$date[1])) +
  coord_flip() +
  theme_minimal()

Anti-Social behavior is the majority of the crime for May with an increase of 50 totaling with 425 reports

Violent crime with 225 reports

Shoplifiting decreased totaling 145 reports

Other-theft increased to 125 reports

May_Cluster <- leaflet(data = Crime_May)
May_Cluster <- addTiles(May_Cluster)
May_Cluster <- addMarkers(May_Cluster,~location$longitude,~location$latitude,clusterOptions = markerClusterOptions(),popup = ~category)
May_Cluster

June

Crime_Jun %>%
  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 Liverpool 2015-06 ", Crime_Jun$date[1])) +
  coord_flip() +
  theme_minimal()

Anti-Social behavior is the majority of the crime for June decreased with total of 375 reports

Violent crime decreased by 25 with a total of 200 reports

Shoplifiting decreased totaling 160 reports

Other-theft totaling to 100 reports

Jun_Cluster <- leaflet(data = Crime_Jun)
Jun_Cluster <- addTiles(Jun_Cluster)
Jun_Cluster <- addMarkers(Jun_Cluster,~location$longitude,~location$latitude,clusterOptions = markerClusterOptions(),popup = ~category)
Jun_Cluster

July

Crime_Jul %>%
  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 Liverpool 2015-07 ", Crime_Jul$date[1])) +
  coord_flip() +
  theme_minimal()

Anti-Social behavior is the majority of the crime for July increased by 70 with 445 reports

Violent crime increased by 25 with a total of 225 reports

Shoplifiting totaling 160 reports

Other-theft increased totaling to 145 reports

Jul_Cluster <- leaflet(data = Crime_Jul)
Jul_Cluster <- addTiles(Jul_Cluster)
Jul_Cluster <- addMarkers(Jul_Cluster,~location$longitude,~location$latitude,clusterOptions = markerClusterOptions(),popup = ~category)
Jul_Cluster

August

Crime_Aug %>%
  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 Liverpool 2015-08", Crime_Aug$date[1])) +
  coord_flip() +
  theme_minimal()

Anti-Social behavior is the majority of the crime for August decreased with total of 425 reports

Violent crime increased by 25 with a total of 250 reports

Shoplifiting decreased totaling 125 reports

Other-theft decreased totaling to 125 reports

Aug_Cluster <- leaflet(data = Crime_Aug)
Aug_Cluster <- addTiles(Aug_Cluster)
Aug_Cluster <- addMarkers(Aug_Cluster,~location$longitude,~location$latitude,clusterOptions = markerClusterOptions(),popup = ~category)
Aug_Cluster

September

Crime_Sep %>%
  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 Liverpool 2015-09", Crime_Sep$date[1])) +
  coord_flip() +
  theme_minimal()

Anti-Social behavior is the majority of the crime for September with 425 reports

Violent crime decreased with a total of 240 reports

Shoplifiting decreased totaling 125 reports

Other-theft decreased totaling to 125 reports

Sep_Cluster <- leaflet(data = Crime_Sep)
Sep_Cluster <- addTiles(Sep_Cluster)
Sep_Cluster <- addMarkers(Sep_Cluster,~location$longitude,~location$latitude,clusterOptions = markerClusterOptions(),popup = ~category)
Sep_Cluster

October

Crime_Oct %>%
  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 Liverpool 2015-10 ", Crime_Oct$date[1])) +
  coord_flip() +
  theme_minimal()

Anti-Social behavior is the majority of the crime for October with 425 reports

Violent crime increased with a total of 320 reports

Shoplifiting increased totaling 170 reports

Other-theft increased totaling to 155 reports

Oct_Cluster <- leaflet(data = Crime_Oct)
Oct_Cluster <- addTiles(Oct_Cluster)
Oct_Cluster <- addMarkers(Oct_Cluster,~location$longitude,~location$latitude,clusterOptions = markerClusterOptions(),popup = ~category)
Oct_Cluster

November

Crime_Nov %>%
  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 Liverpool 2015-11", Crime_Nov$date[1])) +
  coord_flip() +
  theme_minimal()

Anti-Social behavior is the majority of the crime for November decreased with a total of 390 reports

Violent crime decreased with a total of 300 reports

Shoplifiting totaling 170 reports

Other-theft totaling to 155 reports

Nov_Cluster <- leaflet(data = Crime_Nov)
Nov_Cluster <- addTiles(Nov_Cluster)
Nov_Cluster <- addMarkers(Nov_Cluster,~location$longitude,~location$latitude,clusterOptions = markerClusterOptions(),popup = ~category)
Nov_Cluster

December

Crime_Dec %>%
  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 Liverpool 2015-12 ", Crime_Dec$date[1])) +
  coord_flip() +
  theme_minimal()

Anti-Social behavior is the majority of the crime for December decreased significantly with a total of 305 reports

Violent crime decreased with a total of 290 reports

Shoplifiting decreased with a total of 150 reports

Other-theft decreased with a total of 140 reports

Dec_Cluster <- leaflet(data = Crime_Dec)
Dec_Cluster <- addTiles(Dec_Cluster)
Dec_Cluster <- addMarkers(Dec_Cluster,~location$longitude,~location$latitude,clusterOptions = markerClusterOptions(),popup = ~category)
Dec_Cluster

Anti-Social Behavior defined by the UK and Merseyside Police, that it involves:

There are many factors that contribute to anti-social behavior and a reason why it is the number one crime seen in Liverpool and possibly within the UK. An observation made by these 12 months from January 2015 to December 2015 is the amount of crime taking place in the city center the close surrounding areas. The highest report and cluster of crime in Liverpool center is during the month of July. This can be a reason do to students/young adults being off for holiday. The crime report doesn’t list the age, but it is a suggestion of the age group that can be causing this crime. It is at the peak of Summer and a lot of people are on break for the holidays and a reason for this peak. In December, however, there is a massive decrease in crime and this can be due to the festive holidays of Christmas.

Overall, using API is an interesting way to obtain data and being able to illustrate how the geography has changed over a 12 month period in the city of Liverpool, primarily in the city center area.