是否可以对图例应用与颜色编码相同的条件

Is it possible to apply the same condition as color encoding for legend

我的源代码如下

  "transform": [
    
    {
      "window": [
        {
          "op": "rank", "field": "Value", "as": "_rank"
        }
      ],
      "sort": [
        {
          "field": "Value",
          "order": "descending"
        }
      ]
    }
  ],
  "encoding": {
    "color": {
      "field": "_rank",
      "condition": {
        "test": "datum._rank>5",
        "value": "grey"
      }
    },
    "x": {
      "field": "Week",
      "type": "nominal",
      "axis": {
        "labelAngle": 0
      }
    },
    "y": {
      "field": "Value",
      "type": "quantitative",
      "axis": {
        "grid": false
      }
    }
  },
  "layer": [
    {
      "mark": {
        "type": "bar",
        "tooltip": true
      }
    },
    {
      "mark": {
        "type": "text",
        "align": "center",
        "baseline": "middle",
        "dx": 0,
        "dy": -5,
        "tooltip": true
      },
      "encoding": {
        "text": {
          "field": "Value"
        }
      }
    }
  ]

我为颜色编码设置了一个条件,以显示除 top5 之外的所有内容以不同的颜色显示,并且任何不是 top5 的值都应该是灰色的。

"color": {
  "field": "_rank",
  "condition": {
    "test": "datum._rank>5",
    "value": "grey"
  }
}

对于条形图来说一切都很好,但图例不会在相同条件下生成。

是否也可以为图例的颜色扩展相同的 top5 逻辑?即在图例中任何 <5 的颜色(每个)都是灰色,其他所有颜色都与条件相同(当前这部分正在生成)

Editor

图例颜色将反映您指定的 color scale,而不反映条件。

最简单的方法可能是 setting the range 为您的配色方案;例如:

{
  "data": {"url": "data/cars.json"},
  "mark": "point",
  "encoding": {
    "x": {"field": "Horsepower", "type": "quantitative"},
    "y": {"field": "Miles_per_Gallon", "type": "quantitative"},
    "color": {
      "field": "Origin", "type": "nominal",
      "scale": {"range": ["purple", "#ff0000", "teal"]}
    }
  }
}

您必须根据数据中的颜色类别数量修改指定的颜色。