Section 2.5 Plotting interactive maps with leaflet
In Chapter 1, we introduced the
ggplot2 package for making maps in R. In this chapter, we are going to introduce leaflet as one way to easily make some neat maps. It is the leading open-source JavaScript library for mobile-friendly interactive maps. It is very popular, used by websites ranging from The New York Times and The Washington Post to GitHub and Flickr, as well as GIS specialists like OpenStreetMap, Mapbox, and CartoDB. We will also make use of the RColorBrewer package.
library(leaflet) #for mapping
library(RColorBrewer) #for getting nice colours for your maps
Then create a map with this simple bit of code:
m <- leaflet() %>% addTiles()
And just print it:
m

It is not a super useful map, but it was really easy to make! You might of course want to add some content to your map.
You can add a point manually:
m <- leaflet() %>% addTiles() %>%
addMarkers(lng=-2.230899, # longitude
lat=53.464987, # latitude
popup="University of Manchester") # text for popup
#print leaflet map
m

If you click over the highlighted point, you will read our input text "University of Manchester".
You can add many points manually, with some popup text as well:
# create dataframe of latitude, longitude, and popups
latitudes <- c(53.464987, 53.472726, 53.466649)
longitudes <- c(-2.230899, -2.245481, -2.243421)
popups <- c("You are here", "Here is another point", "Here is another point")
df <- data.frame(latitudes, longitudes, popups)
# create leaflet map
m <- leaflet(data = df) %>% addTiles() %>%
addMarkers(lng=~longitudes, lat=~latitudes, popup=~popups)
#print leaflet map
m
We can also map polygons, not just points. Let’s plot our crimes on/near bars to illustrate. To do this, we can return to our buffers where we counted the number of crimes within 100 metres of each bar/ licensed premise (the "crimes_per_prem" object).
First, let’s pick a colour palette. We do this with the
colorBin() function. We will discuss colour choices in maps in Chapter 5; for now, let’s just pick the "RdPu" palette. We should also specify the domain = parameter (what value to use for shading, in this case n, bins =, the number of crimes), the number of bins (in this case 5, we will discuss this in detail in the coming chapters as well), and pretty = to use pretty breaks (this may actually mess with the number of bins specified in the bins parameter; but again, for now this is OK).
Let’s create this palette and save in an object called
pal for palette:
pal <- colorBin("RdPu", domain = crimes_per_prem$n, bins = 5, pretty = TRUE)
Now we can make a leaflet map, where we add these polygons (buffers) with the
addPolygons() function, and call our palette, specifying again the variable to use for shading, as well as some other parametres. One to highlight specifically is the label parameter. This allows us to use a variable as a label for when a user clicks on our polygon (buffer). Here we specify the name of the bar with label = ~as.character(name). This way we not only shade each buffer with the number of crimes which fall inside it, but also include a little popup label with the name of the establishment:
leaflet(crimes_per_prem) %>%
addTiles() %>%
addPolygons(fillColor = ~pal(n), fillOpacity = 0.8,
weight = 1, opacity = 1, color = "black",
label = ~as.character(name)) %>%
addLegend(pal = pal, values = ~n, opacity = 0.7,
title = 'Violent crimes', position = "bottomleft")
It’s not the neatest of maps, with all these overlaps, but we will talk about prettifying maps further down the line. You can, however, pan and zoom, and investigate to find our most high-crime venue, the Crafty Pig. And here, with this background information, we can solve the puzzle. You see, the Crafty Pig appears to be the nearest venue to an area in Manchester City Centre called Piccadilly Gardens which is an area known for high levels of crime and anti social-behaviour. Therefore, it is likely that we are erroneously attributing many of these crimes to the Crafty Pig venue, as they may be taking place in Piccadilly Gardens instead. It is important, therefore, to think about any unintended consequences of the spatial operations we carry out, and how these might affect the conclusions which we draw from our crime mapping exercises.
Finally, let’s say we want to save our interactive map, while keeping it interactive. You can do this by clicking on the export button at the top of the plot viewer, and choose the Save as Webpage option saving this as a
.html file:

Then you can open this file with any type of web browser (safari, firefox, chrome) and share your map that way. You can send this to your friends, and make them jealous of your fancy map-making skills.
