javascript - 如何使用 JavaScript 在带有 transform.lookup 的 Vega-Lite Choropleth Map (geoshape) 中聚合颜色编码
问题描述
我正在尝试使用 JavaScript 在 vega-lite Choropleth 地图中聚合颜色编码。
最小的代码是:
{
"$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
}
}
}
这是一个简化的版本,它可以工作,但如果你添加, "aggregate: "sum"
encoding.color,那么它就不再工作了。
如果我过于简单化,我的完整地图会稍微复杂一点:它有一个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'}],
EDIT2: data.csv 中的数据如下所示:
Date,PostCode,Cases,Deaths,NIS5
2020-04-08,2470,1,,13036
2020-04-08,2430,1,,13053
2020-04-08,1457,1,,25124
2020-04-08,3212,1,,24066
2020-04-08,2400,1,,13025
2020-04-08,1651,1,,23003
2020-04-08,2360,1,,13031
2020-04-07,1070,22,12,21004
2020-04-07,1070,22,12,21001
2020-04-07,4000,24,11,62093
2020-04-07,4000,24,11,62063
2020-04-01,9320,1,,41002
2020-04-01,7380,1,,53068
2020-04-01,9308,1,,41002
2020-03-31,1070,34,7,21004
2020-03-31,1070,34,7,21001
2020-03-31,3500,7,7,71022
2020-03-31,3800,10,5,71053
2020-03-31,4000,28,4,62063
2020-03-31,4000,28,4,62093
我正在寻找每个“NIS5”的“案例”总和,总结所有日期
解决方案
当您将总和表示为编码的一部分时,它会按所有其他编码字段分组。在您的情况下,没有其他编码字段,因此它聚合所有内容并丢弃几何。
解决此问题的一种方法是将相关字段添加到detail
编码中,以便聚合不会将它们从数据中删除(vega 编辑器):
{
"$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"}
}
}
推荐阅读
- python - 服务层中的 Python Flask 事务控制
- python - 基于系列的布尔值删除数据帧索引
- c++ - 继承构造函数的字符和访问级别
- c++ - 如何获取网格的外边缘(仅属于一个三角形的边缘)
- java - 修改同步块中的对象(例如 HashSet)是否等同于使其成为同步对象?
- python - 有没有更简单的方法来抓取 br 标签?
- javascript - 是否有任何用于网络摄像头的人脸 AR SDK 或图像处理 SDK?
- python - 让 Visual Studio 和 python 加载 conda 模块的问题
- javascript - 如何使我的 JavaScript 搜索高级?
- r - 条件配对 t 检验