Skip to content

Instantly share code, notes, and snippets.

@rcatlord
Created June 27, 2017 13:37
Show Gist options
  • Save rcatlord/e3757c6593d18eb713dee96403938e91 to your computer and use it in GitHub Desktop.
Save rcatlord/e3757c6593d18eb713dee96403938e91 to your computer and use it in GitHub Desktop.
Using the data.police.uk API
library(jsonlite)
# df <- jsonlite::fromJSON(txt="https://data.police.uk/api/crimes-street/all-crime?lat=53.444853&lng=-2.278289&date=2017-02")
df <- jsonlite::fromJSON(txt="https://data.police.uk/api/crimes-street/all-crime?poly=52.268,0.543:52.794,0.238:52.130,0.478&date=2017-02")
df$location$longitude <- as.numeric(as.character(df$location$longitude))
df$location$latitude <- as.numeric(as.character(df$location$latitude))
library(tidyverse)
df %>%
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 ", df$date[1])) +
coord_flip() +
theme_minimal()
library(leaflet)
leaflet(data = df) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addMarkers(~location$longitude,~location$latitude, clusterOptions = markerClusterOptions(),popup = ~category)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment