Developing Data Products Week 3 Assignment

Carlos Barco
September 24, 2017

First Slide

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.

Second Slide

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:

Slide With Code

rm(list=ls())
library(plotly)
library(data.table)
library(tidyr)
library(lubridate)
library(zoo)

Read data for 2005-2014 and 2015 as data tables and keep only severity and date columns

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)

Concatenate data tables and free up environment

accidents <- rbind(accidents0514, accidents15) rm(list = c(“accidents0514”, “accidents15”))

Convert severity to factor and add labels

accidents$Accident_Severity <- factor(accidents$Accident_Severity, levels = 1:3, labels = c(“Fatal”, “Serious”, “Slight”))

Convert date strings to Date objects

accidents$Date <- dmy(accidents$Date)

Group data by date and severity, get count, one row per date

accident_count <- accidents %>% group_by(Date, Accident_Severity) %>% summarise(count = n()) %>% spread(key = Accident_Severity, value = count) %>% as.data.frame()

Create a smoother for each severity to visualise general trends

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)

Road accidents in Great Britain (From Year 2005 to 2015)

plot*

  • Advice: Open in a new tap window to avoid delay in visualization