在此示例中,如何按Vegalite中的特定字段对颜色堆栈进行排序?

时间:2019-01-03 17:56:43

标签: sorting vega-lite

我试图查看如何对使用颜色通道对某些信息进行编码的条形图进行排序,并且在下面链接的该图中,嗯……我很困惑。

我正在尝试按“产量”对堆叠的颜色进行排序,因此产量最高的项目位于底部,但此处将基于“站点”的分组保持不变。

维加莱石有可能吗?

根据我读过in the documentation on sorting的信息,我认为这是处理排序的依据,但是我运气不高。

"encoding": {
  "color": {
    "type": "nominal",
    "field": "site",
    "sort": {
      "field":"yield",
      "op": "count",
      "order": "ascending"
    }
  },
  "x": {"type": "nominal", "field": "variety"},
  "y": {"type": "quantitative", "aggregate": "sum", "field": "yield"}

}

以这种方式对条形图进行排序需要做什么?

Here's the chart in Vega Editor

1 个答案:

答案 0 :(得分:0)

您可以按照https://vega.github.io/vega-lite/docs/stack.html#order

所述使用order频道
{
  "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
  "data": {"url": "data/barley.json"},
  "mark": "bar",
  "encoding": {
    "color": {"type": "nominal", "field": "site"},
    "y": {"type": "quantitative", "aggregate": "sum", "field": "yield"},
    "order": {"aggregate": "sum", "field": "yield", "type": "quantitative"}
  }
}

enter image description here