# 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.

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

## Create a basic map

mymap <- leaflet() %>%
mymap

## More map styles

mymap <- leaflet() %>%
setView( lng=-0.127949, lat=51.507752, zoom = 5 ) %>%
mymap

mymap <- leaflet() %>%
setView( lng=-0.127949, lat=51.507752, zoom = 15 ) %>%
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: ,

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

## Free data science in R guide

Sign up to our newsletter and we will send you a series of guides containing tips and tricks on data science and machine learning in R.

No thanks