Day 5: Leaflet

by Danielle Navarro, 01 May 2018

The leaflet package lets you draw awesome interactive maps (using leaflet.js, I presume!), and it’s nicely documented on the RStudio website here. So I think that for today’s learning exercise I’ll follow along with the tutorial there, but maybe take a few excursions as I go. To make it a bit more fun I’ll use a different data set. A few moments on google turns up the latlong.net site, so I quickly grab data about the locations of a few places in Sydney from there, save it as a CSV, and my first task will be to plot them on a Sydney map. So here’s the data:

If I’ve understood it right, this code is doing three things. First, the call to leaflet creates the map widget using the htmlwidgets package (which, hey, I should actually read the docs for that one too – but these are supposed to be quick posts and I’ve got a time budget). The widget is then piped to addTiles which uses data from Open Street Maps to draw the map itself. Finally the addMarkers function places map markers using the data from syd.

That’s pretty awesome for a few minutes of googling and a cut-and-paste job!

Choosing different tiles

The map tiles in the widget shown above come from Open Street Map, which is a super cool thing in itself that I remember reading about one time ages ago. Apparently I can plug in tiles from many different providers though? That seems really neat - presumably I can get a very differnet look and feel just by tweaking that.

Looks like the addProviderTiles() function is the thing I need to do that. There’s a nice list of providers. So for instance if I wanted a black and white gothic looking thing, I could do this:

Piping a data frame through the map layers

Okay, so now that I read a little further into the documentation 📚 I see that leaflet() and the various functions that provide map layers (e.g., addTiles(), addMarkers(), etc) all have an optional data argument which can be used to specify the data for the map. It seems pretty flexible. It can handle data frames (like the syd data frame I loaded earlier), but it also plays nicely with the sp package which (as I understand it) attempts to fold a lot of things about spatial mapping under a single framework. The cool thing is that the functions work pretty hard to guess how to use the variable names.

That’s neat. It didn’t work out that I wanted to use syd$Place as the popup labels for the markers, but I presume I could just manually set popup = syd$Place like last time.

Customising markers

Okay… moving right along! I’m on the clock here!

Apparently we can customise the markers? The default marker is fine, but it’s sometimes nice to come up with alternatives. There’s a “pirates and ships” icons example in the tutorial makes me wonder if I can do something like that with my Sydney map? A first pass at this:

Hm… skimming, skimming… ooooohhh the addAwesomeMarkers() function looks, well, awesome. It lets you specify markers using font-awesome and a bunch of other possibilities.

Other examples?

Just before moving on completely, I took a little stickybeak at a few of the other ways you can use the widget. I could, for instance, plot a map that shows only the locations of various US cities as dots…

Along the same lines, if I actually knew what I were doing with leaflet, I could extend it to draw some very cool chloropleth maps. I feel like I need to get more of a sense of what’s going on here, but the tutorial has a very cute example: