This function creates a comparison view between two Mapbox GL or Maplibre GL maps, allowing users to either swipe between the two maps or view them side-by-side with synchronized navigation.
map1: A mapboxgl or maplibre object representing the first map.
map2: A mapboxgl or maplibre object representing the second map.
width: Width of the map container.
height: Height of the map container.
elementId: An optional string specifying the ID of the container for the comparison. If NULL, a unique ID will be generated.
mousemove: A logical value indicating whether to enable swiping during cursor movement (rather than only when clicked). Only applicable when mode="swipe".
orientation: A string specifying the orientation of the swiper or the side-by-side layout, either "horizontal" or "vertical".
mode: A string specifying the comparison mode: "swipe" (default) for a swipeable comparison with a slider, or "sync" for synchronized maps displayed next to each other.
Returns
A comparison widget.
Details
Comparison modes
The compare() function supports two modes:
mode="swipe" (default) - Creates a swipeable interface with a slider to reveal portions of each map
mode="sync" - Places the maps next to each other with synchronized navigation
In both modes, navigation (panning, zooming, rotating, tilting) is synchronized between the maps.
Using the compare widget in Shiny
The compare widget can be used in Shiny applications with the following functions:
mapboxglCompareOutput() / renderMapboxglCompare() - For Mapbox GL comparisons
maplibreCompareOutput() / renderMaplibreCompare() - For Maplibre GL comparisons
mapboxgl_compare_proxy() / maplibre_compare_proxy() - For updating maps in a compare widget
After creating a compare widget in a Shiny app, you can use the proxy functions to update either the "before" (left/top) or "after" (right/bottom) map. The proxy objects work with all the regular map update functions like set_style(), set_paint_property(), etc.
To get a proxy that targets a specific map in the comparison: