Row

Roads with accidents

Accident Types

Row

By month

By day of the week

By Year

Row

Age of accident victims

---
title: "Accidents in Victoria (2006 to Aug 2019)"
author: "Bodiyabaduge Dewsri Lalithi Perera (S3762890) & Harini Mylanahally Sannaveeranna (s3755660)"
subtitle: "Data Visualization - Assignment 3"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    social: menu
    source_code: embed
---

```{r setup, include=FALSE}
pkgs <- c("readr", "data.table", "dplyr", "tidyr", "DT", "reshape2", "tm", "stringr", "gsubfn", "lubridate",
          "ggplot2", "gridExtra", "highcharter", "plotly", "ggrepel", "leaflet", "leaflet.extras", "ggmap", 
          "RColorBrewer", "viridisLite", "countrycode", "ggmap", "zipcode","forecast","treemap","flexdashboard") 

for (pkg in pkgs) {
                    if (! (pkg %in% rownames(installed.packages())))
                      { install.packages(pkg) }
                    require(pkg, character.only = TRUE)
                  }
rm(pkgs, pkg)

thm <- 
  hc_theme(
    colors = c("#1a6ecc", "#434348", "#90ed7d"),
    chart = list(
      backgroundColor = "transparent",
      style = list(fontFamily = "Source Sans Pro")
    ),
    xAxis = list(
      gridLineWidth = 1
    )
  )

#Load data 
#setwd("Desktop/RMIT Sem 2/Data Preprocessing/Ass3")
df <- read_csv("ACCIDENT/ACCIDENT.csv", 
            col_types = cols(`Accident Type Desc` = col_factor(levels = c("Collision with vehicle", "Struck Pedestrian", "Struck animal", "Collision with a fixed object", "collision with some other object", "Vehicle overturned (no collision)", "Fall from or in moving vehicle", "No collision and no object struck", "Other df")),
                            `Light Condition Desc` = col_factor(levels = c("Day", "Dusk/Dawn", "Dark Street lights on", "Dark Street lights off", "Dark Street lights unknown", "Dark No street lights", "Unknown"), ordered = TRUE)))

df$SEVERITY <- df$SEVERITY %>% factor(levels = c("1", "2", "3", "4"), labels=c("Low", "Mediam", "High", "Very High"), ordered = TRUE)
df <- df %>% mutate(DAY = day(dmy(ACCIDENTDATE)), MONTH = month(dmy(ACCIDENTDATE)),YEAR = year(dmy(ACCIDENTDATE)), HOUR = hour(hms(ACCIDENTTIME)))
df$MONTH <- month.abb[df$MONTH]

df$`Day Week Description` <- df$`Day Week Description` %>% factor(levels = c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"), ordered = TRUE)

df$MONTH <- df$MONTH  %>% factor(levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))


P <- read_csv("ACCIDENT/PERSON.csv")

P$SEX <- P$SEX %>% factor(levels = c("F", "M"), labels =c("Female", "Male"))

P$`Age Group`<- P$`Age Group`%>% factor(levels = c("0-4", "5-12", "13-15","16-17", "17-21", "22-25", "26-29", "30-39", "40-49", "50-59", "60-64", "64-69" , "70+", "unknown"), ordered = TRUE)

P$`Inj Level Desc` <- P$`Inj Level Desc` %>% factor(levels = c("Not injured", "Other injury", "Serious injury", "Fatality"), ordered = TRUE)
 
P$`Road User Type Desc` <- P$`Road User Type Desc` %>% factor(levels = c("Pedestrians", "Drivers", "Passengers", "Motorcyclists", "Pillion Passengers", "Bicyclists", "Unknown"), ordered = TRUE)

L <- L <- read_csv("ACCIDENT/ACCIDENT_LOCATION.csv")

L <- mutate(L, ROAD_ROUTE_1_Desc =cut(ROAD_ROUTE_1, 
                                breaks=c(1999, 3000, 4000, 5000, 6000, 8000, Inf), 
                                labels=c("Freeways or Highways","Forest Rds","Tourist Rds", "Main Rds", "Freeway ramps", "Unclassified Roads")))

L <- L %>% unite(Road_new, ROAD_NAME, ROAD_TYPE, sep = " ")
 
```





