Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
library(jsonlite)
library(plyr)
library(ggplot2)
library(ggthemes)
if(!requireNamespace("devtools")) install.packages("devtools")
devtools::install_github("dkahle/ggmap", ref = "tidyup")
library(ggmap)
library(extrafont)
extrafont::loadfonts(device="win")
## FORMATTING
windowsFonts("Roboto" = windowsFont("Roboto")) # font
windowsFonts("Roboto Black" = windowsFont("Roboto Black"))
windowsFonts("Roboto Light" = windowsFont("Roboto Light"))
windowsFonts("Roboto Medium" = windowsFont("Roboto Medium"))
windowsFonts("Roboto Thin" = windowsFont("Roboto Thin"))
theme_set(theme_bw(base_family="Roboto")) # ggplot2 default theme
setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # set working directory
## VARIABLES
google.location.data <- fromJSON(readLines("Location History.json"), flatten = TRUE)
google.locations <- google.location.data$locations
rm(google.location.data) # freeing up memory
Sys.setlocale("LC_TIME", "C")
google.locations$timekeeping <- as.POSIXct(as.numeric(google.locations$timestampMs)/1000, origin="1970-01-01")
google.locations$weekdays <- factor(format(google.locations$timekeeping, "%a"),
levels = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"))
date.diff <- max(google.locations$timekeeping) - min(google.locations$timekeeping)
measurements.per.hour <- (nrow(google.locations)/as.numeric(date.diff))/24 # number of measurements per hour
measurements.per.minute <- measurements.per.hour/60 # number of measurements per minute
measurement.interval <- 60/measurements.per.hour # number of minutes per measurement
## HEATMAP
ldf <- data.frame(t=rep(0,nrow(google.locations)))
ldf$t <- as.numeric(google.locations$timestampMs)/1000
class(ldf$t) <- 'POSIXct'
ldf$lat <- as.numeric(google.locations$latitudeE7/1E7)
ldf$lon <- as.numeric(google.locations$longitudeE7/1E7)
register_google(key = "...", # your Static Maps API key
account_type = "standard")
philly <- get_map(c(-75.1215556,40.0063906), zoom = 11, source='stamen',maptype="watercolor")
downtown <- get_map(c(-75.1643024,39.9529834), zoom = 13, source='stamen',maptype="watercolor")
ggmap(philly, darken = c(0.45, "white")) +
geom_point(data=ldf,aes(lon,lat),color = "RoyalBlue", alpha = .12, size = 1)
ggsave(file = "heatmap.png", width = 11, height = 7.5, units = "in",
dpi = 300)
## HOUR GRAPH
ggplot(google.locations) +
geom_line(aes(x=format(timekeeping, "%H"), y = (..count..)/(as.numeric(date.diff)/7),
group = weekdays,
color = weekdays),
stat = "count",
size = 1) +
geom_line(aes(x=format(timekeeping, "%H"), y = (..count..)/(as.numeric(date.diff)),
group = 1),
stat = "count",
linetype = 2,
size = 0.7) +
scale_color_manual("", values = c("#396AB1", "#DA7C30", "#3E9651", "#CC2529",
"#6B4C28", "#f781bf", "#6B4C9A")) +
labs(color = "Day of the Week",
title = "Hourly Overview of Google Location Data",
caption = "Based on personal Google Takeout data. Average taken over 2,215 days.") +
ylab("Number of Measurements per Day (Average)") +
xlab("Hour of the Day") +
theme(plot.title = element_text(family="Roboto Black"),
plot.subtitle = element_text(color="#666666", size = 13),
axis.title.x = element_text(family="Roboto"),
axis.title.y = element_text(family="Roboto"),
axis.text.x = element_text(family="Roboto", hjust=0),
axis.text.y = element_text(family="Roboto", hjust=0),
plot.caption = element_text(family="Roboto"),
legend.position = "right",
legend.title = element_text(family="Roboto"))
ggsave(file = "takeout.png", width = 11, height = 7.5, units = "in",
dpi = 300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.