These examples were created for a Nature Toolbox article by Jeffrey Perkel, published 5 June 2018. [https://www.nature.com/articles/d41586-018-05331-6]
The following maps are created using the Leaflet R plugin. The Leaflet library produces interactive maps that allow users to pan, zoom, and click on points of interest.
I created and read in a table of latitude/longitude values in comma-separated values (CSV) format, using values extracted from Google Maps. Then I plot those points on a topographical map of London, and overlay a second data source from Macrostrat.org, which colors the map according to its geological features.
library (leaflet)
#
# In Google Maps, find a location, right click, select 'What's Here', and copy
# the lat/long values. These values can be stored in a .csv file. I've added them
# here to file for simplicity.
#
df <- read.csv(textConnection(
"Name,Lat,Long
Nature,51.533925,-0.121553
Francis Crick Institute,51.531877,-0.128767
University College London,51.524486,-0.133997
MRC Laboratory for Molecular Cell Biology,51.524435,-0.132495
King's College London,51.511573,-0.116083
Imperial College London,51.498780,-0.174888
Cambridge University,52.206960,0.115034
Oxford University,51.754843,-1.254302
Platform 9 3/4,51.532349,-0.123806
"))
# create a blank map
m <- leaflet()
# mark points with blue circles, except Nature's offices in red...
m <- addCircleMarkers(m, lat=df$Lat, lng=df$Long, label=df$Name,
color = (ifelse (df$Name == "Nature", "red", "blue")))
# add an underlying basemap
m <- addProviderTiles(m, providers$OpenTopoMap)
# center the view on London
m <- setView(m, -0.119,51.525, zoom = 8)
# pull in MacroStrat tiles
m <- addTiles (m, 'https://tiles.macrostrat.org/carto/{z}/{x}/{y}.png',
options = tileOptions(opacity = 0.6))
# draw the map
m
Collect session information.
sessionInfo()
## R version 3.5.0 (2018-04-23)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS High Sierra 10.13.5
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] leaflet_2.0.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.17 later_0.7.2 digest_0.6.15 rprojroot_1.3-2
## [5] mime_0.5 R6_2.2.2 jsonlite_1.5 xtable_1.8-2
## [9] backports_1.1.2 magrittr_1.5 evaluate_0.10.1 stringi_1.2.2
## [13] promises_1.0.1 rmarkdown_1.9 tools_3.5.0 stringr_1.3.1
## [17] htmlwidgets_1.2 crosstalk_1.0.0 shiny_1.1.0 httpuv_1.4.3
## [21] yaml_2.1.19 compiler_3.5.0 htmltools_0.3.6 knitr_1.20