Learn R Programming

mapboxapi (version 0.2)

static_mapbox: Return a static Mapbox map from a specified style

Description

Return a static Mapbox map from a specified style

Prepare overlay markers for use in a Mapbox static map

Usage

static_mapbox(
  style_id,
  username,
  overlay_sf = NULL,
  overlay_style = NULL,
  overlay_markers = NULL,
  longitude = NULL,
  latitude = NULL,
  zoom = NULL,
  width = 600,
  height = 400,
  bearing = NULL,
  pitch = NULL,
  scaling_factor = c("1x", "2x"),
  before_layer = NULL,
  access_token = NULL
)

prep_overlay_markers( data = NULL, marker_type = c("pin-s", "pin-l", "url"), label = NA, color = NA, longitude = NULL, latitude = NULL, url = NA )

Arguments

style_id

The style ID

username

Your Mapbox username

overlay_sf

The overlay sf object (optional). The function will convert the sf object to GeoJSON then plot over the basemap style. Spatial data that are too large will trigger an error, and should be added to the style in Mapbox Studio instead.

overlay_style

A named list of vectors pecifying how to style the sf overlay. Possible names are "stroke", "stroke-width", "stroke-opacity", "fill", and "fill-opacity". The fill and stroke color values should be specified as six-digit hex codes, and the opacity and width values should be supplied as floating-point numbers.

overlay_markers

The prepared overlay markers (optional). See the function prep_overlay_markers for more information on how to specify a marker overlay.

longitude

A vector of longitudes; inferred from the input dataset if data is provided.

latitude

A vector of latitudes; inferred from the input dataset if data is provided.

zoom

The map zoom. The map will infer this from the overlay unless longitude, latitude, and zoom are all specified.

width

The map width; defaults to 600px

height

The map height; defaults to 600px

bearing

The map bearing; defaults to 0

pitch

The map pitch; defaults to 0

scaling_factor

The scaling factor of the tiles; either "1x" (the default) or "2x"

before_layer

A character string that specifies where in the hierarchy of layer elements the overlay should be inserted. The overlay will be placed just above the specified layer in the given Mapbox styles.

access_token

Your Mapbox access token; can be set with mb_access_token().

data

An input data frame with longitude and latitude columns (X and Y or lon and lat as names are also acceptable) or an sf object with geometry type POINT.

marker_type

The marker type; one of "pin-s", for a small pin; "pin-l", for a large pin; and "url", for an image path.

label

The marker label (optional). Can be a letter, number (0 through 99), or a valid Maki icon (see https://labs.mapbox.com/maki-icons/) for options).

color

The marker color (optional). Color should be specified as a three or six-digit hexadecimal code without the number sign.

url

The URL of the image to be used for the icon if marker_type = "url".

Value

A pointer to an image of class "magick-image". The resulting image can be manipulated further with functions from the magick package.

A formatted list of marker specifications that can be passed to the static_mapbox function.

Examples

Run this code
# NOT RUN {
library(mapboxapi)

points_of_interest <- tibble::tibble(
  longitude = c(-73.99405, -74.00616, -73.99577, -74.00761),
  latitude = c(40.72033, 40.72182, 40.71590, 40.71428)
)

prepped_pois <- prep_overlay_markers(
  data = points_of_interest,
  marker_type = "pin-l",
  label = 1:4,
  color = "fff"
)

map <- static_mapbox(
  style_id = "streets-v11",
  username = "mapbox",
  overlay_markers = prepped_pois,
  width = 1200,
  height = 800
)

map

# }
# NOT RUN {
# }

Run the code above in your browser using DataLab