Most basic background map with R and Leaflet



The R leaflet package allows to build interactive maps with R. This post explains how to zoom on a specific part of the map, with explanation and reproducible code.

Background map section About Maps

The previous most basic map with leaflet explains how to show a map with the R leaflet package.

To specify which area of the map you want to display, just specify latitude, longitude and how much you want to zoom in the setView() call:

  • Longitude (lng) goes between -180 (West) and +180 (East).
  • Latitude (lat) goes between -90 (South) and +90 (North).
  • Zoom is controled using the zoom argument.



# Load the library
library(leaflet)

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

# Make the map
m <- leaflet() %>% 
  addTiles() %>% 
  setView( lng = 166.45, lat = -22.25, zoom = 8 )
m

# save the widget in a html file if needed.
# library(htmlwidgets)
# saveWidget(m, file=paste0( getwd(), "/HtmlWidget/backgroundMapZoom.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! 🔥