Have a project at work that I want to try this on so work through some of the examples.
Map with the default OSM tile layer.
library(leaflet)
m = leaflet() %>% addTiles()
m
Set a view here on two very circular subdivisions.
m = m %>% setView(-111.884481, 33.208993, zoom = 14)
m
Add a couple popups.
m %>% addPopups(-111.884481, 33.208993, 'Circular Subdivision <b>1</b>') %>% addPopups(-111.867079, 33.209244, 'Circular Subdivision <b>2</b>')
Let’s create a colorful map of the US States.
library(maps)
mapStates = map("state", fill = TRUE, plot = FALSE)
leaflet(data = mapStates) %>% addTiles() %>%
addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE)
A Formula Interface.
m = leaflet() %>% addTiles()
df = data.frame(
lat = rnorm(100),
lng = rnorm(100),
size = runif(100, 5, 20),
color = sample(colors(), 100)
)
m = leaflet(df) %>% addTiles()
m %>% addCircleMarkers(radius = ~size, color = ~color, fill = FALSE)
## Assuming 'lng' and 'lat' are longitude and latitude, respectively
m %>% addCircleMarkers(radius = runif(100, 4, 10), color = c('red'))
## Assuming 'lng' and 'lat' are longitude and latitude, respectively
Vector layers.
set.seed(123)
m = leaflet() %>% addTiles()
rand_lng = function(n = 10) rnorm(n, -93.65, .01)
rand_lat = function(n = 10) rnorm(n, 42.0285, .01)
# circles (units in metres)
m %>% addCircles(rand_lng(50), rand_lat(50), radius = runif(50, 10, 200))
We can use other tile sets.
leaflet() %>%
addTiles(
'http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}',
attribution = 'Tiles © Esri — Esri, DeLorme, NAVTEQ, TomTom, Intermap, iPC, USGS, FAO, NPS, NRCAN, GeoBase, Kadaster NL, Ordnance Survey, Esri Japan, METI, Esri China (Hong Kong), and the GIS User Community') %>%
setView(-111.884481, 33.208993, zoom = 14)
Black and White.
leaflet() %>% addProviderTiles('Stamen.TonerLite') %>% setView(-111.884481, 33.208993, zoom = 14)
So artsy.
leaflet() %>% addProviderTiles('Stamen.Watercolor') %>% setView(-111.884481, 33.208993, zoom = 14)
Terrain.
leaflet() %>% addProviderTiles('Stamen.Terrain') %>% setView(-111.884481, 33.208993, zoom = 14)
OpenStreetMap’s HOT.
leaflet() %>% addProviderTiles('OpenStreetMap.HOT') %>% setView(-111.884481, 33.208993, zoom = 14)