-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New file to play with accidents data from TfL API and population data from: https://data.london.gov.uk/dataset/office-national-statistics-ons-population-estimates-borough
- Loading branch information
Jim Leach
authored
Oct 4, 2016
1 parent
a87e88f
commit 1e1b26d
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Step 0 - prep ----------------------------------------------------------- | ||
library(tidyverse) | ||
library(forcats) | ||
library(httr) | ||
library(jsonlite) | ||
library(readr) | ||
|
||
credentials <- list(app_id = tfl_app_id, app_key = tfl_app_key) | ||
|
||
# Step 1 - get data ------------------------------------------------------- | ||
# Accidents | ||
url <- "https://api.tfl.gov.uk/AccidentStats/2015" | ||
req <- GET(url, query = credentials) | ||
accidents <- req %>% | ||
content("text") %>% | ||
fromJSON() %>% | ||
as_data_frame() | ||
|
||
|
||
pop <- read_csv("./api/population-estimates-single-year-age.csv") | ||
colnames(pop) <- colnames(pop) %>% tolower() | ||
|
||
# Step 2 - prep data ------------------------------------------------------ | ||
accidents <- accidents %>% | ||
mutate(severity = as.factor(severity), | ||
borough = as.factor(borough), | ||
severity = fct_infreq(severity), | ||
borough= fct_infreq(borough)) %>% | ||
select(borough, severity, lat, lon, location, date) %>% | ||
mutate(date_cln = gsub("T", " ", date), | ||
date_cln = gsub("Z", " ", date_cln), | ||
date_cln = lubridate::ymd_hms(date_cln)) | ||
|
||
pop <- pop %>% | ||
filter(year == 2014) %>% | ||
select(borough, `all persons`) %>% | ||
rename(persons = `all persons`) | ||
|
||
joined <- accidents %>% | ||
left_join(pop) | ||
|
||
joined %>% | ||
group_by(borough) %>% | ||
summarise(total_incidents = n(), | ||
persons = mean(persons)) %>% | ||
na.omit() %>% | ||
mutate(rate = total_incidents / persons) %>% | ||
ggplot(aes(x = persons, y = total_incidents, colour = total_incidents)) + | ||
geom_point(size = 4) + | ||
geom_smooth() | ||
|
||
# Step 3 - plot ----------------------------------------------------------- | ||
accidents %>% | ||
ggplot(aes(x = fct_rev(borough))) + | ||
geom_bar(aes(fill = severity), colour = "white") + | ||
scale_fill_brewer(palette = "Dark2") + | ||
coord_flip() + | ||
theme_minimal() | ||
|
1e1b26d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inspired by https://juliasilge.com/blog/Bayesian-Blues/