Developing Data Products Week 3 Assignment

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).

## Warning: package 'plotly' was built under R version 3.4.1

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:
1) 2014 All STATS19 data (accident, casualties and vehicle tables) for 2005 to 2014, from: http://data.dft.gov.uk.s3.amazonaws.com/road-accidents-safety-data/Stats19_Data_2005-2014.zip
2) 2015 Road Safety - Accidents 2015, from: http://data.dft.gov.uk/road-accidents-safety-data/RoadSafetyData_Accidents_2015.zip Then extract the Accidents0514.csv and Accidents_2015.csv files from the zip files in a subdirectory named data.

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

Concatenate data tables and free up environment

Convert severity to factor and add labels

Convert date strings to Date objects

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

## Warning: package 'dplyr' was built under R version 3.4.1

Create a smoother for each severity to visualise general trends

Road accidents in GB (2005-2015)

plot data

library(plotly)
p <- plot_ly(accident_count) %>%
  add_trace(x = ~Date, y = ~Slight, type = "scatter", mode = "markers",
            name = "slight", legendgroup = "slight",
            marker = list(color = "#52A9BD")) %>%
  add_trace(x = ~Date, y = ~Serious, type="scatter", mode = "markers",
            name = "serious", legendgroup = "serious",
            marker = list(color = "#FFF16B")) %>%
  add_trace(x = ~Date, y = ~Fatal, type="scatter", mode = "markers",
            name = "fatal", legendgroup = "fatal",
            marker = list(color = "#F5677D")) %>%
  layout(
    xaxis = list(title = "Years"),
    yaxis = list(title = "Number of accidents")
  )
p
## Warning: Ignoring 39 observations