When I use addLayersControl() to allow users to choose between different base maps, rasters added with addRasterImage() aren't shown. The following code reproduces this issue for me. When I remove the addLayersControl() call, the raster displays on the map as desired, but with it the raster doesn't show up.
library(leaflet)
library(raster)
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")
leaflet() %>%
addTiles(group = "Map") %>%
addProviderTiles("Esri.WorldImagery", group = "Satellite") %>%
addLayersControl(
baseGroups = c("Map", "Satellite"),
options = layersControlOptions(collapsed = FALSE)
) %>%
addRasterImage(r)
Give a group name to the raster layer too and specify it as an overlay group in addLayersControl
Thanks, @bhaskarvk, I did try that, but still having the same problem:
library(leaflet)
library(raster)
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")
leaflet() %>%
addTiles(group = "Map") %>%
addProviderTiles("Esri.WorldImagery", group = "Satellite") %>%
addLayersControl(
baseGroups = c("Map", "Satellite"),
overlayGroups = c("Raster"),
options = layersControlOptions(collapsed = FALSE)
) %>%
addRasterImage(r, group = "Raster")
@mstrimas it works if you change the order of the call so that addLayersControl gets added last.
library(leaflet)
library(raster)
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")
leaflet() %>%
addTiles(group = "Map") %>%
addProviderTiles("Esri.WorldImagery", group = "Satellite") %>%
addRasterImage(r, group = "Raster") %>%
addLayersControl(
baseGroups = c("Map", "Satellite"),
overlayGroups = c("Raster"),
options = layersControlOptions(collapsed = FALSE)
)
Most helpful comment
@mstrimas it works if you change the order of the call so that
addLayersControlgets added last.