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



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