Using leaflet, just because

I love it when researchers take the time to share their knowledge of the computational tools that they use. So first, let me point you at Environmental Computing, a site run by environmental scientists at the University of New South Wales, which has a good selection of R programming tutorials.

One of these is Making maps of your study sites. It was written with the specific purpose of generating simple, clean figures for publications and presentations, which it achieves very nicely.

I’ll be honest: the sole motivator for this post is that I thought it would be fun to generate the map using Leaflet for R as an alternative. You might use Leaflet if you want:

  • An interactive map that you can drag, zoom, click for popup information
  • A “fancier” static map with geographical features of interest
  • concise and clean code which uses pipes and doesn’t require that you process shapefiles

Without further ado:

library(leaflet)
library(readr)
point_data <- read_csv("http://environmentalcomputing.net/wp-content/uploads/2018/03/point_data.csv")

leaflet(point_data) %>% 
  fitBounds(lng1 = min(point_data$lon) - 0.11, 
            lat1 = min(point_data$lat) - 0.11,
            lng2 = max(point_data$lon) + 0.11, 
            lat2 = max(point_data$lat) + 0.11) %>% 
  addCircleMarkers(~lon, ~lat,
                   radius = 3,
                   opacity = 100,
                   color = "white", 
                   label = as.character(point_data$Site), 
                   labelOptions = labelOptions(noHide = TRUE, 
                                               textOnly = TRUE, 
                                               style = list(color = "white"),
                                               direction = "auto",
                                               offset = c(0, -10))) %>% 
  addScaleBar(options = list(imperial = FALSE)) %>%
  addProviderTiles(providers$Esri.WorldImagery)

Esri.WorldImagery

Building a Leaflet map is similar to ggplot2, except that the pipe symbol is used to add layers instead of “+”. In the example above we create a leaflet object from the coordinate data file, add a bounding box based on the latitude/longitude values, add markers and a scale bar.
In the final step we add a map tile from a provider. The example here is named Esri.WorldImagery. You can preview the complete set of providers at this site.

Hydda.Base

Leaflet maps are designed to be pretty, so there are not many provider tiles that might be called plain or minimalist. For something less “satellite imagery” and more “traditional map”, try changing the provider to Hydda.Base.

Esri.WorldGrayCanvas

In the final example, we change the label colour to black and use the Esri.WorldGrayCanvas, which is about as plain and monochrome as Leaflet gets.

If you use RStudio, Leaflet maps are rendered in the Viewer pane, from where you can export to HTML or image files (though the latter is a little broken for me right now). RStudio also makes it easy to explore and install other useful Leaflet-associated packages, such as leaflet.extras.

So that’s Leaflet for R. One issue is that there are quite a few new functions to learn, some of which have many options. Fortunately the introductory documentation is very good.