如何根据其他列筛选值?

How can I filter values based on other columns?

如何 select 某些值 and/or 值以 vega-lite 中的其他列为条件?例如,下面我只想显示“c”列中具有“red”的值。

{
  "data": {
    "values": [
      {"a": "A", "b": 2, "c": "red"}, 
      {"a": "A", "b": 7, "c": "red"}, 
      {"a": "A", "b": 4, "c": "blue"},
      {"a": "B", "b": 1, "c": "blue"}, 
      {"a": "B", "b": 2, "c": "red"} 
    ]
  },
  "mark": "bar",
  "encoding": {
    "x": {"field": "a", "type": "nominal"},
    "y": {"aggregate": "average", "field": "b", "type": "quantitative"}
  }
}

我已经尝试根据 this vega github tutorial, and using the online vega editor"mark": "bar", 之前添加以下代码,但它不会过滤列 b。我想我也可以用它来过滤字符串。

"transform": {
    "filter": "datum.b > 3"
  },

关于多个过滤条件。

您可以使用过滤器转换来执行此操作:

{
  "data": {
    "values": [
      {"a": "A", "b": 2, "c": "red"},
      {"a": "A", "b": 7, "c": "red"},
      {"a": "A", "b": 4, "c": "blue"},
      {"a": "B", "b": 1, "c": "blue"},
      {"a": "B", "b": 2, "c": "red"}
    ]
  },
  "transform": [{"filter": "datum.c == 'red'"}],
  "mark": "bar",
  "encoding": {
    "x": {"field": "a", "type": "nominal"},
    "y": {"aggregate": "average", "field": "b", "type": "quantitative"}
  }
}