get_static_tiles | R Documentation |
This function queries the Mapbox Static Tiles API and composites the tiles
as a raster suitable for use as a basemap in
tmap or
ggplot2 (with the
ggspatial::layer_spatial()
function. It returns a raster
layer that corresponds either to an input bounding box or a buffered area
around an input shape.
get_static_tiles(
location,
zoom,
style_id,
username,
style_url = NULL,
scaling_factor = c("1x", "2x"),
buffer_dist = 5000,
units = "m",
crop = TRUE,
access_token = NULL
)
location |
An input location for which you would like to request tiles.
Can be a length-4 vector representing a bounding box, or an |
zoom |
The zoom level for which you'd like to return tiles. |
style_id |
A Mapbox style ID; retrieve yours from your Mapbox account. |
username |
A Mapbox username. |
style_url |
A Mapbox style URL. |
scaling_factor |
The scaling factor to use; one of |
buffer_dist |
The distance to buffer around an input |
units |
Units of |
crop |
Whether or not to crop the result to the specified bounding box or buffer area.
Defaults to |
access_token |
A Mapbox access token. Supply yours here or set globally with the |
A raster layer of tiles from the requested Mapbox style representing the area around the input location. The raster layer is projected in the Web Mercator coordinate reference system.
## Not run:
library(mapboxapi)
library(tigris)
library(tmap)
library(ggspatial)
library(ggplot2)
ny_tracts <- tracts("NY", "New York", cb = TRUE)
ny_tiles <- get_static_tiles(
location = ny_tracts,
zoom = 10,
style_id = "light-v9",
username = "mapbox"
)
# tmap usage:
tm_shape(ny_tiles) +
tm_rgb() +
tm_shape(ny_tracts) +
tm_polygons(alpha = 0.5, col = "navy") +
tm_credits("Basemap (c) Mapbox, (c) OpenStreetMap",
position = c("RIGHT", "BOTTOM")
)
# ggplot2 usage:
ggplot() +
layer_spatial(ny_tiles) +
geom_sf(data = ny_tracts, fill = "navy", alpha = 0.5) +
theme_void() +
labs(caption = "Basemap (c) Mapbox, (c) OpenStreetMap")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.