Different Basemaps in leaflet (R)

A sample of different basemaps that can be used in leaflet.

Jeffrey Schmid-Paz
2022-01-01

During work I learned, that showing people insights from data in a geographic form is extremely intuitive and accessible for all kind of professionals. While doing several reports in different tools I looked for the best suitable basemaps to carry my analysis over to the people who need to look at the report. I hope that a simple post to show how to use different basemaps or background layers in leaflet for R serves you well in a similar need as I had. Please note, that I myself am not a trained geographer, which is why the maps used here focus more on those that a general public would use. Also, most of the services, restrict the number of accesses made per user such that the explanations here are only useful if you plan to make simple maps for yourself or a limited number of other users.

Preparations

So the first step in R of course would be to load the necessary packages in R.

library(leaflet)
library(sf) # to create a point to draw on the map

First let’s define an arbitrary coordinate to center our map on using the sf package. For this example I go with the city of Lucerne in Switzerland.

lucerne <- st_sfc(st_point(x = c(8.31, 47.05 )))
st_crs(lucerne) <-  'WGS84'

Create maps

Everyone who used leafletin R before, knows the default OpenStreetMap layer. For this, you simply leave the addTiles() function, which adds a base layer, empty such that the defaults are used

leaflet() %>% 
  addTiles() %>% 
  addMarkers(data = lucerne)

Using the addProviderTiles() function ue can conveniently choose among several background maps. See http://leaflet-extras.github.io/leaflet-providers/preview/ for a neat overview of all the included maps.

leaflet() %>% 
  addProviderTiles(provider = "Stamen.Terrain") %>% 
  addMarkers(data = lucerne)

leaflet however allows pretty much any open available XYZ (Tiled web map) to be used as background. Below, I provide a function call for you to see, how the practical implementation in a script works. A great source of basemaps may be governmental agencies. For Switzerland the federal agency swisstopo offers a broad range of such maps

leaflet() %>% 
  addTiles(urlTemplate = "https://wmts20.geo.admin.ch/1.0.0/ch.swisstopo.pixelkarte-farbe/default/current/3857/{z}/{x}/{y}.jpeg",
           attribution = '&copy; <a href="https://www.geo.admin.ch/de/about-swiss-geoportal/impressum.html#copyright">swisstopo</a>') %>% 
  addMarkers(data = lucerne)

In the function call you have to provide the URL of the XYZ tile as value for the urlTemplate argument. Make sure that the CRSs of your XYZ tile map and your leaflet object match (i.e. typically WGS84).

List of Services

Here is a short list of basemaps i personally thought are useful. Note that some maps may cover only limited areas. I hope i can extend the list over the months to come. In the list, I also provided a suggested attribution to include when you use them with fair use terms. However, I refuse to take any responsibility for any legal challenges you may face from using the mentioned maps in this blog post in any of your use cases you may have.

Roads

Service Link (copy & paste into addTiles()) Suggested Attribution Preview
Google Maps https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z} © Google Maps google_maps_road
swisstopo https://wmts20.geo.admin.ch/1.0.0/ch.swisstopo.pixelkarte-farbe/default/current/3857/{z}/{x}/{y}.jpeg © swisstopo swisstopo_color

Satellite Imagery

Service Link (copy & paste into addTiles()) Suggested Attribution Preview
Google Maps https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z} © Google Maps google_maps_satellite
swisstopo https://wmts20.geo.admin.ch/1.0.0/ch.swisstopo.swissimage/default/current/3857/{z}/{x}/{y}.jpeg © swisstopo swisstopo_satellite

Hybrid maps

Service Link (copy & paste into addTiles()) Suggested Attribution Preview
Google Maps https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z} © Google Maps google_maps_hybrid

If you happen to know some other great basemaps to be used or see any errors, feel free to leave a comment or open an issue over at the github repository of this website.

R Session Info

R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=English_Switzerland.1252 
[2] LC_CTYPE=English_Switzerland.1252   
[3] LC_MONETARY=English_Switzerland.1252
[4] LC_NUMERIC=C                        
[5] LC_TIME=English_Switzerland.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] sf_1.0-5        leaflet_2.0.4.1

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.1        terra_1.4-22           
 [3] xfun_0.25               purrr_0.3.4            
 [5] lattice_0.20-44         mapview_2.10.0         
 [7] colorspace_2.0-2        vctrs_0.3.8            
 [9] generics_0.1.0          leaflet.providers_1.9.0
[11] htmltools_0.5.2         stats4_4.1.1           
[13] yaml_2.2.1              base64enc_0.1-3        
[15] utf8_1.2.2              rlang_0.4.11           
[17] e1071_1.7-8             pillar_1.6.2           
[19] glue_1.4.2              DBI_1.1.1              
[21] sp_1.4-5                lifecycle_1.0.0        
[23] stringr_1.4.0           munsell_0.5.0          
[25] raster_3.4-13           htmlwidgets_1.5.4      
[27] codetools_0.2-18        memoise_2.0.0          
[29] evaluate_0.14           knitr_1.33             
[31] callr_3.7.0             fastmap_1.1.0          
[33] ps_1.6.0                crosstalk_1.2.0        
[35] class_7.3-19            fansi_0.5.0            
[37] leafem_0.1.6            Rcpp_1.0.7             
[39] KernSmooth_2.23-20      scales_1.1.1           
[41] classInt_0.4-3          satellite_1.0.4        
[43] cachem_1.0.5            webshot_0.5.2          
[45] jsonlite_1.7.2          distill_1.3            
[47] png_0.1-7               digest_0.6.27          
[49] stringi_1.7.3           processx_3.5.2         
[51] dplyr_1.0.7             grid_4.1.1             
[53] tools_4.1.1             magrittr_2.0.1         
[55] proxy_0.4-26            tibble_3.1.3           
[57] crayon_1.4.1            pkgconfig_2.0.3        
[59] downlit_0.4.0           ellipsis_0.3.2         
[61] rmarkdown_2.11          R6_2.5.0               
[63] units_0.7-2             compiler_4.1.1         

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY-SA 4.0. Source code is available at https://github.com/JBSP-code/personal-blog, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Schmid-Paz (2022, Jan. 1). Jeffrey Schmid-Paz: Different Basemaps in leaflet (R). Retrieved from https://www.jeffreyschmid.com/posts/2022-01-01-different-basemaps-in-leaflet-r/

BibTeX citation

@misc{schmid-paz2022different,
  author = {Schmid-Paz, Jeffrey},
  title = {Jeffrey Schmid-Paz: Different Basemaps in leaflet (R)},
  url = {https://www.jeffreyschmid.com/posts/2022-01-01-different-basemaps-in-leaflet-r/},
  year = {2022}
}