This project will show how you can make interactive maps in your Rmarkdown file using the package Leaflet. Before we get started we first load this package.
library(leaflet)
The we created a datafram with the latitude and longtitude of the city Amsterdam in the Netherlands
set.seed(12345)
data <- data.frame(lat = runif(20, min = 52.2, max = 52.6),
lng = runif(20, min = 4.6, max = 5.0))
With the leaflet function it is easy to obtain a map using these specified longtitude and latitude.
data %>%
leaflet() %>%
addTiles() %>%
addMarkers(lat = 52.379189, lng = 4.899431, popup = "Amsterdam Central")
Another functionality of the package is to add markers corresponding to the size of the cities. For this the function addMarkers is used. One can zoom in and out on this map, and cities cluster together as you zoom in or out.
cities <- data.frame(name = c('Amsterdam', 'The Hague', 'Utrecht', 'Haarlem', 'Maastricht', 'Lelystad', 'Groningen',
'Zwolle'),
size = c(120000, 98362, 73632, 64734, 64632, 54362, 53632, 45832),
lat = c(52.379189, 52.078663, 52.092876, 52.387386, 50.851368, 52.518536, 53.21917, 52.5125),
lng = c(4.899431, 4.288788, 5.104480, 4.646219, 5.690973, 5.471422, 6.56667, 6.09444))
cities %>%
leaflet() %>%
addTiles() %>%
addCircles(weight = 1, radius = sqrt(cities$size)*30) %>%
addMarkers(clusterOptions = markerClusterOptions())