[英]Vega-lite: Change order of stacks in a stacked bar chart and ignore negative value
This is the chart I am trying to follow for my work这是我在工作中要遵循的图表
How could I change the order of the 'Measure' to keep 'Measure 1' at the top only by ignoring that it is a negative value?我怎么能改变'Measure'的顺序,让'Measure 1'保持在顶部,忽略它是一个负值? I still want to show the '-' sign in the text but I want the bar to go to the top.
我仍然想在文本中显示“-”符号,但我希望 go 的栏位于顶部。
For David: One of the bars are in such a way where the top blue bar can be negative but should always be on the top对于大卫:其中一个条形的顶部蓝色条可以为负数,但应始终位于顶部
Like this?像这样? If so, you need a sort.
如果是这样,你需要一个排序。
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{"Value": 0.321, "Date": "09/30/2021", "Measure": "Measure 4"},
{"Value": 0.031, "Date": "09/30/2021", "Measure": "Measure 3"},
{"Value": 0.123, "Date": "09/30/2021", "Measure": "Measure 2"},
{"Value": -0.475, "Date": "09/30/2021", "Measure": "Measure 1"}
]
},
"width": 500,
"height": 250,
"resolve": {"scale": {"color": "independent"}},
"layer": [
{
"mark": "bar",
"encoding": {
"y": {
"field": "Value",
"type": "quantitative",
"axis": {"format": ".1%"},
"sort": "descending"
},
"x": {"field": "Date", "type": "nominal", "axis": {"labelAngle": -45}},
"color": {"field": "Measure", "type": "nominal"}
}
},
{
"transform": [
{
"stack": "Value",
"groupby": ["Date"],
"as": ["lower", "upper"],
"sort": [{"field": "Measure", "order": "descending"}]
},
{"calculate": "(datum.lower + datum.upper) / 2", "as": "midpoint"}
],
"mark": {"type": "text"},
"encoding": {
"y": {"field": "midpoint", "type": "quantitative"},
"x": {"field": "Date", "type": "nominal"},
"color": {
"field": "Measure",
"type": "nominal",
"scale": {"range": ["white"]},
"legend": null
},
"text": {"aggregate": "sum", "field": "Value"}
}
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.