Row { data-width=600}
-----------------------------------------------------------------------

### Roads with accidents

```{r}
L %>% group_by(Road_new) %>% 
  summarise(count = n()) %>% arrange(desc(count)) %>% head(10) %>% na.omit(Road_new) %>%
  hchart(type= "treemap", 
         hcaes(x = Road_new, value = count, color= count)) %>%
  hc_add_theme(hc_theme_538()) %>% 
  hc_title(text = "10 high accident roads in Victoria") %>%
  hc_credits(enabled = TRUE, text = "Sources: Vicroads open data store house", 
             style = list(fontSize = "10px"))

```


### Accident Types

```{r}
df %>% group_by(`Accident Type Desc`) %>% 
  summarise(count = n()) %>% arrange(desc(count)) %>%
  hchart(type= "treemap", 
         hcaes(x = `Accident Type Desc`, value = count, color= count)) %>%
  hc_add_theme(hc_theme_538()) %>% 
  hc_title(text = "Accident Types") %>%
  hc_credits(enabled = TRUE, text = "Sources: Vicroads open data store house", 
             style = list(fontSize = "10px"))
```


Row {.tabset data-width=600}
-----------------------------------------------------------------------

### By month

```{r, fig.keep='none'}
df %>% group_by(MONTH) %>%  
  summarise(count = n()) %>% 
  hchart("line", hcaes(x = MONTH, y = count, color=-count)) %>%
  hc_legend(enabled = TRUE) %>% 
  hc_add_theme(hc_theme_elementary()) %>% 
  hc_title(text = "Accident count by month") %>%
  hc_credits(enabled = TRUE, text = "Sources: Vicroads open data store house", 
             style = list(fontSize = "10px"))
```

### By day of the week

```{r}
set.seed(2)

df %>% group_by(`Day Week Description`) %>%  
  summarise(count = n()) %>% 
  hchart("line", hcaes(x = `Day Week Description`, y = count, color=-count)) %>%
  hc_legend(enabled = TRUE) %>%
  hc_add_theme(hc_theme_elementary()) %>%
  hc_title(text = "Accident count by week day") %>%
  hc_credits(enabled = TRUE, text = "Sources: Vicroads open data store house", 
             style = list(fontSize = "10px"))
  
```

### By Year
```{r out.width="100%"}

df %>% group_by(YEAR) %>%  
  summarise(count = n()) %>% arrange(YEAR) %>% 
  hchart("line", hcaes(x = YEAR, y = count, color=-count)) %>%
  hc_legend(enabled = TRUE) %>% 
  hc_add_theme(hc_theme_elementary()) %>% 
  hc_title(text = "Accident count by year") %>%
  hc_credits(enabled = TRUE, text = "Sources: Vicroads open data store house", 
             style = list(fontSize = "10px"))


```

Row { data-width=400}
-----------------------------------------------------------------------

### Age of accident victims

```{r}
P %>% group_by(`Age Group`) %>% 
  summarise(count = n()) %>% arrange(`Age Group`) %>%
  hchart("bar", hcaes(x = `Age Group`, y = count, color = -count)) %>%
  hc_add_theme(hc_theme_ffx()) %>% 
  hc_title(text = "Age of the accident victims") %>%
  hc_credits(enabled = TRUE, text = "Sources: Vicroads open data store house", 
             style = list(fontSize = "10px")) %>%
  hc_yAxis(plotLines = list(list(label = list(text = "Average"),
                  color = "#FF0000",
                  width = 2,
                  value = 438142/14)))
```