给出一个堆叠的条形图,例如本示例:https://vega.github.io/editor/?#/examples/vega-lite/stacked_bar_weather
我想控制聚合中项目的顺序,例如,“雾”位于底部,“太阳”位于其下,等等。
原因是我有一种类型比其他类型大得多。我希望它显示在底部,然后控制域以“切断”该部分的大部分内容。
谢谢
答案 0 :(得分:1)
您可以通过order
编码来控制堆栈顺序:请参见https://vega.github.io/vega-lite/docs/stack.html#sorting-stack-order
不幸的是,这仅允许按字段值排序,而不是按此处想要的显式顺序排序。解决方法是使用calculate transform将您的显式顺序转换为字段(view in editor):
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"data": {"url": "data/seattle-weather.csv"},
"transform": [
{
"calculate": "indexof(['sun', 'fog', 'drizzle', 'rain', 'snow'], datum.weather)",
"as": "order"
}
],
"mark": "bar",
"encoding": {
"x": {
"timeUnit": "month",
"field": "date",
"type": "ordinal",
"axis": {"title": "Month of the year"}
},
"y": {"aggregate": "count", "type": "quantitative"},
"color": {
"field": "weather",
"type": "nominal",
"scale": {
"domain": ["sun", "fog", "drizzle", "rain", "snow"],
"range": ["#e7ba52", "#c7c7c7", "#aec7e8", "#1f77b4", "#9467bd"]
},
"legend": {"title": "Weather type"}
},
"order": {"field": "order", "type": "ordinal"}
}
}