R 中 2 个光栅图像的传单并排

leaflet side-by-side for 2 raster images in R

{leaflet.extras2} implements the leaflet side-by-side 插件并提供这个最小示例:

library(leaflet)
library(leaflet.extras2)

leaflet(quakes) %>%
  addMapPane("left", zIndex = 0) %>%
  addMapPane("right", zIndex = 0) %>%
  addTiles(group = "base", layerId = "baseid",
           options = pathOptions(pane = "right")) %>%
  addProviderTiles(providers$CartoDB.DarkMatter, group="carto", layerId = "cartoid",
                   options = pathOptions(pane = "left")) %>%
  addCircleMarkers(data = breweries91[1:15,], color = "blue", group = "blue",
                   options = pathOptions(pane = "left")) %>%
  addCircleMarkers(data = breweries91[15:20,], color = "yellow", group = "yellow") %>%
  addCircleMarkers(data = breweries91[15:30,], color = "red", group = "red",
                   options = pathOptions(pane = "right")) %>%
  addLayersControl(overlayGroups = c("blue","red", "yellow")) %>%
  addSidebyside(layerId = "sidecontrols",
                rightId = "baseid",
                leftId = "cartoid")

但是适配两张raster图片时,遇到错误:

Error in addRasterImage(., r2, colors = pal2, options = pathOptions(pane = "right"),  : 
  unused argument (options = pathOptions(pane = "right"))

这是一个可重现的例子:

library(raster)
library(leaflet)
library(leaflet.extras2)
library(rcartocolor)

# example raster data
r1 <- r2 <- raster(system.file("external/test.grd", package = "raster"))

# color palettes
pal1 <- colorNumeric(carto_pal(name = "OrYel"), 
                     values(r1), na.color = "transparent")
pal2 <- colorNumeric(carto_pal(name = "BluYl"), 
                     values(r2), na.color = "transparent")

# side by side map
leaflet() %>% 
  addMapPane("left",  zIndex = 0) %>%
  addMapPane("right", zIndex = 0) %>%
  addTiles(group = "base", layerId = "baseid",
           options = pathOptions(pane = "right")) %>%
  addTiles(group = "carto", layerId = "cartoid",
           options = pathOptions(pane = "left")) %>%
  addRasterImage(r1, colors = pal1, 
                 options = pathOptions(pane = "right"), group = "r1") %>%
  addRasterImage(r2, colors = pal2, 
                 options = pathOptions(pane = "right"), group = "r2") %>%
  addLayersControl(overlayGroups = c("r1", "r2")) %>% 
  addSidebyside(layerId = "sidecontrols",
                rightId = "baseid",
                leftId  = "cartoid")

解决方案由@RexParsons 指出
您将需要 addRasterImageoptions = leafletOptions(pane = ""),它将在下一个版本中添加到 {leaflet}
第 1 步:现在您可以安装 raster-options:

remotes::install_github("rstudio/leaflet", ref="joe/feature/raster-options")

第 2 步:Session -> Restart R

第 3 步:添加 options = leafletOptions(pane = ""):

leaflet() |> 
  addMapPane("right", zIndex = 0) |> 
  addMapPane("left",  zIndex = 0) |>
  addTiles(group = "base", layerId = "baseid1", options = pathOptions(pane = "right")) |> 
  addTiles(group = "base", layerId = "baseid2", options = pathOptions(pane = "left")) |> 
  addRasterImage(x = r1, colors = pal1, options = leafletOptions(pane = "right"), group = "r1") |> 
  addRasterImage(x = r2, colors = pal2, options = leafletOptions(pane = "left"), group = "r2") |> 
  addLayersControl(overlayGroups = c("r1", "r2")) |>  
  addSidebyside(layerId = "sidecontrols",
                rightId = "baseid1",
                leftId  = "baseid2")

输出:

选项 2(addProviderTiles):

leaflet() |> 
  addMapPane("right", zIndex = 0) |> 
  addMapPane("left",  zIndex = 0) |>
  addTiles(group = "base", layerId = "baseid", options = pathOptions(pane = "right")) |>
  addProviderTiles(providers$CartoDB, group="carto", layerId = "cartoid", options = pathOptions(pane = "left")) |> 
  addRasterImage(x = r1, colors = pal1, options = leafletOptions(pane = "right"), group = "r1") |> 
  addRasterImage(x = r2, colors = pal2, options = leafletOptions(pane = "left"), group = "r2") |> 
  addLayersControl(overlayGroups = c("r1", "r2")) |>  
  addSidebyside(layerId = "sidecontrols",
                rightId = "baseid",
                leftId  = "cartoid")

输出:

对你有用吗?