leaflet for R has now the option to use addMapPane
to stack leaflet objects in a determined order. This works fine for vector data but using it with addRasterImage
returns an error message. Is there a special options
to use a pane
with rasters?
library(leaflet)
library(raster)
r <- raster(ncol=1000, nrow=1000)
r[] <- runif(ncell(r),0,1)
extent(r) <- matrix(c(172, -37, 175, -38), nrow=2)
crs(r)<-sp::CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
pal <- colorNumeric(c("#0C2C84", "#41B6C4", "#FFFFCC"), values(r),
na.color = "transparent")
m <- leaflet(m) %>% addTiles() %>%
addMapPane("baseMap", zIndex = 410) %>%
addMapPane("baseSat", zIndex = 420) %>%
addMapPane("data", zIndex = 425) %>%
addMapPane("r", zIndex = 430) %>%
addProviderTiles(providers$Esri.WorldImagery, group = "Satellite imagery",
options = pathOptions(pane = "baseSat")) %>%
addProviderTiles(providers$Esri.WorldTerrain,
options = pathOptions(pane = "baseMap")) %>%
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R",
options = pathOptions(pane = "data")) %>%
addRasterImage(r, colors = pal, opacity = 0.8, project = FALSE,
options = pathOptions(pane = "r"))
Error in addRasterImage(., r, colors = pal, opacity = 0.8, project = FALSE, :
unused argument (options = pathOptions(pane = "r"))
Very late to answer the question but I had the same problem and found a solution.
There is a not-yet-merged pull request on GitHub for leaflet which allows for the options argument in addRasterImage()
. Currently, you can install this version of leaflet from here. To ensure that I could access this later or make changes, I downloaded this entire repo into a new folder, then you can open it in RStudio and run: devtools::install()
.
EDIT: The faster way to get a quick install now (without downloading a local copy of the package itself) is to run:
remotes::install_github("rstudio/leaflet", ref="joe/feature/raster-options")
Once this is done, the code that you already have should work fine. I have been using addRasterImage(..., options=leafletOptions(pane="r"))
and I get the desired result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With