Introduction

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.

Installation

#installing packages: dplyr, leaflet and the tidyverse

Getting Started

Here 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)



Add Tiles

Leaflet Function

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)


Add Multiple Tiles

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"))



Add Markers to the Map

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)



Add Clusters

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)



Set A Starting Zoom

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)



Add a Minimap

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
    )



Further Resources

Learn more about Making Interactive Maps with the following:



Works Cited

This code through references and cites the following sources:



Thanks for Checking this Out ;)

Sanaz