Extends Leaflet for R by adding widget to control opacity of multiple layers, based on Leaflet.Control.Opacity (dayjournal/Leaflet.Control.Opacity) JavaScript plugin with some modifications to support layers other than tile layers (currently tested: tile, image and marker).


Before you begin, ensure you have met the following requirement(s):

  • You have R >= 3.5.0 installed.

Install the latest version released on CRAN:


Or install the development version from GitHub:

if (!require("remotes")) install.packages("remotes")

Basic Usage

leaflet.multiopacity provides the function addOpacityControls to include opacity sliders in leaflet map.

# Load libraries

# Create raster example
r <- raster(xmn = -2.8, xmx = -2.79,
            ymn = 54.04, ymx = 54.05,
            nrows = 30, ncols = 30)
values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE)
crs(r) <- crs("+init=epsg:4326")

# Provide layerId, group or category to show opacity controls
# If not specified, will render controls for all layers
leaflet() %>%
  addProviderTiles("Wikimedia", layerId = "Wikimedia") %>%
  addRasterImage(r, layerId = "raster") %>%
  addAwesomeMarkers(lng = -2.79545, lat = 54.04321,
                    layerId = "hospital", label = "Hospital") %>%
  addOpacityControls(layerId = c("raster", "hospital"),
                     collapsed = FALSE, position = "topright",
                     title = "Opacity Control")

For more information and examples, please take a look at the vignettes:

Usage with leaflet

Usage with leafletProxy

Contributing to leaflet.multiopacity

To contribute to leaflet.multiopacity, follow these steps:

  1. Fork this repository.
  2. Create a branch: git checkout -b <branch_name>.
  3. Make your changes and commit them: git commit -m '<commit_message>'
  4. Push to the original branch: git push origin leaflet.multiopacity/<location>
  5. Create the pull request.

Alternatively see the GitHub documentation on creating a pull request.

Bug Reports

If you have detected a bug (or want to ask for a new feature), please file an issue with a minimal reproducible example on GitHub.


This project uses the following license: MIT License.