Visualising spatial data using sf and mapdeck - part four

This is the final post in my series on visualing spatial data. Here I’ll continute working with a dataset with the GPS locations of migrating birds (from here) to show how you can use mapdeck’s trips layer to effectively visualise movement over time.

Visualising the journey of individual birds over time using the trips layer

In all these visualisation of the data I’ve shown in my previous posts, the time information is essentially lost and we don’t get a good sense of the birds’ movements over time. However, the trips layer in Mapdeck is an excellent way to capture this.

For this we just need the XYZM linestrings made in the previous post. These linestrings join together the positions each bird was recorded at in temporal order, allowing us to trace its position over time.

Note that this layer does require having XYZM linestrings, but if you only have coordinates and timestamps you can create a z column in data.table and set it to 0. You will also need to play around a bit with animation_speed and trail_length to find parameters that makes sense for what you’re plotting.

    style = mapdeck_style("dark"),
    location = c(0, 35),
    zoom = 2,
    pitch = 35
    ) %>%
        data = sf_harriers,
        stroke_colour = "animal-nickname",
        animation_speed = 2000,
        trail_length = 5000,
        opacity = 1,
        stroke_width = 2,
        legend = TRUE,
        legend_options = (list(title = "Bird name")),

As you can see, the trips layer shows the movement of the harriers much more elegantly than the previous maps I’ve shown. You can see clearly that they spend most of the time in Europe (as we also saw with the heatmap layer) but you can also see the route they take as they migrate to and from Africa (as we saw using the path layer).

Other things to explore

In this example I’ve talked about only a small part of what you can do with both sf and mapdeck. Mapdeck can be used with Shiny and has more layers you can use to visualise your data, as well as more options to customise the look of maps than I’ve talked about here. The sf library has a ton of useful functions to explore. In particular, I haven’t talked at all about operations you can perform using spatial data. For example, you can do things like identify all points that fall within a particular polygon or identify which features fall within a certain distance of another. While my focus here has been on visualising spatial data, manipulating spatial data in this way is also very powerful. There are a lot of resources available online to delve into this, and below I’ve included links to some I’ve found particularly helpful.

Useful resources