Skip to content

Instantly share code, notes, and snippets.

@brettkobo
Created November 14, 2016 02:19
Show Gist options
  • Save brettkobo/43fa1d80d6c94a2c56748c9dc384747f to your computer and use it in GitHub Desktop.
Save brettkobo/43fa1d80d6c94a2c56748c9dc384747f to your computer and use it in GitHub Desktop.
R Script to analysis traffic patterns over time, specifically in LA.
#googling the distance
library("jsonlite")
library("ggplot2")
library("httr")
library("scales")
#setting theme for ggplot2
mar <- 10
themeBrettrics <- theme(plot.title = element_text(size = 15, face = "bold", margin = margin(10, 0, 10, 0)),
axis.ticks = element_line(color = "black"),
axis.text = element_text(size = 11),
axis.title = element_text(size = 13, face = "bold"),
axis.title.y = element_text(margin = margin(mar, mar, mar, mar)),
axis.title.x = element_text(margin = margin(mar, mar, mar, mar)))
#setting the key to access the Google Distance Matrix API
key <- "insert API key here"
#setting the orgin and destination.
origin <- "Pasadena+CA|Marina+Del+Ray+CA"
des <- "Marina+Del+Ray+CA|Pasadena+CA"
#creating empty dataFrame
travel_time <- data.frame()
#current time in UNIX timestamp + 15 minutes / used to start the query
running_time <- as.numeric(Sys.time()) + 900
for (i in 1:593) {
# hitting the Google Distance Matrix API
goog_time_travel <- jsonlite::fromJSON(
paste("https://maps.googleapis.com/maps/api/distancematrix/json",
"?origins=", origin,
"&destinations=", des,
"&traffic_model=best_guess",
"&departure_time=",
round(running_time, 0),
"&key=", key, sep = "")
)
#extracting the time data
val1 <- data.frame(goog_time_travel$rows$elements[1])
val2 <- data.frame(goog_time_travel$rows$elements[2])
#driving to destination
time_in_traffic1 <- val1$duration_in_traffic$text[1]
value_in_traffic1 <- val1$duration_in_traffic$value[1]
#driving from destination
time_in_traffic2 <- val2$duration_in_traffic$text[2]
value_in_traffic2 <- val2$duration_in_traffic$value[2]
#noraml time /wo traffic to destination
dur_time1 <- val1$duration$text[1]
dur_value1 <- val1$duration$value[1]
#noraml time /wo traffic from destiation
dur_time2 <- val2$duration$text[2]
dur_value2 <- val2$duration$value[2]
#building the data.frame
travel_time <- rbind(travel_time, data.frame(running_time, time_in_traffic1, value_in_traffic1, time_in_traffic2, value_in_traffic2, dur_time1, dur_value1, dur_time2, dur_value2))
#increase the time by 15 minutes every call
running_time <- running_time + 900
#show counter
print(i)
}
#converting UNIX timestamp into functioning date format
travel_time$dateStamp <- as.POSIXct(as.numeric(as.character(travel_time$running_time)), origin = "1970-01-01", tz = Sys.timezone())
#size of line
lineSize = 1.2
#creating plot
travel <- ggplot(travel_time, aes(dateStamp)) +
geom_line(aes(y = (value_in_traffic1/3600)*60, color = "Pasadena to Marina del Ray"), size = lineSize, alpha = .8) + #time from Pasadena to Marina Del Ray
geom_line(aes(y = (value_in_traffic2/3600)*60, color = "Marina del Ray to Pasadena"), size = lineSize, alpha = .8) + #time from Marina Del Ray to Pasadena
scale_x_datetime(date_breaks = "12 hour", labels = date_format("%a %l %p", tz = Sys.timezone())) +
scale_y_continuous(limits = c(30,120), breaks = seq(30,120, 15)) +
themeBrettrics +
labs(title = "Time from Home to Work", x = "Day & Time", y = "Minutes") +
scale_colour_manual(name='Legend',
breaks = c('Pasadena to Marina del Ray', 'Marina del Ray to Pasadena'),
values = c('#2B49CC', '#FF794F')) +
guides(col = guide_legend(ncol = 1,
byrow = TRUE,
title.position = "top",
title.hjust = .5,
title.theme = element_text(face = "bold", angle = 0))) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 8), legend.position = "bottom")
svg("Google The Distance/homeToWorkVV.svg", width = 8, height = 6, pointsize = 12)
print(travel)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment