Black Lives Matter. Please consider donating to Black Girls Code today.
https://www.blackgirlscode.com/

Filled Area in Mapbox in R

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


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Mapbox Access Token

To plot on Mapbox maps with Plotly you may need a Mapbox account and a public Mapbox Access Token. 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 the Scattermapbox trace and sets fill = 'toself'

library(plotly)

fig <- 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') 
fig <- fig %>%
  layout(
    mapbox = list(
      style = "stamen-terrain",
      center = list(lon = -73, lat = 46),
      zoom = 5),
    showlegend = FALSE)

fig

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)

fig <- 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)) 
fig <- fig %>%
layout(
  mapbox = list(
    style = "stamen-terrain",
    center = list(lon = 30, lat = 30),
    zoom = 2),
  showlegend = FALSE)

fig

Use the Corresponding Geojson

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

library(plotly)

fig <- plot_ly(
  type = 'scattermapbox',
  mode = "markers",
  lon = c(-73.605), lat = c(45.51),
  marker = list(size = 20, color = c("cyan"))) 
fig <- fig %>%
  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"))))

fig

Reference

See https://plotly.com/r/reference/#scattermapbox for more information and options!

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)