library(tidyverse)
## -- Attaching packages ------------------------------------------------ tidyverse 1.2.1 --
## v ggplot2 3.2.1     v purrr   0.3.2
## v tibble  2.1.3     v dplyr   0.8.3
## v tidyr   0.8.3     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## -- Conflicts --------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(esquisse)
library(readr)


Vehicle_Data_Cleaned <- read_csv("Datasets/Vehicle Data Cleaned.csv")
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   ACCIDENT_NO = col_character(),
##   VEHICLE_ID = col_character(),
##   INITIAL_DIRECTION = col_character(),
##   ROAD.SURFACE.TYPE.DESC = col_character(),
##   REG_STATE = col_character(),
##   VEHICLE_BODY_STYLE = col_character(),
##   VEHICLE_MAKE = col_character(),
##   VEHICLE_MODEL = col_character(),
##   VEHICLE_POWER = col_logical(),
##   VEHICLE.TYPE.DESC = col_character(),
##   CONSTRUCTION_TYPE = col_character(),
##   FUEL_TYPE = col_character(),
##   CUBIC_CAPACITY = col_number(),
##   FINAL_DIRECTION = col_character(),
##   TRAILER_TYPE = col_character(),
##   VEHICLE_COLOUR_1 = col_character(),
##   VEHICLE_COLOUR_2 = col_character(),
##   INITIAL_IMPACT = col_character(),
##   TRAFFIC.CONTROL.DESC = col_character(),
##   DATE = col_character()
##   # ... with 16 more columns
## )
## See spec(...) for full column specifications.
## Warning: 290774 parsing failures.
##    row              col           expected actual                                file
## 102200 EDITION          a double             ED35 'Datasets/Vehicle Data Cleaned.csv'
## 102201 EDITION          a double             ED35 'Datasets/Vehicle Data Cleaned.csv'
## 103402 EDITION          a double             ED35 'Datasets/Vehicle Data Cleaned.csv'
## 103403 EDITION          a double             ED35 'Datasets/Vehicle Data Cleaned.csv'
## 105707 TOTAL_POPULATION 1/0/T/F/TRUE/FALSE   -    'Datasets/Vehicle Data Cleaned.csv'
## ...... ................ .................. ...... ...................................
## See problems(...) for more details.
tort <- Vehicle_Data_Cleaned %>% mutate(Road_geometry =ifelse(ROAD.GEOMETRY.DESC %in% c("Cross intersection","Multiple intersection","T intersection","Y intersection"),"Intersection",ifelse(ROAD.GEOMETRY.DESC %in% c("Not at intersection"),"Not intersection","other")))


temp <- tort %>% select(Road_geometry,LIGHT.CONDITION.DESC,SPEED_ZONE,RAINFALL,NO_OF_VEHICLES,ROAD_TYPE,DRIVER_AGE,DRIVER_SEX,VEHICLE_YEAR_MANUF,VEHICLE.TYPE.DESC)



tort1 <- temp %>%
    filter(!(Road_geometry %in% "other"))
ggplot(temp) +
    aes(x = LIGHT.CONDITION.DESC) +
    geom_bar(fill = "#4daf4a") +
    theme_classic() +
    facet_wrap(vars(DRIVER_SEX))

ggplot(temp) +
    aes(x = LIGHT.CONDITION.DESC, fill = DRIVER_SEX) +
    geom_bar(position = "dodge") +
    scale_fill_hue() +
    theme_classic()

ggplot(temp) +
    aes(x = LIGHT.CONDITION.DESC) +
    geom_bar(position = "dodge", fill = "#4daf4a") +
    theme_classic()

ggplot(temp) +
    aes(x = LIGHT.CONDITION.DESC, fill = VEHICLE.TYPE.DESC) +
    geom_bar() +
    scale_fill_hue() +
    theme_classic() +
    theme(legend.position = "bottom")

ggplot(temp) +
    aes(x = LIGHT.CONDITION.DESC, fill = VEHICLE.TYPE.DESC) +
    geom_bar(position = "dodge") +
    scale_fill_hue() +
    theme_classic() +
    theme(legend.position = "bottom")

ggplot(temp) +
    aes(x = LIGHT.CONDITION.DESC, fill = DRIVER_AGE) +
    geom_bar() +
    scale_fill_hue() +
    theme_classic()

ggplot(temp) +
    aes(x = LIGHT.CONDITION.DESC, fill = DRIVER_AGE) +
    geom_bar(position = "dodge") +
    scale_fill_hue() +
    theme_classic()

    ggplot(temp) +
    aes(x = RAINFALL) +
    geom_histogram(bins = 30L, fill = "#0c4c8a") +
    scale_x_continuous(trans = "log10") +
    labs(y = "Number of Accidents") +
    theme_classic()
## Warning: Transformation introduced infinite values in continuous x-axis
## Warning: Removed 155540 rows containing non-finite values (stat_bin).

ggplot(temp) +
    aes(x = RAINFALL, fill = LIGHT.CONDITION.DESC) +
    geom_histogram(bins = 30L) +
    scale_fill_hue() +
    scale_x_continuous(trans = "log10") +
    labs(y = "Number of Accidents") +
    theme_classic()
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155540 rows containing non-finite values (stat_bin).

ggplot(temp) +
    aes(x = RAINFALL) +
    geom_histogram(bins = 30L, fill = "#0c4c8a") +
    scale_x_continuous(trans = "log10") +
    labs(y = "Number of Accidents") +
    theme_classic() +
    facet_wrap(vars(LIGHT.CONDITION.DESC))
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155540 rows containing non-finite values (stat_bin).

