The world’s leading publication for data science, AI, and ML professionals.

How to Create Strava #StatMaps Visualizations in R

Learn how to make beautiful interactive maps in R with GPS tracker data

Speed Map of Paradise Loop - Image by Author
Speed Map of Paradise Loop – Image by Author

Last month, Strava rolled out a simple, but fun new feature to its subscribers called #statmaps. Strava subscribers can now use a hashtag to enhance the map style of a given activity. The activity’s polyline is then displayed as a gradient of color based on the chosen attribute.

Strava #temperaturemap - Image screenshotted from Strava and stylized by author
Strava #temperaturemap – Image screenshotted from Strava and stylized by author

I think this is pretty neat, but I’m not a Strava premium subscriber. So, naturally, I wondered if I could replicate this in R for free with my raw Garmin data.

Download the starting file here and follow along for the journey!


Loading and Processing Garmin Data

I have a Garmin Edge 820 and it stores activity data in .fit files. I used a package called FITfileR to parse this file type. You’ll need to install this package via github using the remotes package. See package installation documentation here.

Let’s load in a trip from this weekend around Paradise Loop in Tiburon.

Sometimes activities are stored in multiple messages depending on how the ride is tracked, we’ll combine these messages into one data frame.

Garmin sends a GPS ping every second and stores location, speed, temperature, altitude, and distance. To get our visualization to work, we’ll be plotting a line between each one of those pings. The nextLat and nextLng vectors contain exactly that – the subsequent ping’s GPS coordinates. This will make our visualization work smoothly.

The table structure will look like this:


Generating a Speed Map

Paradise Loop Speed Map - Image by Author
Paradise Loop Speed Map – Image by Author

Click here for the interactive map

A #speedmap will change colors based on your speed. We’ll arrange all of the unique rounded speeds (148 observations) in this route in ascending order and line them up with a blue-purple-red gradient spread out across 148 hex values using colorRampPalette. Have fun playing around with the colors to personalize your visualization. Finally, we’ll join these values to the main ride_records table so that each row has a color value.

Now every speed value will have a corresponding hex value:

Finally, let’s use the powerful leaflet package to plot this route on a map. To get the map to render properly, we need to add a unique polyline between each GPS coordinate. With 3,811 rows in our data, we’re using a for loop to iterate through each row and plot each line separately with its corresponding color. Depending on how long the route is, this might take a some time to render. The following chunk took about 40 seconds to run on my computer.

And we get a beautiful interactive speed map!


Generating an Altitude Map

Paradise Loop Altitude Map - Image by Author
Paradise Loop Altitude Map – Image by Author

Click here for the interactive map

The code is very similar to what we covered above, except we’ll use the rounded altitude vector and change up the colors a bit.


Here are a few more maps I made with different GPS tracker attributes using the same process

San Francisco Butter Loop Temperature Map

I love this one because it really highlights the microclimates around SF

San Francisco Butter Loop Temperature Map - Image by Author
San Francisco Butter Loop Temperature Map – Image by Author

Click here for interactive map

Butter Loop Distance Map

Distance maps are nice to highlight the direction of a route.

San Francisco Butter Loop Distance Map - Image by Author
San Francisco Butter Loop Distance Map – Image by Author

Click here for interactive map

Hawk Hill Speed Map

Hawk Hill Speed Map - Image by Author
Hawk Hill Speed Map – Image by Author

Click here for interactive map


Conclusion

I hope you have fun making your own interactive maps with GPS tracker data. Try taking these maps to the next level by testing out different basemaps that give your visualization more contrast or adding in a legend to make the map even more useful for users.


Keep in touch!

LinkedIn | Medium | Website | Instagram


Related Articles