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
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.
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
Creating Maps in R (2019)
Creating a heatmap of the UK in R
UK population density map in R
Create a unique population density map of the UK in R using ggplot2 and geom_point.
Data Art: London mapped in R
Plotting an artistic map of London in R.
3D world map with plotly
Quickly plot data on a 3D world map with the plotly package for R.
London map in 3 easy steps
Create a unique map of London using just ggplot2.
Access more for free
Access more articles and code for free