如何使用JavaScript使用transform.lookup在Vega-Lite Choropleth贴图(几何形状)中聚合颜色编码

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

我正在尝试使用JavaScript在vega-lite Choropleth贴图中汇总颜色编码。

I have made a working example to make it easier to help and understand

最小代码为:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "width": 600,
  "height": 500,
  "mark": "geoshape",
  "data": {
    "url": "https://gist.githubusercontent.com/benooghe/95c8b4d63f67f1856fdd81e6303c654e/raw/8ec1fdd91bfbf4973f97ffb8a5daacb8f431908e/geo_belgium.json",
    "format": {"type": "topojson", "feature": "data"}
  },
  "encoding": {
    "color": {
      "field": "properties.nis_code", 
      "type": "quantitative", 
      // "aggregate":"sum"               // <-- THIS BREAKS THE MAP
    }
  }
}

这是一个简化的版本,它可以工作,但是如果您在encoding.color中添加, "aggregate: "sum",那么它将不再起作用。

如果我过于简化,我的完整地图稍微复杂一点:它有一个transform.lookup

  let yourVlSpec = {
    "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
    "width": 600,
    "height": 500,
    "mark": "geoshape",
    "data": { // TOPOJSON DATA -----
      "url": 'geo_belgium.json',
      "format": {
        "type": "topojson",
        "feature": "data"
      }
    },
    "transform": [{ // LEFT JOIN DATA -----
      "lookup": "properties.nis_code",
      "from": {
        "data": {
          "url": "data.csv"
        },
        "key": "NIS5",
        "fields": ["Cases", "Deaths"]
      }
    }],
    "encoding": {
      "color": {
        "field": "Cases",
        "type": "quantitative",
        // "aggregate": "sum"      // <-- THIS BREAKS THE MAP
      }
    }
  };
  vegaEmbed('#vis', yourVlSpec);

编辑:我也在transform中尝试过此操作

"aggregate": [{"op": "sum", "field": 'Cases', "as": 'sum_cases'}],
javascript vega-lite
1个回答
0
投票

当您将和表示为编码的一部分时,它会按所有其他编码字段进行分组。在您的情况下,没有其他编码字段,因此它聚合everything并丢弃几何图形。

解决此问题的一种方法是将相关字段添加到detail编码,以便聚合不会从数据中删除它们(vega editor):

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "width": 600,
  "height": 500,
  "mark": "geoshape",
  "data": {
    "url": "https://gist.githubusercontent.com/benooghe/95c8b4d63f67f1856fdd81e6303c654e/raw/8ec1fdd91bfbf4973f97ffb8a5daacb8f431908e/geo_belgium.json",
    "format": {"type": "topojson", "feature": "data"}
  },
  "encoding": {
    "color": {"field": "properties.nis_code", "type": "quantitative", "aggregate": "sum"},
    "detail": [{"field": "geometry", "type": "nominal"}, {"field": "properties.id", "type": "nominal"}, {"field": "type", "type": "nominal"}]
  }
}

但是,在您的情况下,这与原始的未汇总图表相同,因为每个地理区域只有一个nis_code

如果要按几何区域以外的类别进行分组,最好的方法是使用joinaggregate转换(不会删除未命名字段),然后按您希望分组的特定数据进行分组。例如,这是按nis_code功能(level_2)分组的vega editor之和:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "width": 600,
  "height": 500,
  "mark": "geoshape",
  "data": {
    "url": "https://gist.githubusercontent.com/benooghe/95c8b4d63f67f1856fdd81e6303c654e/raw/8ec1fdd91bfbf4973f97ffb8a5daacb8f431908e/geo_belgium.json",
    "format": {"type": "topojson", "feature": "data"}
  },
  "transform" : [{
    "joinaggregate": [{"field": "properties.nis_code", "op": "sum", "as": "sum_nis_code"}],
    "groupby": ["properties.level_2"]
  }],
  "encoding": {
    "color": {"field": "sum_nis_code", "type": "quantitative"}
  }
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.