If, like me, you are a runner is quite likely you use any of the multiple apps to get track of your workouts. If you want to plot your activities all together is quite easy thanks this post on Flowing Data blog. A more recent post from Visual Cinnamon blog shows also how to add a map overlay, so I decided to do a quick try in order to plot my running tracks and proudly gaze at my humble workouts.

I have all my activity tracked in Strava, at least the last 3 years (for previous years I was using another service, which was closed suddenly without having the chance to retrieve my data, too bad). Anyway, Strava has a nice feature, a bulk activity export so all your data is available.

With my data ready to be used, I just added some slightly modifications based on the code in R from Visual Cinnamon post, as I wanted to differentiate workouts from a year to another, so here it is the code:

library(plotKML)
library(ggplot2)
library(ggmap)


#generate a list with available years from data
years = c(2012:2014)
files = vector(mode="list", length=length(years))
names(files) = years

#read files from each year's folder
for (i in years)
files[[i]]=dir(path=paste(as.character(i),"/",sep=""),pattern="\\.gpx")

#routes list to store all the tracks by year
routes = vector(mode="list", length=length(years))
names(routes) = years


#store all routes in a data frame, by year
index <- c()
latitude <- c()
longitude <- c()
for (y in years) {
	for (i in 1:length(files[[y]])) {
	     
	     route <- readGPX(files[[y]][i])
	     location <- route$tracks[[1]][[1]]
	     
	     index <- c(index, rep(i, dim(location)[1]))
	     latitude <- c(latitude, location$lat)
	     longitude <- c(longitude, location$lon)
	 }
	routes[[y]] <- data.frame(cbind(index, latitude, longitude))
}

At this point in time we can generate the plot for all the workouts, as shown below:

tracks20122015

I just want to plot workouts from my town, so I just center the map to that location (data also contains tracks from different races, but I have discarded them because I would need to zoom out the map to show a wider geographical area, and my interest is only on my local workouts):

#get map from my location
mymap <- qmap(location = "Sant Vicenç dels Horts", 
zoom = 13, color="bw", legend="none")

And finally add a layer with the paths, so we can plot together map and tracks, once per year:

colors = c("#ffff00", "#ff0000", "#00ffff")
for (i in 1:length(years)
  mymap + geom_path(aes(x = longitude, y = latitude, 
  group = factor(index)), colour=colors[i], 
  data = routes[[years[i]]], alpha=0.5)

tracks_year_2014

tracks_year_2013

tracks_year_2012

That’s it! A quick, easy and elegant way to plot routes in R. From a personal point of view it’s interesting to see through this 3 years span, routes that I used to go which are replaced by new ones, or just to check that my knowledge about trails around my town is increasing year after year (I always try to avoid the same routes in my workouts).

Posted in Data Visualization, Maps, R Tags: ,

Have your say

About me

Data Visualization · Interactive · HCI · Open Data · Data Science · Digital Humanities


More info here and here: