# initialize libraries
if (!requireNamespace("devtools")) install.packages("devtools")
devtools::install_github("dkahle/ggmap")
library(ggmap)  # for google maps plotting
library(dplyr)  # for pivot table summaries
library(readr)  # for "parse_datetime"
library(ggplot2)  # for violin plots
register_google(key = "NeverWillIEverShareIt")

# import the tow data
df <- read.csv("tow.csv")
head(df)

# give me a summary of companies
df %>%
  group_by(Company) %>%
  summarize(n())

# Clean up the data
df$Company[df$Company == "A1 TOWING"] <- "A-1 TOWING"
df$Company[df$Company == "ACURATE TOWING"] <- "ACCURATE TOWING"
df$Company[df$Company == "ADVNACED TOWING"] <- "ADVANCED TOWING"
df$Company[df$Company == "ALS TOWING"] <- "AL'S TOWING"
df$Company[df$Company == "BLAIRS TOWING"] <- "BLAIR'S TOWING"
df$Company[df$Company == "HENRY TOWI"] <- "HENRY'S TOWING"
df$Company[df$Company == "HENRYS TOWING"] <- "HENRY'S TOWING"
df$Company[df$Company == "PETES TOWING"] <- "PETE'S TOWING"
df$Company <- as.factor(df$Company)  # sets as factoring
df$Date <- parse_datetime(df$Date, "%m/%d/%Y %I:%M:%S %p")  #sets as date
df <- filter(df, Company != "")

# Verify the cleanup
df %>%
  group_by(Company) %>%
  summarize(n())
  
# Transform / create data (Months, Weekdays, Times, Factorials)
DotW <- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")  # Establish factorial numbering order
MotY <- c("January","February","March","April","May","June","July","August","September","October","November","December")  # Establish factorial numbering order
df$Weekday <- weekdays(as.Date(df$Date))  # convert dates into weekdays, adds as column
df$Weekday <- factor(df$Weekday, levels= DotW)  # set factorial numbers based on our list
df$DotM <- format(df$Date, "%d")  # converts dates into days of the month (1-31)
df$DotY <- format(df$Date, "%j")  # converts dates into days of the year (1 - 366)
df$Month <- months(as.Date(df$Date)) # converts dates to word month
df$Month <- factor(df$Month, levels= MotY)  # set factorial numbers based on our list
df$Year <- format(df$Date, "%Y")  # converts dates to word month
df$Address <- paste0(df$Location, ", Arlington, VA")

# Most popular towing days of the week
weekday_top <- df %>%  # start a new pivot
  group_by(Weekday) %>%  # group these items by weekday
  summarize(Count = n())  # count all the instances
View(weekday_top)  # Print out the result

# Most popular towing days of the year
DotY_top <- df %>%
  group_by(DotY) %>%
  summarize(Count = n())
DotY_top[rev(order(DotY_top$Count)), ]
# Graphed.
DotY_plot <- ggplot(DotY_top, aes(x = seq(along.with=DotY), y = Count))
DotY_plot <- DotY_plot + geom_point() + geom_smooth(method = "lm", se=TRUE, formula = y ~ poly(x, 10, raw=TRUE))
DotY_plot <- DotY_plot + labs(title = "Count of Towings by Day of the Year", x = "Day of the Year", caption = "*using method of Loess and the formula 'y ~ poly10'")
DotY_plot

# Tows by month of the year
month_top <- df %>% 
  group_by(Month) %>%
  summarize(Count = n())
month_top <- month_top[order(month_top$Month), ]
month_top
# Graphed.
date_line <- ggplot(df, aes(x=Month, fill=Company))
date_line + geom_bar()

# Heatmap for tows

# ONLY RUN THE BELOW ONCE.  TAKES 1 HOUR.
#addresses <- df %>%
#  group_by(Address) %>%
#  summarize(Count = n())
#addresses$coord <- geocode(addresses$Address)
#write.csv(addresses, "tow_addresses.csv")

addresses <- read.csv("tow_addresses.csv")

addresses <- addresses %>%
  filter(
  -77.2 <= coord.lon & coord.lon <= -70,
  38.83 <= coord.lat & coord.lat <= 40,
  )
towings = merge(x=df,y=addresses,by="Address",all=TRUE)

head(towings)

# By Hotspots
qmplot(coord.lon, coord.lat, data = towings, geom = "blank", maptype = "toner-background", darken = .5, legend = "bottomleft") +
stat_density_2d(aes(fill = ..level..), geom = "polygon", alpha = .3, color = NA) +
scale_fill_gradient2("Towing", low = "green", mid = "yellow", high = "red", midpoint=1000)

# By Company
qmplot(coord.lon, coord.lat, data = filter(towings, Company != ""), maptype = "toner-background", darken = .5, color = Company) +
facet_wrap(~ Company)

# By Month of the Year
qmplot(coord.lon, coord.lat, data = filter(towings, Month != ""), maptype = "toner-background", darken = .5, color = Month) +
facet_wrap(~ Month)

