标记内数据点的聚合

问题描述 投票:0回答:1

使用 Vega-Lite 我可以在编码中使用聚合,如下所示:

{
  "data": {
    "name": "thedata",
    "values": [
      {"id": "house1", "no of rooms": 2},
      {"id": "house1", "no of rooms": 3},
      {"id": "house2", "no of rooms": 4},
      {"id": "house3", "no of rooms": 8}
    ]
  },
  "transform": [
    {"calculate": "datum['no of rooms'] <= 5 ? 'small' : 'large'", "as": "myType"}
  ],
  "mark": {
    "type": "circle"
  },
  "encoding": {
    "y": {
      "field": "id", "type": "nominal"
    },
    "x": {
      "field": "no of rooms", "type": "quantitative",
      "aggregate": "mean"
    },
    "size": {"value": 200}
  }
}

现在我想知道使用 Vega 是否可以实现这一点。或者我是否必须重新考虑我的方法并在转换内或标记部分内创建聚合?

我天真的方法是使用

"x": "signal": "scale(myscale, sum(datum['no of rooms']))"

您能给我一些指导吗?

json charts visualization vega-lite vega
1个回答
0
投票

答案是否定的。 VL 提供了更简洁的语法,可转换为 Vega。在这些情况下,查看正在生成的 Vega 是很有用的。在编辑器中,单击“编辑 Vega 规格”即可查看它。这里使用了聚合变换。

enter image description here

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "background": "white",
  "padding": 5,
  "width": 200,
  "style": "cell",
  "data": [
    {
      "name": "thedata",
      "values": [
        {"id": "house1", "no of rooms": 2},
        {"id": "house1", "no of rooms": 3},
        {"id": "house2", "no of rooms": 4},
        {"id": "house3", "no of rooms": 8}
      ]
    },
    {
      "name": "data_0",
      "source": "thedata",
      "transform": [
        {
          "type": "formula",
          "expr": "datum['no of rooms'] <= 5 ? 'small' : 'large'",
          "as": "myType"
        },
        {
          "type": "aggregate",
          "groupby": ["id"],
          "ops": ["mean"],
          "fields": ["no of rooms"],
          "as": ["mean_no of rooms"]
        },
        {
          "type": "filter",
          "expr": "isValid(datum[\"mean_no of rooms\"]) && isFinite(+datum[\"mean_no of rooms\"])"
        }
      ]
    }
  ],
  "signals": [
    {"name": "y_step", "value": 20},
    {
      "name": "height",
      "update": "bandspace(domain('y').length, 1, 0.5) * y_step"
    }
  ],
  "marks": [
    {
      "name": "marks",
      "type": "symbol",
      "style": ["circle"],
      "from": {"data": "data_0"},
      "encode": {
        "update": {
          "fill": {"value": "#4c78a8"},
          "ariaRoleDescription": {"value": "circle"},
          "description": {
            "signal": "\"Mean of no of rooms: \" + (format(datum[\"mean_no of rooms\"], \"\")) + \"; id: \" + (isValid(datum[\"id\"]) ? datum[\"id\"] : \"\"+datum[\"id\"])"
          },
          "x": {"scale": "x", "field": "mean_no of rooms"},
          "y": {"scale": "y", "field": "id"},
          "size": {"value": 200},
          "shape": {"value": "circle"}
        }
      }
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "linear",
      "domain": {"data": "data_0", "field": "mean_no of rooms"},
      "range": [0, {"signal": "width"}],
      "nice": true,
      "zero": true
    },
    {
      "name": "y",
      "type": "point",
      "domain": {"data": "data_0", "field": "id", "sort": true},
      "range": {"step": {"signal": "y_step"}},
      "padding": 0.5
    }
  ],
  "axes": [
    {
      "scale": "x",
      "orient": "bottom",
      "gridScale": "y",
      "grid": true,
      "tickCount": {"signal": "ceil(width/40)"},
      "domain": false,
      "labels": false,
      "aria": false,
      "maxExtent": 0,
      "minExtent": 0,
      "ticks": false,
      "zindex": 0
    },
    {
      "scale": "x",
      "orient": "bottom",
      "grid": false,
      "title": "Mean of no of rooms",
      "labelFlush": true,
      "labelOverlap": true,
      "tickCount": {"signal": "ceil(width/40)"},
      "zindex": 0
    },
    {"scale": "y", "orient": "left", "grid": false, "title": "id", "zindex": 0}
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.