Skip to content

Support interactive legends for match_expr()-based layers in add_legend() #197

Open

Description

@danielvartan

add_legend(interactive = TRUE) currently requires classification to be a mapgl_classification object. This prevents interactive legends when a layer is styled with match_expr(), even though match_expr() is a common and valid way to map feature IDs to colors.

I prepared a reproducible example to show what I mean. The code is available here, with an interactive report here.

Image

Activity

  1. walkerke commented on Apr 13, 2026

    @walkerke
    Owner

    Thanks for the detailed example! Interactive legends do already work with match_expr(). The key is passing filter_column to tell the legend which column in the source data to filter on.

    Here's a minimal example:

      library(mapgl)
      library(sf)
    
      nc <- st_read(system.file("shape/nc.shp", package = "sf"))
    
      nc$region <- cut(
        nc$BIR74,
        breaks = c(0, 1000, 5000, 15000, Inf),
        labels = c("Low", "Medium", "High", "Very High")
      )
    
      colors <- c("#eff3ff", "#6baed6", "#2171b5", "#08306b")
    
      maplibre(bounds = nc) |>
        add_fill_layer(
          id = "counties",
          source = nc,
          fill_color = match_expr(
            column = "region",
            values = c("Low", "Medium", "High", "Very High"),
            stops = colors
          ),
          fill_opacity = 0.7,
          fill_outline_color = "white"
        ) |>
        add_legend(
          legend_title = "Births (1974)",
          values = c("Low", "Medium", "High", "Very High"),
          colors = colors,
          type = "categorical",
          layer_id = "counties",
          filter_column = "region",
          interactive = TRUE
        )
    Image

    As of right now, we don't have a classification helper for match expressions, so that argument won't apply in this case.

  2. danielvartan commented on Apr 13, 2026

    @danielvartan
    ContributorAuthor

    Thank you for the example.

    Your example works when the source has the category field as a vector layer, but not when a static source is used.

    Unfortunately, this does not work in my case.

    In the example I used a precompiled PMTiles file based on commonly used tiles in geospatial research in Brazil, sourced from the geobr R package (see the PMTilesBR project). Using a standard, precompiled tile source is very useful because it allows you to build lightweight applications without the need to serve your own tiles on the web.

    In this case, the hosted file does not have the category field as a vector layer. The styling is done by assigning a value (in this case, a hex color) to each hex address, so adding a filter_column does not work: the classification is not part of the source tile.

  3. walkerke commented on Apr 14, 2026

    @walkerke
    Owner

    ok I see what you're saying - right now the implementation requires that you have a pre-defined column to filter on. I'll think that through as it'll take a bit more infrastructure work to hook that up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions