library(ggplot2)
library(RColorBrewer)
library(data.table)  # faster fread() and better weekdays()
library(dplyr)       # consistent data.frame operations
## -------------------------------------------------------------------------
## data.table + dplyr code now lives in dtplyr.
## Please library(dtplyr)!
## -------------------------------------------------------------------------
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
## 
##     between, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(purrr)       # consistent & safe list/vector munging
## 
## Attaching package: 'purrr'
## The following objects are masked from 'package:dplyr':
## 
##     contains, order_by
## The following object is masked from 'package:data.table':
## 
##     transpose
library(tidyr)       # consistent data.frame cleaning
library(lubridate)   # date manipulation
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:data.table':
## 
##     hour, mday, month, quarter, wday, week, yday, year
## The following object is masked from 'package:base':
## 
##     date
library(countrycode) # turn country codes into pretty names
library(ggplot2)     # base plots are for Coursera professors
library(scales)      # pairs nicely with ggplot2 for plot label formatting
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
library(gridExtra)   # a helper for arranging individual ggplot objects
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
library(ggthemes)    # has a clean theme for ggplot2
library(viridis)     # best. color. palette. evar.
library(knitr) 


setwd("/Users/subasishdas1/Copy/Rpubs/rpubs/urban_fata1")
bb <- read.csv("Final2.csv")

dat3 <- subset(bb, Group_ID=="Group03")
dim(dat3)
## [1] 6832   39
dat3_1 <- subset(dat3, state=="CA")
dat3_2 <- subset(dat3, state=="TX")

CrashDir1_GroupCrash <- subset(dat3_1, Group=="Crash" & Dir=="dir1")
CrashDir2_GroupCrash <- subset(dat3_1, Group=="Crash" & Dir=="dir2")
dim(CrashDir1_GroupCrash)
## [1] 432  39
dim(CrashDir2_GroupCrash)
## [1] 432  39
a1 <- CrashDir1_GroupCrash[c(8, 13)]
a2 <- a1[!duplicated(a1[,1]),]

gg = ggplot(CrashDir1_GroupCrash, aes(x=TIME_CNTR, y=CNTR_NUM, fill=average_speed))
gg = gg + geom_tile(color="white", size=0.1)
gg = gg + labs(x=NULL, y=NULL, title="CA001 Group 3")
gg = gg + theme_tufte(base_family="Helvetica")
gg = gg + theme(plot.title=element_text(hjust=0))
gg = gg + theme(axis.ticks = element_blank())
gg = gg + theme(axis.text = element_text(size=7))
gg = gg + theme(legend.title = element_text(size=8))+
  scale_fill_gradient2(midpoint=57.5, low="#CDC08C", high="#9B110E") 
gg = gg + theme(legend.text = element_text(size=6))+
  theme(axis.text.x=element_text(angle=90, vjust=0))
gg= gg+geom_vline(xintercept=52, linetype="dashed", 
                color = "black", size=1)
gg= gg+geom_hline(yintercept=3.5, linetype="dashed", 
                color = "blue", size=1)
##detach("package:ggplot2", unload=TRUE)
##library(ggplot2)
gg1= gg+annotate(geom = "text", x = 51, y = 1.9, label = "Crash on Northbound", color = "black",
             angle = 90, size=5)
gg2= gg1+annotate(geom = "text", x = 40, y = 3.4, label = "Upstream", color = "blue", size=5)
gg3= gg2+annotate(geom = "text", x = 40, y = 3.6, label = "Downstream", color = "blue",
                  size=5)
gg4= gg3+annotate(geom = "text", x = 53, y = 1.9, label = "at 5:15 PM", color = "black",
             angle = 90, size=5)
gg4+theme(text = element_text(size=14), axis.text.y = element_text(size = 14),
          axis.text.x = element_text(size = 11))