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



Contact

This document is a work by Yan Holtz. Any feedback is highly encouraged. You can fill an issue on Github, drop me a message on Twitter, or send an email pasting yan.holtz.data with gmail.com.

Github Twitter