Skip to content

Instantly share code, notes, and snippets.

@johnschrom
Created May 8, 2014 23:59
Show Gist options
  • Save johnschrom/84d513fdca8fdcde8ebf to your computer and use it in GitHub Desktop.
Save johnschrom/84d513fdca8fdcde8ebf to your computer and use it in GitHub Desktop.
Map Moves Data
library(ggplot2)
library(maps)
library(mapproj)
# Thanks to Ernesto at Quantified Self
# http://quantifiedself.com/2014/03/map-moves-data/
# This is set up take data from the "Tracks" export, using:
# https://labs.traqs.me/moves/
data <- read.csv('moves//tracks.csv')
# For me (at least), the colnames weren't in the right order in the data file:
colnames(data) <- c('date', 'time', 'tz.offset', 'lat', 'lng')
geo <- data[,c('lat', 'lng')]
###############################################################################
# United States
tmp <- mapproject(list(y=geo$lat,x=geo$lng))
geo.albers <- data.frame('x'=tmp$x, 'y'=tmp$y)
states <- map_data("usa",project="albers",par=c(39,45))
p <- ggplot()
p <- p + geom_polygon(data=states, aes(x=long, y=lat, group=group),
colour="#333333", fill="#111111", alpha=0.1)
p <- p + theme( panel.grid.major = element_line(colour=NA),
panel.grid.minor = element_line(colour=NA),
panel.background = element_rect(fill="black",
colour=NA),
axis.ticks = element_line(colour=NA),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank());
p <- p + geom_point(data=geo.albers, aes_string(x='x', y='y'),
colour='#008C9E', size=1)
plot(p)
###############################################################################
# Minneapolis
geo.mpls <- geo[which((geo$lat < 45.1) & (geo$lat > 44.82) &
(geo$lng < -93.15) & (geo$lng > -93.43)),]
p <- ggplot()
p <- p + theme( panel.grid.major = element_line(colour=NA),
panel.grid.minor = element_line(colour=NA),
panel.background = element_rect(fill="black",
colour=NA),
axis.ticks = element_line(colour=NA),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank());
p <- p + geom_point(data=geo.mpls, aes_string(x='lng', y='lat'),
colour='#008C9E', size=1, alpha=0.2)
plot(p)
###############################################################################
# SF Bay Area
geo.sf <- geo[which((geo$lat < 38) & (geo$lat > 37.2) &
(geo$lng < -121.8) & (geo$lng > -123)),]
p <- ggplot()
p <- p + theme( panel.grid.major = element_line(colour=NA),
panel.grid.minor = element_line(colour=NA),
panel.background = element_rect(fill="black",
colour=NA),
axis.ticks = element_line(colour=NA),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank());
p <- p + geom_point(data=geo.sf, aes_string(x='lng', y='lat'),
colour='#008C9E', size=1, alpha=0.2)
plot(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment