In this quick post, we’ll take one MTB ride, tracked by FitBit in a TXC File, and generate a animated gif. Using gganimate Package and using the same code we learned, we can animate the map with few words.
Reading TCX File
We already saw to how read and GPS track information stored in a TXC/GPX file, once it’s just an XML File.
Also we saw how is easy to plot the track over a map using ggmap package.
1
2
3
4
5
6
7
8
9
10
11
# getting the map backgroubd bbox<-make_bbox(lon=track$lon,lat=track$lat,f=.1)gmap<-get_map(location=bbox,maptype="terrain",source="google")# base plotggmap(gmap)+geom_path(data=track,mapping=aes(lon,lat),color="red",alpha=1,size=0.8,lineend="round")+coord_fixed()+theme_void()+theme(legend.position="none")
Animating the Map
Now, with a little more code, we can use the gganimate Package to create a animated gif version of this plot.
gganimate plotting a series of ggplots and put them together in a gif (or other format) using ImageMagick. Two aesthetics keywords in the ggplot2 grammar are in charge to control how the individual charts will be gerated: frame and cumulative. The first indicate which feature in the data frame is the “time dimention” and the other controls if the plot will be incremental (from a “frame” to “frame”) or cumulative (from “beginning” to the “current frame”).
# lets make a frame each 3 minutes# to not destroy the track info, we collapse the data on each 3 minutestrack%>%mutate(dt=floor_date(dt,"3 minutes"))->track# base plotggmap(gmap)+# cumulative layer, the "whole path" along the time (dt)geom_path(data=track,mapping=aes(lon,lat,frame=dt,cumulative=T),color="yellow",alpha=1,size=0.8,lineend="round")+# the "instant" plot, the 3 minutes path in the frame (dt)geom_path(data=track,mapping=aes(lon,lat,frame=dt,cumulative=F),size=1.2,lineend="round",color="red")+coord_fixed()+theme_void()+theme(legend.position="none")->pp<-gganimate(p,interval=0.01,ani.width=400,ani.height=400,filename="11654237848.gif")