# clean the R workspace
rm(list = ls())
# load packages
require(tidyverse)
require(leaflet)
# read csv files of GPS coordinates of activities
runs_coordinates <- read.csv("./data/runs_bike_rides/runs_coordinates.csv")
iran_rides_coordinates <- read.csv("./data/runs_bike_rides/iran_rides_coordinates.csv")
milan_rides_coordinates <- read.csv("./data/runs_bike_rides/milan_rides_coordinates.csv")
brescia_rides_coordinates <- read.csv("./data/runs_bike_rides/brescia_rides_coordinates.csv")
free_walks_coordinates <- read.csv("./data/runs_bike_rides/free_walks_coordinates.csv")
free_walks_coordinates_rotterdam <- read.csv("./data/runs_bike_rides/walk_rotterdam_netherlands_coordinates.csv")
runs_coordinates_netherlands <- read.csv("./data/runs_bike_rides/runs_netherlands_coordinates.csv")
rides_coordinates_netherlands <- read.csv("./data/runs_bike_rides/rides_netherlands_coordinates.csv")
all_coordinates_dublin <- read.csv("./data/runs_bike_rides/dublin_coordinates.csv")


# in order to sty DRY (Don't Repeat Yourself) ! I am going to write a small function to call leaflet mapping functions and use it in following sections to avoid writing same code multiple times
my_leaflet_map <- function(activity_data) {
  leaflet() %>% 
    addTiles() %>% 
    addPolylines(lat = activity_data$latitude, 
                 lng = activity_data$longitude)
}

As title says clearly, in this short post I am trying to keep motivating myself! After sharing my bike rides or run paths visualized by R on a map on social media (like picture below), I have often got this question from my friends, why are you sharing them?



My answer has been:

You know how hard it is to ride a bike or run when it is cold outside?! I am trying to keep motivating myself! These posts are written, first to remind myself that it is doable, then to inspire my friends to move!

Besides above goal, it is a while I have intended to learn a better way to visualize routes and paths with GPS coordinates over maps, in a more interactive way and with possibility to be embedded in R Markdown docuements like the one you are reading now!

Before I was using geom_path from the amazing ggplot2 package, to connect the coordinates, (like picture above) recently I heard about leaflet package, and here you are going to see different maps of my bike rides, runs and free walking paths which are visualized with leaflet addPolylines.

In order to capture and export my rides, runs and walks GPS coordinates I am using Strava which gives .gpx format exports of all the activities. Then I use plotKML package to import those files in R and build data frames from them. Below I have used the csv exports of those data frames, since they include time, elevation and some other information that Strava exports, so I wouldn’t suggest you to share your gpx files publicly, as I am not doing here.

My bike rides

Bike rides in Milan, Italy

I was not riding much in Milan, it was a total of 5 rides. That was the first time I learned about GPS art (see here) and I created my master piece of Milan’s helmet! :D

my_leaflet_map(activity_data = milan_rides_coordinates)


Bike rides in Iran

For my summer vacations, I paid a two months visit to my country, Iran, after near 11 tough months on 2016. That was an opportunity to start riding! I only had 2 rides, but for longer time and paths compared to my normal to that date.

my_leaflet_map(activity_data = iran_rides_coordinates)


Bike rides in Brescia, Italy

Here is most of my bike rides which has happened in Brescia, in Italy. A total of 543 rides so far which is the most part of my 2,634.9 km total rides. You can zoom in to have better view of the rides (thanks to leaflet’s amazing visualizations!)

my_leaflet_map(activity_data = brescia_rides_coordinates)


My runs in Brescia, Italy

Here there is the places I have run, that have been mostly in Brescia, a total of 35 runs for a total distance of 168.7 km.

my_leaflet_map(activity_data = runs_coordinates)


My Free walks

At the end I have put the free walks paths. This includes places I have visited and walked around, like Verona, Trento, Ljubljana and Salo. A total of 31 walks. Remember to zoom in.

There is one thing that I am still trying to learn in leaflet, and that is how to draw multiple polylines without attaching the end point of last one to the start point of the next! In below visualization, if you zoom in, you will see that my walks in each of the places I mentioned above are connected to each other. I didn’t put time to separate them in different csv files as I have done for my bike rides above, but I need to figure out how to visualize them without having this problem, any suggestion would be very much welcomed.

my_leaflet_map(activity_data = free_walks_coordinates)


The Netherlands

Bike rides in Leiden

my_leaflet_map(activity_data = rides_coordinates_netherlands)


Runs in Leiden

my_leaflet_map(activity_data = runs_coordinates_netherlands)


Free walks

my_leaflet_map(activity_data = free_walks_coordinates_rotterdam)


First two weeks in Dublin (Runs and free walks)

my_leaflet_map(activity_data = all_coordinates_dublin)