9.10 Interactive Maps
tmap_mode("view") # or use tmap_leaflet()
## tmap mode set to interactive viewing
map_nz3
## stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)
+ tm_basemap(server = "OpenTopoMap") map_nz
= left_join(world, coffee_data, by = "name_long")
world_coffee = c("coffee_production_2016", "coffee_production_2017")
facets tm_shape(world_coffee) + tm_polygons(facets) +
tm_facets(nrow = 1, sync = TRUE)
# restore static plot default
tmap_mode("plot")
## tmap mode set to plotting
9.10.1 Mapview
::mapview(nz) mapview
Consider the following example where sf
is used to intersect lines and polygons and then is visualized with mapview
9.10.3 Leaflet
leaflet
which is the most mature and widely used interactive mapping package in R. leaflet
provides a relatively low-level interface to the Leaflet JavaScript library. Here is the leaflet
package in action, showing cycle hire points in London.
= colorNumeric("RdYlBu", domain = cycle_hire$nbikes)
pal leaflet(data = cycle_hire) |>
addProviderTiles(providers$CartoDB.Positron) |>
addCircles(col = ~pal(nbikes), opacity = 0.9) |>
addPolygons(data = lnd, fill = FALSE) |>
addLegend(pal = pal, values = ~nbikes) |>
setView(lng = -0.1, 51.5, zoom = 12) |>
addMiniMap()