Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
180 lines (155 sloc) 10.1 KB
title author date output
Visualizing data with R
David Haberthür
15 January 2016
keep_md toc


I tracked my location data with OpenPaths since the beginning of 2014. OpenPaths comes as an application for your phone, which tracks its location, uploads it to the OpenPath servers. You can then donate your data for scientific research and also look at the data yourself, which is what we do here.

To be able to do this, we grab a .CSV file with the location data. Log in to OpenPaths, and click on CSV under Download my data, which gives you a comma separated list of your location data, which can then visualize with R, which is what we've done here.


We want to plot the location points on a map, which we can do with the wonderful ggmap library. To get the place names from the GeoNames Web Services, we need the RCurl library, to parse the output XML, we obviously need the XML library.

First, we load the data file and then remove all the datapoints where we have an altitudes of '0' (which is probably a fluke in the GPS data) Obviously, we only want to look at this years data, we thus save a subset of the dataset for further processing.

data <- read.csv("/Users/habi/Dev/R/openpaths_habi.csv")
data$alt[data$alt == 0] <- NA
whichyear <- 2016
thisyear <- subset(data, grepl(whichyear, data$date))
geoname <- function(lat,lon){
  # Grab GeoNames XML from their API, according to location
  txt = getURL(paste0("", lat, "&lng=", lon, "&username=habi", collabse = NULL), .encoding = 'UTF-8', .mapUnicode = TRUE)
  # Parse XML tree
  xmldata <- htmlTreeParse(txt, asText=TRUE)
  # Extract <name> node (with empirically found location)
  Name <- xmldata$children[[2]][[1]][[1]][[1]][[2]][[1]]
  # Since we're only using the name as string, we can return it as such

Then, we display a summary of the geographical points.


In r whichyear I was in the mean [somewhere close to r geoname(mean(thisyear$lat), mean(thisyear$lon))](`r mean(thisyear$lat)/r mean(thisyear$lon)). I've lived atr round(mean(thisyear$alt, na.rm=TRUE),1)` m AMSL in the mean.

The .csv file also contains information about the iPhone we've used to collect the data. Let's look at these.


We see that in r whichyear I have only used one phone, my iPhone 5S (iPhone6,2) and went through r sum(summary(thisyear$os) != 0) different iOS version numbers. If we assume that I've tracked my location consistently, then I've used iOS r names(summary(thisyear$os)[which.max(summary(thisyear$os))]) the longest, with r max(summary(thisyear$os)) saved data points.

Location data


Interesting points in my yearly location data:

  • The minimal and maximal latitudes of r round(min(thisyear$lat),3) and r round(max(thisyear$lat),3), South and North respectively.
  • the minimal and maximal longitudes of r round(min(thisyear$lon),3) to r round(max(thisyear$lon),3), East and West respectively.
  • as well as the altitude, which ranges from r round(min(thisyear$alt, na.rm=TRUE),1) m AMSL to r round(max(thisyear$alt, na.rm=TRUE),1) m AMSL.

We can get the extreme points out of the data pretty easily. To do so, we subset the data depending on the value we want to have, construct a Location from these points, grab the map tile from that location, put a marker on top of it and display this is an image.

For the northmost point, this goes like so:

NLocation = c(lon=subset(thisyear, lat==max(thisyear$lat))$lon[1],
              lat=subset(thisyear, lat==max(thisyear$lat))$lat[1])
mapImage<- get_map(location= NLocation, source='stamen', maptype='toner-hybrid', zoom=10)
ggmap(mapImage) +
  geom_point(aes(x = subset(thisyear, lat==max(thisyear$lat))$lon[1],
                 y = subset(thisyear, lat==max(thisyear$lat))$lat[1]),
             alpha = .125,
             size = 10) +
  ggtitle(paste("Northmost point in", whichyear))

The northmost point in r whichyear is in [r geoname(NLocation[c(2)], NLocation[c(1)])](`r NLocation[c(2)]/r NLocation[c(1)]), which is true. Even though I've not been inr geoname(NLocation[c(2)], NLocation[c(1)])` exactly, my mobile phone was probably connected to a tower there. I spent two days in Ingelheim in January, at the International Masters competition in Swimming, which is close by.

The rest of the extremes can be extracted accordingly.

ELocation = c(lon=subset(thisyear, lon==max(thisyear$lon))$lon[1],
              lat=subset(thisyear, lon==max(thisyear$lon))$lat[1])
mapImage<- get_map(location= ELocation, source='stamen', maptype='toner-hybrid', zoom=10)
ggmap(mapImage) +
  geom_point(aes(x = subset(thisyear, lon==max(thisyear$lon))$lon[1],
                 y = subset(thisyear, lon==max(thisyear$lon))$lat[1]),
             alpha = .125,
             size = 10) +
    ggtitle(paste("Most eastern point in", whichyear))

SLocation = c(lon=subset(thisyear, lat==min(thisyear$lat))$lon[1],
              lat=subset(thisyear, lat==min(thisyear$lat))$lat[1])
mapImage<- get_map(location= SLocation, source='stamen', maptype='toner-hybrid', zoom=10)
ggmap(mapImage) +
  geom_point(aes(x = subset(thisyear, lat==min(thisyear$lat))$lon[1],
                 y = subset(thisyear, lat==min(thisyear$lat))$lat[1]),
             alpha = .125,
             size = 10) +
    ggtitle(paste("Southmost point in", whichyear))

WLocation = c(lon=subset(thisyear, lon==min(thisyear$lon))$lon[1],
              lat=subset(thisyear, lon==min(thisyear$lon))$lat[1])
mapImage<- get_map(location= WLocation, source='stamen', maptype='toner-hybrid', zoom=10)
ggmap(mapImage) +
  geom_point(aes(x = subset(thisyear, lon==min(thisyear$lon))$lon[1],
                 y = subset(thisyear, lon==min(thisyear$lon))$lat[1]),
             alpha = .125,
             size = 10) +
    ggtitle(paste("Most western point in", whichyear))

The most eastern point was in the [r geoname(ELocation[c(2)], ELocation[c(1)])](`r ELocation[c(2)]/r ELocation[c(1)]), during the first vacation with our (then) newborn daughter in the Bregenzer Wald. The most southern point was in [r geoname(SLocation[c(2)], SLocation[c(1)])]( SLocation[c(2)]/r SLocation[c(1)]), while skiing in Zermatt. The most western point was in [r geoname(WLocation[c(2)], WLocation[c(1)])]( WLocation[c(2)]/r WLocation[c(1)]`), during a meeting at the EPFL in Lausanne for working on the GlobalDiagnostiX project.

HighestLocation = c(lon=subset(thisyear, alt==max(thisyear$alt, na.rm=TRUE))$lon[1],
                    lat=subset(thisyear, alt==max(thisyear$alt, na.rm=TRUE))$lat[1])
mapImage<- get_map(location= HighestLocation, source='stamen', maptype='toner-hybrid', zoom=10)
ggmap(mapImage) +
  geom_point(aes(x = subset(thisyear, alt==max(thisyear$alt, na.rm=TRUE))$lon[1],
                 y = subset(thisyear, alt==max(thisyear$alt, na.rm=TRUE))$lat[1]),
             alpha = .125,
             size = 10) +
    ggtitle(paste("Highest point in", whichyear))

LowestLocation = c(lon=subset(thisyear, alt==min(thisyear$alt, na.rm=TRUE))$lon[1],
                    lat=subset(thisyear, alt==min(thisyear$alt, na.rm=TRUE))$lat[1])
mapImage<- get_map(location= LowestLocation, source='stamen', maptype='toner-hybrid', zoom=10)
ggmap(mapImage) +
  geom_point(aes(x = subset(thisyear, alt==min(thisyear$alt, na.rm=TRUE))$lon[1],
                 y = subset(thisyear, alt==min(thisyear$alt, na.rm=TRUE))$lat[1]),
             alpha = .125,
             size = 10) +
    ggtitle(paste("Closest to sealevel in", whichyear))

The highest point in r whichyear at r round(max(thisyear$alt, na.rm=TRUE),1) m AMSL was close to r geoname(HighestLocation[c(2)], HighestLocation[c(1)]) on the [Furgsattel](`r HighestLocation[c(2)]/r HighestLocation[c(1)]). The lowest point atr round(min(thisyear$alt, na.rm=TRUE),1)m AMSL was inr geoname(LowestLocation[c(2)], LowestLocation[c(1)]), [close to Ingelheim]( LowestLocation[c(2)]/r LowestLocation[c(1)]`).

Where was I in Switzerland?

To plot the obtained data on a map, we have to center the resulting map location. Since I only want to show the data points in Switzerland, we center the map on that. Afterwards, we can simply plot all the r whichyear geolocation points on top of that image, and you can see where I was in Switzerland in r whichyear.

HomeBase <- get_map(location= 'Switzerland', source = 'stamen', maptype='toner-hybrid', zoom=8)
AllPoints <- data.frame(lon=thisyear$lon, lat=thisyear$lat)
ggmap(HomeBase) + geom_point(aes(x = lon, y = lat),
                             data = AllPoints,
                             size = 3,
                             alpha = 0.125,
                             color="darkred") +
    ggtitle(paste("Where was Habi in Switzerland in", whichyear))