Carlos Barco
September 24, 2017
The interactive plot on the next slide represents the number of road accidents in Great Britain from 2005 to 2015, grouped by severity (slight, serious, or fatal). A Loess smoother line has been added to highlight the overall evolution of the number of accidents.
The source data sets are not included in this repository. To reproduce this presentation, you will first need to download the two following zipped data sets:
Accidents0514.csv and Accidents_2015.csv files from
the zip files in a subdirectory named data.rm(list=ls())
library(plotly)
library(data.table)
library(tidyr)
library(lubridate)
library(zoo)
accidents0514 <- fread(“/Users/carlosbarco/plotly_data/Accidents0514.csv”, header = TRUE, sep = “,”) accidents0514 <- accidents0514 %>% select(Accident_Severity, Date) accidents15 <- fread(“/Users/carlosbarco/plotly_data/Accidents_2015.csv”, header = TRUE, sep = “,”) accidents15 <- accidents15 %>% select(Accident_Severity, Date)
accidents <- rbind(accidents0514, accidents15) rm(list = c(“accidents0514”, “accidents15”))
accidents$Accident_Severity <- factor(accidents$Accident_Severity, levels = 1:3, labels = c(“Fatal”, “Serious”, “Slight”))
accidents$Date <- dmy(accidents$Date)
accident_count <- accidents %>% group_by(Date, Accident_Severity) %>% summarise(count = n()) %>% spread(key = Accident_Severity, value = count) %>% as.data.frame()
loess_slight <- loess(Slight ~ as.numeric(Date), data = accident_count) loess_serious <- loess(Serious ~ as.numeric(Date), data = accident_count) loess_fatal <- loess(Fatal ~ as.numeric(Date), data = accident_count)