我正在尝试使用 Vega-Lite 将离散数据集及其平均值组合到一个图表中。
问题是轴必须在一个固定域内,并且在平均聚合数据上分层会弄乱这些轴。在下图中,上 x 轴由平均聚合数据层创建,下 x 轴由域约束层创建。
我需要域约束,因为轴必须始终显示域的最小值和最大值以及特殊的离散间隔(在此示例中为 10 和 50)。数据将始终是这些离散值之一。
如何确保两组数据都绘制在受域约束的轴上?
这是简化代码 (and here it is in the Vega editor)。
_entrypoint
答案 0 :(得分:1)
我尝试了一种变体,其中两个层的轴同步并且最大范围值为 100。请参阅 link 或以下代码,让我知道这是否有帮助。
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "",
"datasets": {
"data": [
{"x": 0, "y": 100, "size": 1},
{"x": 50, "y": 100, "size": 1},
{"x": 50, "y": 0, "size": 1},
{"x": 20, "y": 20, "size": 1}
]
},
"layer": [
{
"data": {"name": "data"},
"mark": {"type": "point", "filled": true, "color": "#a7f3d0"},
"encoding": {
"size": {"field": "size", "type": "ordinal", "legend": null},
"x": {
"field": "x",
"type": "quantitative",
"title": "",
"scale": {"domainMax": 100}
},
"y": {"field": "y", "type": "quantitative", "title": ""}
}
},
{
"data": {"name": "data"},
"mark": {"type": "point", "shape": "diamond"},
"encoding": {
"x": {
"field": "x",
"aggregate": "mean",
"title": "",
"type": "quantitative"
},
"y": {
"field": "y",
"aggregate": "mean",
"title": "",
"axis": null,
"type": "quantitative"
}
}
}
],
"resolve": {"axis": {"x": "independent", "y": "independent"}},
"config": {"view": {"stroke": "lightslategray"}}
}