Last active
January 4, 2016 02:32
-
-
Save tysonwepprich/d46fa7051f28cdfbf752 to your computer and use it in GitHub Desktop.
Maps activity data from Strava .gpx files for runs, rides, and walks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# maps of my Strava data | |
# code modified from Xavi Gimenez | |
# http://www.xavigimenez.net/blog/2015/04/mapping-3-years-of-running-workouts/ | |
library(plotKML) | |
library(ggplot2) | |
library(ggmap) | |
library(stringr) | |
library(lubridate) | |
library(dplyr) | |
# setwd("C:/Users/Tyson/Desktop/StravaData") | |
# read downloaded .gpx files from your Stava profile | |
# assuming your working directory only has the data files | |
files = list.files() | |
index <- c() | |
latitude <- c() | |
longitude <- c() | |
date <- vector(mode = "character", length = length(files)) | |
unk <- vector(mode = "character", length = length(files)) | |
act <- vector(mode = "character", length = length(files)) | |
# this takes some time! | |
for (i in 1:length(files)) { | |
route <- readGPX(files[i]) | |
location <- route$tracks[[1]][[1]] | |
index <- c(index, rep(i, dim(location)[1])) | |
latitude <- c(latitude, location$lat) | |
longitude <- c(longitude, location$lon) | |
date[i] <- substr(files[i], 1, 8) | |
unk[i] <- substr(files[i], 10, 15) | |
act[i] <- substr(files[i], 17, 20) | |
} | |
routes <- data.frame(cbind(index, latitude, longitude)) | |
metadata <- data.frame(cbind(1:length(files), date, unk, act)) | |
names(metadata)[1] <- "index" | |
dat <- merge(routes, metadata, by = "index") | |
dat$lubridate <- ymd(dat$date) | |
saveRDS(dat, file = "stravaData.rds") | |
unique(dat$act) | |
bikedat <- dat %>% | |
filter(act == "Ride") | |
walkdat <- dat %>% | |
filter(act == "Walk") | |
rundat <- dat %>% | |
filter(act == "Run.") | |
# get maps for my location | |
BikeClosemap <- get_map(location = "St. Mary's and Peace, Raleigh, NC", | |
zoom = 13, color="bw") | |
BikeFarmap <- get_map(location = "New Bern and I-440, Raleigh, NC", | |
zoom = 11, color="bw") | |
Runmap <- get_map(location = "Centennial and Nazareth, Raleigh, NC" , | |
zoom = 13, color="bw") | |
Walkmap <- get_map(location = c(lon = -78.659447, lat = 35.775064), | |
zoom = 15, color="bw") | |
# make activity plots overlaid on maps | |
colors = c("#de2d26", "#ffa500", "#ff0000", "#00ffff") | |
outBikeClose <- ggmap(BikeClosemap) + geom_path(aes(x = longitude, y = latitude, | |
group = factor(index)), colour=colors[1], | |
data = bikedat, alpha=0.5) + ggtitle("Biking close up") | |
outBikeFar <- ggmap(BikeFarmap) + geom_path(aes(x = longitude, y = latitude, | |
group = factor(index)), colour=colors[1], | |
data = bikedat, alpha=0.5) + ggtitle("Biking") | |
outRun <- ggmap(Runmap) + geom_path(aes(x = longitude, y = latitude, | |
group = factor(index)), colour=colors[1], | |
data = rundat, alpha=0.5) + ggtitle("Running") | |
outWalk <- ggmap(Walkmap) + geom_path(aes(x = longitude, y = latitude, | |
group = factor(index)), colour=colors[1], | |
data = walkdat, alpha=0.5) + ggtitle("Walking dog") | |
# make image files of plots | |
png(filename="bikeclose.png", width = 1200, height = 800, units = "px") | |
outBikeClose | |
dev.off() | |
png(filename="bike.png", width = 1200, height = 800, units = "px") | |
outBikeFar | |
dev.off() | |
png(filename="run.png", width = 1200, height = 800, units = "px") | |
outRun | |
dev.off() | |
png(filename="walk.png", width = 1200, height = 800, units = "px") | |
outWalk | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment