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:

syd <- read.csv("https://djnavarro.net/files/latlong-sydney.csv")
syd
##       ï..Place  Latitude Longitude
## 1  Opera House -33.85616  151.2153
## 2  Bondi Beach -33.89084  151.2743
## 3      Airport -33.94735  151.1794
## 4 Surrey Hills -33.89065  151.2129
## 5   Parramatta -33.80825  151.0050
## 6  Taronga Zoo -33.84105  151.2422
## 7    Bankstown -33.91729  151.0359

I am of course filled with many opinions about the choice of locations – I mean, Bondi, really? 🙄 The Inner Westie in me is appalled – but whatevs.

So let’s have a go at this. A quick browse through the tutorial suggests that I should try this for my first attempt:

sydneyMap <- leaflet() %>%
lat = syd$Latitude, lng = syd$Longitude,
popup = syd\$Place
)

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.

Here’s the map:

sydneyMap

<div id="htmlwidget-1" style="width:672px;height:480px;" class="leaflet html-widget">