This is the third post in my series on getting started visualising spatial data. In this post I’ll continue to explore different ways of visualising spatial data using a dataset with the GPS locations of migrating birds (from here).
In the last post I simply plotted each point corresponding to the position of a single bird (Ben) over time, which was a good start. To see where Ben spends the most time, we can easily use exactly the same data to create a heatmap.
mapdeck( style = mapdeck_style("satellite"), location = c(0, 35), zoom = 2 ) %>% add_heatmap( data = sf_harriers_ben, update_view = FALSE )
The heatmap layer gives us useful information that the points alone do not - this shows where Ben has spent the most time. The highest density of points occurs at the border of Belgium and the Netherlands and we also see some concentration of points near the border of Mauritania and Mali - the locations that Ben migrates between.
As was the case for the map in the previous post, you can zoom in and move around different parts of the map. The heatmap will readjust, so what looks like a single concentrated area in Africa in the zoomed out view resolves into a number of smaller areas as you zoom in.
Using mapdeck multiple layers can be shown together, so we can plot the scatterplot layer from the previous post and the heatmap layer together:
mapdeck( style = mapdeck_style("satellite"), location = c(0, 35), zoom = 2 ) %>% add_scatterplot( data = sf_harriers_ben, fill_colour = "#000000", update_view = FALSE ) %>% add_heatmap( data = sf_harriers_ben, update_view = FALSE )
Be careful if plotting multiple layers of the same type - in that case you must specify the layer_id argument or they won’t display properly.
Another way to visualise the route the birds have taken is by joining the points together to form a linestring (one of the main types in sf). There are a couple of ways to do this from the original data.table I made in the previous post (or from an existing sf object), but here I’ll use
sfheaders::sf_linestring from the
The main thing to remember when using
sfheaders::sf_linestring is that the points must be ordered. In this case, there are multiple birds and we want each to have it’s own linestring, so we specify
animal-nickname for linestring-id. Note that here I’m creating a XYZM linestring, though for many purposes all you’ll need is an XY linestring. However, an XYZM linestring will be necessary for the trips layer (coming up in the next post).
dt_harriers <- dt_harriers[`animal-nickname` %in% c("Ben","Peter")] setorder( dt_harriers, `animal-nickname`, time ) sf_harriers <- sfheaders::sf_linestring( obj = dt_harriers, x = "location-long", y = "location-lat", z = "height-above-msl", m = "time", linestring_id = "animal-nickname" ) st_crs(sf_harriers) = 4326
Now we can plot all the linestrings together on the map, using stroke_colour to distinguish the paths of the different birds and adding a legend.
mapdeck( style = mapdeck_style("dark"), pitch = 35, location = c(0, 35), zoom = 2 ) %>% add_path( data = sf_harriers, stroke_colour = "animal-nickname", legend = TRUE, legend_options = (list(title = "Bird name")), palette = colourvalues::get_palette("viridis")[150:256, ], update_view = FALSE )
The paths layer makes using of the elevation data, which you can see most clearly if you zoom in to the the border of Belgium and the Netherlands. I’ve changed the pitch to make this clearer and also used the “dark” style to change the look of the map. In some cases we see multiple lines back and forth for the same bird because this dataset covers several years.
In the next post, I’ll show how we can use mapdeck’s trips layer to create a great visualisation of the birds migrating.