Most basic background map with R and Leaflet



This post is a very basic introduction to the leaflet R package. It shows how to display a background map using default parameters, with reproducible code provided.

Background map section About Maps

This page explains how to display a background map with the R leaflet package.

First initiate the map with the leaflet() function. Then add tiles with addTiles(). Note the use of the %>% operator to “pipe” functions. Here you can see how to get the same result using or not this operator.

By default, you get the map beside. See next charts to learn how to zoom on a zone, change background style.



# Load the library
library(leaflet)
 
# Note: if you do not already installed it, install it with:
# install.packages("leaflet")

# Initialize the leaflet map with the leaflet() function
m <- leaflet()
# Then we Add default OpenStreetMap map tiles
m <- addTiles(m)
m
 
# Same stuff but using the %>% operator
m <- leaflet() %>% 
  addTiles()
m

# save the widget in a html file if needed.
# library(htmlwidgets)
# saveWidget(m, file=paste0( getwd(), "/HtmlWidget/backgroundMapBasic.html"))

Related chart types


Map
Choropleth
Hexbin map
Cartogram
Connection
Bubble map



❤️ 10 best R tricks ❤️

👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