Get startedGet started for free

Geospatial maps with leaflet

1. Geospatial maps with leaflet

We will now plot interactive geospatial maps using the leaflet package.

2. The structure of a leaflet plot

Let’s first import the leaflet library. The structure of a leaflet plot is very similar to ggplot, which is also made up of layers. The first layer required is leaflet, much like ggplot which initialises the visualisation with a blank canvas. addTiles adds the world map to the canvas, which is like the x and y axes on a ggplot plot. As you can see, new layers are added on using the pipe operator seen in dplyr, like a ggplot where new layers are added using the + notation.

3. Different map providers

We can also add addProviderTiles, which provides an alternative appearance to the map, on top of the default view, based on several providers with different design stylings. One example is MtbMap which contains popular maps for mountainbiking, which as seen here, is updated mostly in Europe. There are many examples. Another example is NatGeoWorldMap by the popular magazine national geographic. We can see natural landscapes depicted in such a map. The last is stamen toner, which provides high-contrast black and white maps that visualize water bodies like rivers and coastal areas.

4. Adding polygons in leaflet

We can add polygons, like the ones added earlier. In leaflet, this is achieved through addPolygon, where the main argument required is a sf object. In this case, we have added a multipolygon called london_poly. Here, we can change various attributes, like color, and label. We have set the label to the Name attribute of the sf object.

5. Adding polylines in leaflet

Next, we shall add polylines for the paths that we plotted earlier. We will need to provide the longitudes and latitudes of these paths. This information can be extracted from an sf object by looking at its geometry attribute, and then indexing it by 1. Let's do this for each of the paths, and call these loop_geo and capital_geo. The path is plotted using addPolylines. See that the longitudes and latitudes of each path are obtained by slicing each geometry set to get the first and second columns. We have stored this as m2.

6. Adding markers in leaflet

We can add different types of markers. One example is that of circle markers, which can be added using addCircleMarkers that takes in points. This produces a circle for each Airbnb listing in listings_geo. Next is addMarkers, which adds regular markers. Its arguments are the same as addCircleMarkers.

7. markerClusterOptions

In each marker function, the markerClusterOptions function can be added. This allows us to show aggregated values, like the number of listings in a region, now without having to further process the data. See how the numbers change as we zoom in and out.

8. User selection

Let's add a addLayersControl layer which allows users inputs. We can set baseGroups to allow users to toggle between different map providers. To link these controls to each provider tile, we need to label each provider tile and set baseGroups as a vector containing these labels. Likewise, we can set overlayGroups which contains various layers. This allows users to toggle layers on and off. To link these to the different layers, we need to label each layer and set overlayGroups as a vector containing these labels.

9. User selection, rendered

Now that these providers and layers are linked up, we can toggle between different providers. If we like, we can also choose to select only some of the layers to focus on just a few.

10. Let's practice!

Now it's your turn to practice with leaflet!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.