英文:
Static Ranges for X Axis and Counting
问题
以下是您要的翻译内容:
I'm trying to manually define ranges for vega-lite and failing. I've read through documentation trying to understand span, band, scale but not finding any examples.
For my values listed I'm just trying to count how many of each field fall into the pre-defined range. Y axis is just a count of how many of each field falls into the manually defined ranges for the X axis.
For example, the ATR bar in the 1 to 15 range would have a value of 4 and 0 for the 15 to 30 range. ETO would have a value of 3 in the 1 to 15 range and 1 for the 15 to 30.
Any help would be appreciated.
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{"ATR": 1, "ETO": 10, "ITE": 20, "RTI": 15},
{"ATR": 3, "ETO": 4, "ITE": 8, "RTI": 4},
{"ATR": 8, "ETO": 23, "ITE": 1, "RTI": 6},
{"ATR": 7, "ETO": 9, "ITE": 4, "RTI": 11}
]
},
"transform": [
{"fold": ["ATR", "ETO", "ITE", "RTI"]}
],
"mark": "bar",
"encoding": {
"x": {
"type": "quantitative",
"scale": {
"domain": [1, 15], [15, 30]
}
},
"y": {"field": "value", "aggregate": "count", "type": "quantitative"},
"color": {"field": "key", "type": "nominal"}
}
}
I've tried understanding/plugging in various items from the vega lite documentation and working in the vega editor, but can't quite seem to get it right. If I add the "value" field to X axis, it just counts how many of each identifier has that value.
Edit: Update using Bin as suggested
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{"ATR": 1, "ETO": 10, "ITE": 20, "RTI": 15},
{"ATR": 3, "ETO": 4, "ITE": 8, "RTI": 4},
{"ATR": 8, "ETO": 23, "ITE": 1, "RTI": 6},
{"ATR": 7, "ETO": 9, "ITE": 4, "RTI": 11}
]
},
"transform": [
{"fold": ["ATR", "ETO", "ITE", "RTI"]},
{
"bin": {"binned": true, "steps": [15]},
"field": "value"
}
],
"mark": "bar",
"encoding": {
"x": {
"field": "value",
"type": "ordinal"
},
"y": {"aggregate": "count"},
"xOffset": {"field": "key"},
"color": {"field": "key", "type": "nominal"}
}
}
英文:
I'm trying to manually define ranges for vega-lite and failing. I've read through documentation trying to understand span, band, scale but not finding any examples.
For my values listed I'm just trying to count how many of each field fall into the pre defined range.
Y axis is just a count of how many of each field falls into the manually defined ranges for the X axis.
For example. the ATR bar in the 1 to 15 range would have a value of 4 and 0 for the 15 to 30 range.
ETO would have a value of 3 in the 1 to 15 range and 1 for the 15 to 30.
Any help would be appreciated.
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{"ATR": 1, "ETO": 10, "ITE": 20, "RTI": 15},
{"ATR": 3, "ETO": 4, "ITE": 8, "RTI": 4},
{"ATR": 8, "ETO": 23, "ITE": 1, "RTI": 6},
{"ATR": 7, "ETO": 9, "ITE": 4, "RTI": 11}
]
},
"transform": [
{"fold": ["ATR", "ETO", "ITE", "RTI"]}
],
"mark": "bar",
"encoding": {
"x": {
"type": "quantitative",
"scale": {
"domain": [1,15],[15,30]
}
},
"y": {"field": "value", "aggregate": "count", "type": "quantitative"},
"color": {"field": "key", "type": "nominal"}
}
}
I've tried understanding/plugging in various items from the vega lite documentation and working in the vega editor, but can't quite seem to get it right. If I add the "value" field to X axis, it just counts how many of each identifier has that value.
Edit: Update using Bin as suggested
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{"ATR": 1, "ETO": 10, "ITE": 20, "RTI": 15},
{"ATR": 3, "ETO": 4, "ITE": 8, "RTI": 4},
{"ATR": 8, "ETO": 23, "ITE": 1, "RTI": 6},
{"ATR": 7, "ETO": 9, "ITE": 4, "RTI": 11}
]
},
"transform": [
{"fold": ["ATR", "ETO", "ITE", "RTI"]} ,
{
"bin":{"binned": true, "steps": [15]},
"field": "value"
}
],
"mark": "bar",
"encoding": {
"x": {
"field": "value",
"type": "ordinal"
},
"y": {"aggregate": "count"},
"xOffset":{"field": "key"},
"color": {"field": "key", "type": "nominal"}
}
}
答案1
得分: 0
你是指这样吗?
如果是这样,使用标签表达式。
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{"ATR": 1, "ETO": 10, "ITE": 20, "RTI": 15},
{"ATR": 3, "ETO": 4, "ITE": 8, "RTI": 4},
{"ATR": 8, "ETO": 23, "ITE": 1, "RTI": 6},
{"ATR": 7, "ETO": 9, "ITE": 4, "RTI": 11}
]
},
"transform": [
{"fold": ["ATR", "ETO", "ITE", "RTI"]},
{"bin": {"binned": true, "steps": [15]}, "field": "value"}
],
"mark": "bar",
"encoding": {
"x": {"field": "value", "type": "ordinal", "axis": {
"labelExpr": "datum.label==15?'0-15':'15-30'",
"labelAngle":0
}},
"y": {"aggregate": "count"},
"xOffset": {"field": "key"},
"color": {"field": "key", "type": "nominal"}
}
}
英文:
Do you mean like this?
If so, use a label expression.
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{"ATR": 1, "ETO": 10, "ITE": 20, "RTI": 15},
{"ATR": 3, "ETO": 4, "ITE": 8, "RTI": 4},
{"ATR": 8, "ETO": 23, "ITE": 1, "RTI": 6},
{"ATR": 7, "ETO": 9, "ITE": 4, "RTI": 11}
]
},
"transform": [
{"fold": ["ATR", "ETO", "ITE", "RTI"]},
{"bin": {"binned": true, "steps": [15]}, "field": "value"}
],
"mark": "bar",
"encoding": {
"x": {"field": "value", "type": "ordinal", "axis": {
"labelExpr": "datum.label==15?'0-15':'15-30'",
"labelAngle":0
}},
"y": {"aggregate": "count"},
"xOffset": {"field": "key"},
"color": {"field": "key", "type": "nominal"}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论