Leaflet is one of the most popular Javascript libraries for creating interactive maps. The leaflet R package allows you to create your own leaflet maps without needing to know any Javascript!
# install.packages("leaflet")
library(leaflet)
Getting started with leaflet is easy. The leaflet() function creates a map widget that you can store in a variable so that you can modify the map later on. You can add features to the map using the pipe operator (%>%) just like in dplyr. The addTiles() function adds mapping data from Open Street Map.
# !formatR
m <- leaflet() %>% addTiles()
m # a map with the default OSM tile layer
Now I am pinning Oodi Helsinki Central Library in Finland
m <- m %>%
addMarkers(lat=60.17391336310122, lng=24.938040247094637,
popup="OODI Library")
m
The blue markers that leaflet comes packaged with may not be enough depending on what you’re mapping. Thankfully you can make your own markers from .png files.
oodiicon <- makeIcon(
iconUrl = "https://www.cnba.it/contenuti/uploads/2019/09/Oodi-480x250.jpg",
iconWidth = 31*215/230, iconHeight = 31,
iconAnchorX = 31*215/230/2, iconAnchorY = 16
)
oodilocation <- data.frame(
lat = c(60.17391336310122),
lng = c(24.938040247094637))
oodilocation %>%
leaflet() %>%
addTiles() %>%
addMarkers(icon = oodiicon)
The blue markers that leaflet comes packaged with may not be enough depending on what you’re mapping. Thankfully you can make your own markers from .png or ´.jpg´ files. Here you can see the libraries in the city center of Helsinki
iconlib <- makeIcon(
iconUrl = "https://galeri7.uludagsozluk.com/215/kitap_295314.jpg",
iconWidth = 31*215/230, iconHeight = 31,
iconAnchorX = 31*215/230/2, iconAnchorY = 16
)
libraries <- data.frame(
lat = c(60.17391336310122, 60.17087587765499, 60.1729233860828),
lng = c(24.938040247094637, 24.950524515551646, 24.950313608918563))
libraries %>%
leaflet() %>%
addTiles() %>%
addMarkers(icon = iconlib)
When adding multiple markers to a map, you may want to add popups for each marker. You can specify a string of plain text for each popup, or you can provide HTML which will be rendered inside of each popup.
librarysites <- c(
"<a href='http://www.oodihelsinki.fi/'> Oodi Library </a>",
"<a href='https://www.kansalliskirjasto.fi/'>The National Library of Finland</a>",
"<a href='http://www.helsinki.fi/'>Helsinki University</a>"
)
libraries %>%
leaflet() %>%
addTiles() %>%
addMarkers(icon = iconlib, popup = librarysites)
Sometimes you might have so many points on a map that it doesn’t make sense to plot every marker. In these situations leaflet allows you to plot clusters of markers using addMarkers(clusterOptions = markerClusterOptions()). When you zoom in to each cluster, the clusters will separate until you can see the individual markers.
df <- data.frame(lat = runif(20, min = 60.17, max = 60.27),
lng = runif(20, min = 24.94, max = 25.10))
df %>%
leaflet() %>%
addTiles() %>%
addMarkers(clusterOptions = markerClusterOptions())
Instead of adding markers or clusters you can easily add circle markers using addCircleMarkers().
df <- data.frame(lat = runif(20, min = 60.17, max = 60.27),
lng = runif(20, min = 24.94, max = 25.10))
df %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers()
You can draw arbitrary shapes on the maps you create, including circles and squares. The code below draws a map where the circle on each city is proportional to the population of that city.
md_cities <- data.frame(name = c("Baltimore", "Frederick", "Rockville", "Gaithersburg",
"Bowie", "Hagerstown", "Annapolis", "College Park", "Salisbury", "Laurel"),
pop = c(619493, 66169, 62334, 61045, 55232,
39890, 38880, 30587, 30484, 25346),
lat = c(39.2920592, 39.4143921, 39.0840, 39.1434, 39.0068, 39.6418, 38.9784, 38.9897, 38.3607, 39.0993),
lng = c(-76.6077852, -77.4204875, -77.1528, -77.2014, -76.7791, -77.7200, -76.4922, -76.9378, -75.5994, -76.8483))
md_cities %>%
leaflet() %>%
addTiles() %>%
addCircles(weight = 1, radius = sqrt(md_cities$pop) * 30)
## Assuming "lng" and "lat" are longitude and latitude, respectively
You can add rectangles on leaflet maps as well: Here is the Kaisainiemenlahti
leaflet() %>%
addTiles() %>%
addRectangles(lat1 = 60.1733, lng1 = 24.9410,
lat2 = 60.1763, lng2 = 24.9450)
Adding a legend can be useful if you have markers on your map with different colors:
df <- data.frame(lat = runif(20, min = 60.17, max = 60.27),
lng = runif(20, min = 24.94, max = 25.10),
col = sample(c("red", "blue", "green"), 20, replace = TRUE),
stringsAsFactors = FALSE)
df %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(color = df$col) %>%
addLegend(labels = LETTERS[1:3], colors = c("blue", "red", "green"))
## Assuming "lng" and "lat" are longitude and latitude, respectively
For more details about the leaflet package for R visit http://rstudio.github.io/leaflet/.