Thank you for travelling with Deutsche Bahn –
I was searching for open data on the internet and found some interesting datasets on the website of the Deutsche Bahn. They provide a huge dataset (~ 2 GB) of Wifi access of their ICE fleet on three days in 2017. This dataset can be found here. The dataset contains coordinates of the trains every 5 seconds and how many users were connected to the router, the number of available satellites and some other indicators.
My goal was to animate the route of all available trails for one day and show the number of available satellites, which will be the size of the dot on the map. The bigger the plotted point the more satellites are available.
I’m interested in trying new things and so I decided to analyse this data set and make some (hopefully) nice visualisations using R and some handy packages: tidyverse, especially dplyr and ggplot2 and some (for me) new packages: gganimate and ggmap. They are wonderful when combining them .. you’ll see!
To see my full code, see my GitHub Gist. The main aspect of this post is the visualisation of data, not the data analysis.
I had to remove the last line because it just contains “NA”. The file surveyor_hackathon_data_20171212.csv can be found when downloading and unzipping this file.
Now I just select one day (8th March 2017) and order the date in increasing order.
Now to the most interesting parts: First of all, we load a map of interest, in our case a map of Germany. I decided to choose Google as source, but ggmap also offers other providers, as OSM, Stamen Maps or CloudMade.
Next we use ggplot2 and ggmap to create an object p with different layers. This is the great concept of ggplot2. Most interesting is the mapping aesthetic frame. This will be important when combining different plots to an animation with gganimate. In this case it is a kind of loop over the time. I highly recomment the example site of gganimate. The rest of the code is just improving the look of the plot.
Now we have created the object p and at least we convert it to an animation. For that purpose the function gganimate is used. Important is the argument interval which makes the animation faster (when decreasing) or slower (when increasing). The argument title_frame was set to FALSE because with geom_text() in the code snippet before I included an text element by hand.
Finally the result: Note that you can change the filename to “some-filename.mp4” to create a mp4 file.
I hope you like the final results and see how easy it is with R to create this kind of animations! Special thanks to the creators and maintainers of the used packages.