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.

You might also like

Which Statistical Test?

A tool to help you choose the most appropriate statistical test.

Read more

Creating Maps in R (2019)

Creating a heatmap of the UK in R

Read more

UK population density map in R

Create a unique population density map of the UK in R using ggplot2 and geom_point.

Read more

Data Art: London mapped in R

Plotting an artistic map of London in R.

Read more

3D world map with plotly

Quickly plot data on a 3D world map with the plotly package for R.

Read more