Leaflet maps in R

By Data Tricks, 19 November 2018

This tutorial guides you through producing interactive maps using the leaflet package for R. These can then easily be customised and embedded into websites or other web based applications.

Download the Leaflet package

install.packages("leaflet")
library(leaflet)

Create a basic map

mymap <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=-0.127949, lat=51.507752, popup="Add your description here")
mymap

More map styles

mymap <- leaflet() %>%
addTiles() %>%
setView( lng=-0.127949, lat=51.507752, zoom = 5 ) %>%
addProviderTiles("NASAGIBS.ViirsEarthAtNight2012")
mymap

mymap <- leaflet() %>%
addTiles() %>%
setView( lng=-0.127949, lat=51.507752, zoom = 15 ) %>%
addProviderTiles("Esri.WorldImagery")
mymap

There are many more styles available at this link. Simply copy the name of the style and paste into the addProviderTiles line in the code above.

Note: some style only work up to a certain zoom level. For example, NASA’s Earth at Night style (as per the example above) does not work when fully zoomed in. Ensure you set the correct zoom level using setView in the code.

 

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Please note that your first comment on this site will be moderated, after which you will be able to comment freely.