This code through explores how you can Make Interactive Maps in R with Less Than 15 Lines of Code. It imports the pipe %>% symbol from dplyer or magrittr packages.
#installing packages: dplyr, leaflet and the tidyverseHere we are using List of Airports in Jordan, with latitude and longitude. Unverified community data from The Humanitarian Data Exchange
# Load packages
library(dplyr)
library(leaflet)
library(tidyverse)
# Load data
URL <- ("https://raw.githubusercontent.com/Sanaz-27/Cpp529-CodeThrough/main/list-of-airports-in-jordan-hxl-tags-1.csv")
Data <- read.csv(URL)
head(Data)
Step (1): We have to pass our data into the leaflet function, using the pipe. This alone won’t make a map.
Step (2): Pick out tiles (what the map looks) like at this Link.
Step (3): Pass the output of the leaflet function into the addProviderTiles() function, with the only argument being the tiles you selected in Step (2) I picked OpenStreetMap.Mapnik.
Data %>%
leaflet() %>%
addProviderTiles(providers$OpenStreetMap.Mapnik)Step (1): Select another set of tiles you want to add.
Step (2): Give each of the tiles a name with the group argument, I selected Esri.WorldImagery.
Step (3): Add layer control (this enables you to see both tiles).
You’ll be able to select the tiles you want to view in the top right corner.
Data %>%
leaflet() %>%
addProviderTiles(providers$Esri.WorldImagery, group = "World Imagery") %>%
addProviderTiles(providers$OpenStreetMap.Mapnik, group = "Mapnik") %>%
addLayersControl(baseGroups = c("Mapnik", "World Imagery"))
Step (1): Use the addMarkers() function, if your data set has latitude and longitude (or an abbreviation of them) you don’t need to fill out the argument for lat and lng.
Step (2): You can optionally set the label and popup arguments to display information when you hover your mouse over the marker or click it.
Data %>%
leaflet() %>%
addProviderTiles(providers$Esri.WorldImagery, group = "World Imagery") %>%
addProviderTiles(providers$OpenStreetMap.Mapnik, group = "Mapnik") %>%
addLayersControl(baseGroups = c("Mapnik", "World Imagery")) %>%
addMarkers(lng = Data$longitude,
lat = Data$latitude,
label = Data$name,
popup = Data$type)
This is a good thing to do if you have lots and lots of data points, so that your map doesn’t get overwhelmed. It’s also good to do if you have points that are very close together. All we need to do is update our addMarkers() function with the clusterOptions argument.
Data %>%
leaflet() %>%
addProviderTiles(providers$Esri.WorldImagery, group = "World Imagery") %>%
addProviderTiles(providers$OpenStreetMap.Mapnik, group = "Mapnik") %>%
addLayersControl(baseGroups = c("Mapnik", "World Imagery")) %>%
addMarkers(lng = Data$longitude,
lat = Data$latitude,
clusterOptions = markerClusterOptions(),
label = Data$name,
popup = Data$type)
This step is by no means required, but if you want to focus your map on a specific location to start with, you can use the setView() function.
I’ll focus in on Amman - Jordan.
Data %>%
leaflet() %>%
addProviderTiles(providers$Esri.WorldImagery, group = "World Imagery") %>%
addProviderTiles(providers$OpenStreetMap.Mapnik, group = "Mapnik") %>%
addLayersControl(baseGroups = c("Mapnik", "World Imagery")) %>%
addMarkers(lng = Data$longitude,
lat = Data$latitude,
clusterOptions = markerClusterOptions(),
label = Data$name,
popup = Data$type) %>%
setView(lat = 31.9539, lng = 35.9106, zoom = 10)
To add a minimap to our map, we just need to use the addMiniMap() function.
Two arguments you can adjust with addMiniMap() are changing the tiles (which allows you to change the map background, just like in the main one), and toggleDisplay (which lets you hide the map).
Data %>%
leaflet() %>%
addProviderTiles(providers$Esri.WorldImagery, group = "World Imagery") %>%
addProviderTiles(providers$OpenStreetMap.Mapnik, group = "Mapnik") %>%
addLayersControl(baseGroups = c("Mapnik", "World Imagery")) %>%
addMarkers(lng = Data$longitude,
lat = Data$latitude,
clusterOptions = markerClusterOptions(),
label = Data$name,
popup = Data$type) %>%
setView(lat = 31.9539, lng = 35.9106, zoom = 10) %>%
addMiniMap(
toggleDisplay = TRUE,
tiles = providers$Stamen.TonerLite
)
Learn more about Making Interactive Maps with the following:
Resource I Making Interactive Maps in R with Less Than 15 Lines of Code
Resource II Leaflet Package
Resource III 10 examples of interactive map data visualizations
This code through references and cites the following sources:
Brandon Walker (2019). Source I. Making Interactive Maps in R with Less Than 15 Lines of Code
Leaflet Extras. Github. Leaflet Providers Demo
The Humanitarian Data Exchange (2020). Airports in Jordan
Sanaz