ggplot(temp) +
    aes(x = RAINFALL) +
    geom_histogram(bins = 30L, fill = "#0c4c8a") +
    scale_x_continuous(trans = "log10") +
    labs(y = "Number of Accidents") +
    theme_classic() +
    facet_wrap(vars(LIGHT.CONDITION.DESC))
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155540 rows containing non-finite values (stat_bin).

ggplot(temp) +
    aes(x = RAINFALL, fill = DRIVER_SEX) +
    geom_histogram(bins = 30L) +
    scale_fill_hue() +
    scale_x_continuous(trans = "log10") +
    labs(y = "Number of Accidents") +
    theme_classic() +
    facet_wrap(vars(LIGHT.CONDITION.DESC))
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155540 rows containing non-finite values (stat_bin).

ggplot(temp) +
    aes(x = RAINFALL) +
    geom_histogram(bins = 30L, fill = "#0c4c8a") +
    scale_x_continuous(trans = "log10") +
    theme_classic()
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155540 rows containing non-finite values (stat_bin).

ggplot(tort1) +
    aes(x = RAINFALL, fill = Road_geometry) +
    geom_histogram(bins = 30L) +
    scale_fill_hue() +
    scale_x_continuous(trans = "log10") +
    theme_minimal() +
    facet_wrap(vars(LIGHT.CONDITION.DESC))
## Warning: Transformation introduced infinite values in continuous x-axis
## Warning: Removed 155245 rows containing non-finite values (stat_bin).

ggplot(tort1) +
    aes(x = RAINFALL, fill = ROAD_TYPE) +
    geom_histogram(bins = 30L) +
    scale_fill_hue() +
    scale_x_continuous(trans = "log10") +
    theme_minimal() +
    facet_wrap(vars(LIGHT.CONDITION.DESC))
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155245 rows containing non-finite values (stat_bin).

ggplot(tort1) +
    aes(x = RAINFALL, fill = DRIVER_AGE) +
    geom_histogram(bins = 30L) +
    scale_fill_hue() +
    scale_x_continuous(trans = "log10") +
    theme_minimal() +
    facet_wrap(vars(LIGHT.CONDITION.DESC))
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155245 rows containing non-finite values (stat_bin).

ggplot(tort1) +
    aes(x = RAINFALL, fill = Road_geometry) +
    geom_histogram(bins = 30L) +
    scale_fill_hue() +
    scale_x_continuous(trans = "log10") +
    theme_minimal()
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155245 rows containing non-finite values (stat_bin).

ggplot(tort1) +
    aes(x = RAINFALL) +
    geom_histogram(bins = 30L, fill = "#0c4c8a") +
    scale_x_continuous(trans = "log10") +
    theme_minimal() +
    facet_wrap(vars(Road_geometry))
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155245 rows containing non-finite values (stat_bin).

ggplot(tort1) +
    aes(x = RAINFALL) +
    geom_histogram(bins = 30L, fill = "#0c4c8a") +
    scale_x_continuous(trans = "log10") +
    theme_classic() +
    facet_wrap(vars(SPEED_ZONE))
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155245 rows containing non-finite values (stat_bin).

ggplot(tort1) +
    aes(x = RAINFALL, fill = DRIVER_SEX) +
    geom_histogram(bins = 30L) +
    scale_fill_hue() +
    scale_x_continuous(trans = "log10") +
    theme_classic() +
    facet_wrap(vars(SPEED_ZONE))
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155245 rows containing non-finite values (stat_bin).

ggplot(tort1) +
    aes(x = RAINFALL) +
    geom_histogram(bins = 30L, fill = "#0c4c8a") +
    scale_x_continuous(trans = "log10") +
    theme_classic() +
    facet_wrap(vars(DRIVER_AGE))
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155245 rows containing non-finite values (stat_bin).

ggplot(tort1) +
    aes(x = RAINFALL, fill = DRIVER_SEX) +
    geom_histogram(bins = 30L) +
    scale_fill_hue() +
    scale_x_continuous(trans = "log10") +
    theme_classic()
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155245 rows containing non-finite values (stat_bin).

ggplot(tort1) +
    aes(x = RAINFALL) +
    geom_histogram(bins = 30L, fill = "#0c4c8a") +
    scale_x_continuous(trans = "log10") +
    theme_classic() +
    facet_wrap(vars(DRIVER_SEX))
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155245 rows containing non-finite values (stat_bin).

ggplot(tort1) +
    aes(x = RAINFALL) +
    geom_histogram(bins = 30L, fill = "#0c4c8a") +
    scale_x_continuous(trans = "log10") +
    theme_classic() +
    facet_wrap(vars(NO_OF_VEHICLES))
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155245 rows containing non-finite values (stat_bin).

ggplot(tort1) +
    aes(x = RAINFALL, fill = VEHICLE.TYPE.DESC) +
    geom_histogram(bins = 30L) +
    scale_fill_hue() +
    scale_x_continuous(trans = "log10") +
    theme_classic()
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155245 rows containing non-finite values (stat_bin).

ggplot(tort1) +
    aes(x = RAINFALL) +
    geom_histogram(bins = 30L, fill = "#0c4c8a") +
    scale_x_continuous(trans = "log10") +
    theme_classic() +
    facet_wrap(vars(VEHICLE.TYPE.DESC))
## Warning: Transformation introduced infinite values in continuous x-axis

## Warning: Removed 155245 rows containing non-finite values (stat_bin).