简体   繁体   English

Vega-Lite Wilkinson Dot Plot,按第一位数字分组

[英]Vega-Lite Wilkinson Dot Plot, group by first digit

I see the code to create a Wilkinson Dot Plot in Vega-Lite:我看到了在 Vega-Lite 中创建 Wilkinson Dot Plot 的代码:

{
  "$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"}
  }
}

Creates a plot grouping the exact numbers, but I'd like the output to be by the first digit so really theres 7 vertical dots for 1, 4 vertical dots for 2, and 5 vertical dots for 3. I tried adding calculation: "str.map(x => x.charAt(0))" to the transform array so I could group by that, but was unsuccessful in my execution.创建一个 plot 对确切数字进行分组,但我希望 output 由第一个数字组成,所以实际上 1 有 7 个垂直点,2 有 4 个垂直点,3 有 5 个垂直点。我尝试添加calculation: "str.map(x => x.charAt(0))"到转换数组,以便我可以按此分组,但执行失败。 Any ideas appreciated!任何想法表示赞赏!

You were on the right track, except that calculate transforms cannot use arbitrary javascript code, but only the subset made available in Vega Expressions .您在正确的轨道上,除了计算转换不能使用任意 javascript 代码,而只能使用Vega Expressions中提供的子集。 So, for example, you could do something like this ( vega editor ):因此,例如,您可以执行以下操作( vega 编辑器):

{
  "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"}
  }
}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM