Vega-Lite Wilkinson 点图,按第一位数字分组

Vega-Lite Wilkinson Dot Plot, group by first digit

我看到了在 Vega-Lite 中创建威尔金森点图的代码:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "A Wilkinson Dot Plot",
  "height": 100,
  "data": {
    "values": [
      10,11,11,11,14,15,17,
      22,25,26,28,
      33,33,33,34,37
    ]
  },
  "transform": [{
    "window": [{"op": "rank", "as": "id"}],
    "groupby": ["data"]
  }],
  "mark": {
    "type": "circle",
    "opacity": 1
  },
  "encoding": {
    "x": {"field": "data", "type": "ordinal"},
    "y": {"field": "id", "type": "ordinal", "axis": null, "sort": "descending"}
  }
}

创建一个对精确数字进行分组的绘图,但我希望输出是第一个数字,所以实际上 1 有 7 个垂直点,2 有 4 个垂直点,3 有 5 个垂直点。我试过了将 calculation: "str.map(x => x.charAt(0))" 添加到变换数组,以便我可以按其分组,但我的执行没有成功。任何想法表示赞赏!

你走在正确的轨道上,除了计算转换不能使用任意 javascript 代码,而只能使用 Vega Expressions. So, for example, you could do something like this (vega editor 中提供的子集):

{
  "data": {
    "values": [10, 11, 11, 11, 14, 15, 17, 22, 25, 26, 28, 33, 33, 33, 34, 37]
  },
  "transform": [
    {"calculate": "floor(datum.data / 10)", "as": "data"},
    {
      "window": [{"op": "rank", "field": "data", "as": "id"}],
      "groupby": ["data"]
    }
  ],
  "mark": {"type": "circle", "opacity": 1},
  "encoding": {
    "x": {"field": "data", "type": "ordinal"},
    "y": {"field": "id", "type": "ordinal", "axis": null, "sort": "descending"}
  }
}