Runkeeper Map

# Load data
routes <- read.csv("~/Dropbox/Data Science/FlowingData/runkeeper-routes-sf.csv", 
    stringsAsFactors = FALSE)

# View data frame
routes[1:10, ]
##    tempid latitude longitude
## 1       1    37.79    -122.4
## 2       1    37.79    -122.4
## 3       1    37.80    -122.4
## 4       1    37.80    -122.4
## 5       1    37.79    -122.4
## 6       1    37.79    -122.4
## 7       2    37.80    -122.4
## 8       2    37.81    -122.4
## 9       2    37.80    -122.4
## 10      2    37.79    -122.4

# Basic points plot
plot(routes$latitude, routes$longitude, type = "n")
points(routes$latitude, routes$longitude, pch = 20, cex = 0.3, col = "#000000")

plot of chunk unnamed-chunk-1


# Basic lines plot
plot(routes$latitude, routes$longitude, type = "n")
routeIds <- unique(routes$tempid)
for (i in 1:length(routeIds)) {
    currRoute <- subset(routes, tempid == routeIds[i])
    lines(currRoute$latitude, currRoute$longitude)
}

plot of chunk unnamed-chunk-1


# Use map projections
library(mapproj)
## Loading required package: maps

# Albers projection
locProj <- mapproject(routes$latitude, routes$longitude, "albers", par = c(37, 
    37.5))
routes$latproj <- locProj$x
routes$lonproj <- locProj$y

# Map the projected points
plot(routes$latproj, routes$lonproj, type = "n", asp = 1, axes = FALSE, xlab = "", 
    ylab = "")
for (i in 1:length(routeIds)) {
    currRoute <- subset(routes, tempid == routeIds[i])
    lines(currRoute$latproj, currRoute$lonproj)
}

plot of chunk unnamed-chunk-1


# Azimuthal, for kicks and giggles
locProj <- mapproject(routes$latitude, routes$longitude, "azequidistant")



# Aesthetics

# Rectangular
locProj <- mapproject(routes$latitude, routes$longitude, "rectangular", par = 38)
routes$latproj <- locProj$x
routes$lonproj <- locProj$y

# With transparency and thinner lines
par(mar = c(0, 0, 0, 0))
plot(routes$latproj, routes$lonproj, type = "n", asp = 1, axes = FALSE, xlab = "", 
    ylab = "")
for (i in 1:length(routeIds)) {
    currRoute <- subset(routes, tempid == routeIds[i])
    lines(currRoute$latproj, currRoute$lonproj, col = "#00000020", lwd = 0.4)
}

plot of chunk unnamed-chunk-1



# Varied line width, consistent 20 transparency
par(mfrow = c(2, 3))
for (m in 0:5) {

    lineWidth <- 0.1 + 0.5 * m
    plot(routes$latproj, routes$lonproj, type = "n", asp = 1, axes = FALSE, 
        xlab = "", ylab = "")
    for (i in 1:length(routeIds)) {
        currRoute <- subset(routes, tempid == routeIds[i])
        lines(currRoute$latproj, currRoute$lonproj, col = "#00000020", lwd = lineWidth)
    }
}

plot of chunk unnamed-chunk-1



# Consistent line width of 0.5, varied transparency
par(mfrow = c(2, 3))
for (m in 1:6) {

    lineTrans <- round(m * 0.16 * 100)
    lineCol <- paste("#000000", lineTrans, sep = "")
    plot(routes$latproj, routes$lonproj, type = "n", asp = 1, axes = FALSE, 
        xlab = "", ylab = "")
    for (i in 1:length(routeIds)) {
        currRoute <- subset(routes, tempid == routeIds[i])
        lines(currRoute$latproj, currRoute$lonproj, col = lineCol, lwd = 0.5)
    }
}

plot of chunk unnamed-chunk-1



# Varied colors per map
par(mfrow = c(2, 3))
mapCols <- c("#39e436", "#6e0ea9", "#d47627", "#0d5bc0", "#de0f07", "#9a5130")
for (m in 1:6) {
    lineCol <- paste(mapCols[m], 20, sep = "")
    plot(routes$latproj, routes$lonproj, type = "n", asp = 1, axes = FALSE, 
        xlab = "", ylab = "")
    for (i in 1:length(routeIds)) {
        currRoute <- subset(routes, tempid == routeIds[i])
        lines(currRoute$latproj, currRoute$lonproj, col = lineCol, lwd = 0.5)
    }
}

plot of chunk unnamed-chunk-1




# Varied colors per route
library(scales)  # For transparency
pickColor <- function() {

    # Pick a random color
    i <- sample(1:657, 1)
    theColor <- alpha(colors()[i], 0.3)
    return(theColor)
}

par(mar = c(0, 0, 0, 0))
plot(routes$latproj, routes$lonproj, type = "n", asp = 1, axes = FALSE, xlab = "", 
    ylab = "")
for (i in 1:length(routeIds)) {
    currRoute <- subset(routes, tempid == routeIds[i])
    lines(currRoute$latproj, currRoute$lonproj, col = pickColor(), lwd = 0.5)
}




# Introduce base map
library(ggmap)
## Loading required package: ggplot2
detailMap <- function(bbox, thedata) {
    basemap <- get_map(location = bbox, source = "google", maptype = "terrain", 
        color = "bw")
    ggmap(basemap) + geom_path(aes(x = longitude, y = latitude, group = tempid), 
        size = 0.3, color = "#570864", alpha = 0.3, data = thedata)
}
sanfran <- c(-122.50476977784, 37.7052809034877, -122.361947512215, 37.8382579402706)
detailMap(sanfran, routes)
## Warning: bounding box given to google - spatial extent only approximate.
## converting bounding box to center/zoom specification. (experimental)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=37.771769,-122.433359&zoom=13&size=%20640x640&scale=%202&maptype=terrain&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Warning: Removed 99 rows containing missing values (geom_path).

plot of chunk unnamed-chunk-1