如何在 Vega-Lite 中构建预先计算的直方图?
How to build pre-calculated histogram in Vega-Lite?
VegaLite 可以自己装箱和聚合。但是我计算复杂,单独建直方图
结果数据如下
bins = [1, 2, 3, 4] // 4 edges
// |1-2|2-3|3-4| // 3 bars
counts = [1, 2, 1]
问题是 - 如何正确显示条形边缘 - 有 3 个条形,但有 4 个边缘。
您可以使用 x
和 x2
编码指定 bin 起点和终点。指定 bin='binned'
也很有帮助,它告诉 Vega-Lite 数据已预先装箱并触发规范中出现装箱操作时使用的相同显示默认值。例如 (editor link):
{
"data": {
"values": [
{"bin1": 1, "bin2": 2, "counts": 1},
{"bin1": 2, "bin2": 3, "counts": 2},
{"bin1": 3, "bin2": 4, "counts": 1}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "bin1", "type": "quantitative", "bin": "binned"},
"x2": {"field": "bin2"},
"y": {"field": "counts", "type": "quantitative"}
}
}
有关详细信息,请参阅 Using Vega-Lite with Binned data。
VegaLite 可以自己装箱和聚合。但是我计算复杂,单独建直方图
结果数据如下
bins = [1, 2, 3, 4] // 4 edges
// |1-2|2-3|3-4| // 3 bars
counts = [1, 2, 1]
问题是 - 如何正确显示条形边缘 - 有 3 个条形,但有 4 个边缘。
您可以使用 x
和 x2
编码指定 bin 起点和终点。指定 bin='binned'
也很有帮助,它告诉 Vega-Lite 数据已预先装箱并触发规范中出现装箱操作时使用的相同显示默认值。例如 (editor link):
{
"data": {
"values": [
{"bin1": 1, "bin2": 2, "counts": 1},
{"bin1": 2, "bin2": 3, "counts": 2},
{"bin1": 3, "bin2": 4, "counts": 1}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "bin1", "type": "quantitative", "bin": "binned"},
"x2": {"field": "bin2"},
"y": {"field": "counts", "type": "quantitative"}
}
}
有关详细信息,请参阅 Using Vega-Lite with Binned data。