Search code examples
vega-lite

How to remove an item from the legend in Vega-Lite


Is it possible to remove an item from the legend? The number of categories in the legend is dynamic, so I can't use manually-entered list of entries for the legend.

If I use

"labelExpr": "datum.label == '_Support' ? null : datum.label",

it will remove the label, but keep its symbol.

Before suppresion

After suppression


Solution

  • I have hidden a symbol for x here:

    enter image description here

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "data": {
        "values": [
          {"category": "A", "group": "x", "value": 0.1},
          {"category": "A", "group": "y", "value": 0.6},
          {"category": "A", "group": "z", "value": 0.9},
          {"category": "B", "group": "x", "value": 0.7},
          {"category": "B", "group": "y", "value": 0.2},
          {"category": "B", "group": "z", "value": 1.1},
          {"category": "C", "group": "x", "value": 0.6},
          {"category": "C", "group": "y", "value": 0.1},
          {"category": "C", "group": "z", "value": 0.2}
        ]
      },
      "mark": "bar",
      "encoding": {
        "x": {"field": "category"},
        "y": {"field": "value", "type": "quantitative"},
        "xOffset": {"field": "group"},
        "color": {
          "field": "group",
          "legend": {"symbolSize": {"expr": "datum.label=='x'?0:100"}}
        }
      }
    }