Show Sidebar Hide Sidebar

Filled Area in Mapbox in R

How to make an area on Map in R with plotly.

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

library(plotly)
packageVersion('plotly')
## [1] '4.9.0.9000'

Mapbox Access Token

To plot on Mapbox maps with Plotly you may need a Mapbox account and a public Mapbox Access Token, that you can add to your Plotly Settings. See our Mapbox Map Layers documentation for more information. If you're using a Chart Studio Enterprise server, please see additional instructions here.

How to Show an Area on a Map

There are three different ways to show an area in a mapbox:

  1. Use Scattermapbox trace and set fill attribute to 'toself'
  2. Use Scattermapbox trace and define the corresponding geojson
  3. Use the new trace type: Choroplethmapbox for mapbox cases, or Choropleth trace for non-mapbox ones.
The following example uses Scattermapbox and sets fill = 'toself'

library(plotly)

p <- plot_ly(
  fill = "toself",
  lon = c(-74, -70, -70, -74),
  lat = c(47, 47, 45, 45),
  type = 'scattermapbox',
  marker = list(size = 10, color = 'orange'),
  fillcolor = 'color') %>%
  layout(
    mapbox = list(
      style = "stamen-terrain",
      center = list(lon = -73, lat = 46),
      zoom = 5),
    showlegend = FALSE)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="scattermapbox-fill-to-self")
chart_link

Provide Gaps on Map

The following example shows how to use missing values in your data to provide gap in your graph. To ignore the gap on your plot, take benefit of connectorgaps attribute.

library(plotly)

p <- plot_ly(
  mode = "lines",
  fill = "toself",
  type = 'scattermapbox',
  lon = c(-10, -10, 8, 8, NaN, 30, 30, 50, 50, NaN, 100, 100, 80, 80),
  lat = c(30, 6, 6, 30, NaN, 20, 30, 30, 20, NaN, 40, 50, 50, 40)) %>%
layout(
  mapbox = list(
    style = "stamen-terrain",
    center = list(lon = 30, lat = 30),
    zoom = 2),
  showlegend = FALSE)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="gap-scattermapbox")
chart_link

Use the Corresponding Geojson

The second way is using Scattermapbox trace with the corresponding geojson.

library(plotly)

p <- plot_ly(
  type = 'scattermapbox',
  mode = "markers",
  lon = c(-73.605), lat = c(45.51),
  marker = list(size = 20, color = c("cyan"))) %>%
  layout(
    mapbox = list(
    style = "stamen-terrain",
    center = list(lon = -73.6, lat = 45.5),
    zoom = 12,
    layers = list(list(
    source = list(
      type = "FeatureCollection",
      features = list(list(
        type = "Feature",
        geometry = list(
          type = "MultiPolygon",
          coordinates = list(list(list(
            c(-73.606352888, 45.507489991), c(-73.606133883, 45.50687600),
            c(-73.605905904, 45.506773980), c(-73.603533905, 45.505698946),
            c(-73.602475870, 45.506856969), c(-73.600031904, 45.505696003),
            c(-73.599379992, 45.505389066), c(-73.599119902, 45.505632008),
            c(-73.598896977, 45.505514039), c(-73.598783894, 45.505617001),
            c(-73.591308727, 45.516246185), c(-73.591380782, 45.516280145),
            c(-73.596778656, 45.518690062), c(-73.602796770, 45.521348046),
            c(-73.612239983, 45.525564037), c(-73.612422919, 45.525642061),
            c(-73.617229085, 45.527751983), c(-73.617279234, 45.527774160),
            c(-73.617304713, 45.527741334), c(-73.617492052, 45.527498362),
            c(-73.617533258, 45.527512253), c(-73.618074188, 45.526759105),
            c(-73.618271651, 45.526500673), c(-73.618446320, 45.526287943),
            c(-73.618968507, 45.525698560), c(-73.619388002, 45.525216750),
            c(-73.619532966, 45.525064183), c(-73.619686662, 45.524889290),
            c(-73.619787038, 45.524770086), c(-73.619925742, 45.524584939),
            c(-73.619954486, 45.524557690), c(-73.620122362, 45.524377961),
            c(-73.620201713, 45.524298907), c(-73.620775593, 45.523650879)
            )))
            )
          ))
        ),
      type = "fill", below = "traces", color = "royalblue"))))

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="Geojson-scattermapbox")
chart_link

Reference

See https://plot.ly/r/reference/#scattermapbox for more information and options!