I’m excited to share some of the new features in the latest release of mapgl (v0.2.1). This update brings some significant enhancements to comparison maps, layer controls, and legends that make the package even more powerful for interactive mapping applications.
Synchronized side-by-side maps with mode = "sync"
Several users have requested the ability to display maps side-by-side in a synchronized view rather than the default swipe comparison in compare(). The new mode = "sync" parameter in compare() delivers exactly that.
Here’s a basic example of how you can create synchronized side-by-side maps that show a different map style and styled layer:
library(mapgl)library(sf)# Get some spatial datanc <-st_read(system.file("shape/nc.shp", package="sf"))
Reading layer `nc' from data source
`/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/sf/shape/nc.shp'
using driver `ESRI Shapefile'
Simple feature collection with 100 features and 14 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
Geodetic CRS: NAD27
# Create first map with one styling approachm1 <-maplibre(style =carto_style("positron")) |>fit_bounds(nc, animate =FALSE) |>add_fill_layer(id ="nc_fill",source = nc,fill_color ="blue",fill_opacity =0.6 )# Create second map with different stylingm2 <-maplibre(style =carto_style("dark-matter")) |>fit_bounds(nc, animate =FALSE) |>add_fill_layer(id ="nc_fill",source = nc,fill_color ="orange",fill_opacity =0.6 )# Create synchronized side-by-side comparisoncompare(m1, m2, mode ="sync")
The sync mode is an excellent way to compare different styling approaches, different datasets, or changes in data over time when you want to make sure each map shows in its entirety.
Comparison maps in Shiny
Another major enhancement is the ability to use comparison maps in Shiny applications, with full support for proxy-based updates. This means you can build dynamic applications where users can toggle between different layers or styles on either side of a comparison.
Users have been requesting this feature for a while - and admittedly it was a huge development lift! compare() works as a separate htmlwidget within the package, so I’ve had to build out dedicated Shiny infrastructure for it.
Here’s a simple app that demonstrates how you can use a comparison map in Shiny with a color picker for each side:
What’s really powerful about this setup is how you can target each side of the comparison independently using the map_side parameter in the proxy functions. The “before” value targets the left/top map, while “after” targets the right/bottom map.
Better styling and legend linking for the layers control
The layers control has received significant improvements in this release, with better styling, positioning behavior, and the ability to link legends to specific layers. When a layer is toggled in the layers control, its associated legend will now automatically show or hide.
Here’s how you can create a map with multiple layers, each with its own legend: