簡體   English   中英

Vega-Lite Wilkinson Dot Plot,按第一位數字分組

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

我看到了在 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"}
  }
}

創建一個 plot 對確切數字進行分組,但我希望 output 由第一個數字組成,所以實際上 1 有 7 個垂直點,2 有 4 個垂直點,3 有 5 個垂直點。我嘗試添加calculation: "str.map(x => x.charAt(0))"到轉換數組,以便我可以按此分組,但執行失敗。 任何想法表示贊賞!

您在正確的軌道上,除了計算轉換不能使用任意 javascript 代碼,而只能使用Vega Expressions中提供的子集。 因此,例如,您可以執行以下操作( 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