Vega-lite:如何显示多层散点图的多个 "color" 图例?

Vega-lite: How to show multiple "color" legends for multi-layer scatter plot?

我的 vega lite json: Open the Chart in the Vega Editor

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "layer": [
    {
      "data": {"url": "data/cars.json"},
      "params": [
        {
          "name": "grid",
          "select": "interval",
          "bind": "scales"
        }
      ],
      "mark": "circle",
      "encoding": {
        "x": {"field": "Horsepower", "type": "quantitative"},
        "y": {"field": "Miles_per_Gallon", "type": "quantitative"},
        "color": {
          "field": "Horsepower",
          "type": "quantitative",
          "scale": {
            "range": ["blue", "blue"]
          }
        }
      }
    },


    {
      "data": {"url": "data/cars.json"},
      "mark": "circle",
      "encoding": {
        "x": {"field": "Miles_per_Gallon", "type": "quantitative"},
        "y": {"field": "Acceleration", "type": "quantitative"},
        "color": {
          "field": "Displacement",
          "type": "quantitative",
          "scale": {
            "range": ["black", "black"]
          }
        }
      }
    }
  ]
}

我要显示 2 层散点图,所以我希望它有 2 个图例颜色条。 当我使用“颜色”时,第二个图例被合并,并被第一个图例覆盖。

当我改为使用“填充”时,我设法拥有 2 个不同的颜色条。但是如果我有4层怎么办呢?

您需要添加 resolve 属性.

 "resolve": {"legend":{"color": "independent"}, "scale": {"color": "independent"} }

vega 编辑器上的示例: Open the Chart in the Vega Editor