-
Notifications
You must be signed in to change notification settings - Fork 617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow specifying specific order of stack in stacked charts #1734
Comments
WorkaroundA workaround for now would be to use |
Using the workaround at vega/vega-lite#1734 (comment)
Trying to do this and still having trouble, stack transform isn't working for me. Is there currently any way of achieving this? |
Have you tried the workaround above? |
Please include a specification (or link to editor) so one can help you debug |
That is a link to the editor... |
Oh i just realize your image has a link. Thanks! (Normally people put link on a text so it’s clearer as the text will be blue) |
Took me a long time to figure out that there only exist a workaround for sorting area order. Also sorting by encoded channel, where the channel eg. Such as:
editor-spec (it starts with an |
I found another way of doing this: using {
"data": {
"url": "https://raw.githubusercontent.com/dhimmel/biorxiv-licenses/4ad9659e4fc823f4c491e13be4505248df5c1ab6/figure/license-vs-time/vega-lite-data.json"
},
"encoding": {
"color": {
"field": "license",
"type": "nominal",
"sort": [
"CC BY",
"CC BY-ND",
"CC BY-NC",
"CC BY-NC-ND",
"None"
]
},
"x": {
"axis": {
"axisWidth": 0.0,
"format": "%Y",
"labelAngle": 0.0,
"tickSize": 0.0
},
"field": "date",
"scale": {
"nice": "month"
},
"timeUnit": "yearmonth",
"title": "",
"type": "temporal"
},
"order": {
"field": "color_license_sort_index",
"type": "quantitative"
},
"y": {
"aggregate": "count",
"axis": null,
"type": "quantitative",
"stack": "normalize"
}
},
"mark": "area"
} |
Since the order of the {
"$schema": "https://vega.github.io/schema/vega-lite/v4.13.1.json",
"description": "Stack bar chart.",
"data": {
"values": [
{"a": "A", "b": 28, "c": "X"},
{"a": "A", "b": 55, "c": "Y"},
{"a": "B", "b": 55, "c": "Y"},
{"a": "B", "b": 91, "c": "Z"}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "a", "type": "ordinal"},
"y": {"field": "b", "type": "quantitative"},
"color": {
"field": "c",
"type": "nominal",
"scale": {
"domain": ["X", "Y", "Z"],
"range": ["darkgray", "orange", "lightgray"]
}
}
}
} Since the orange bars represent the same value, I decide that I'd like the orange bars to line up at the bottom. Re-ordering the domain & range will make the orange item appear at the bottom of the legend: {
"$schema": "https://vega.github.io/schema/vega-lite/v4.13.1.json",
"description": "Stack bar chart.",
"data": {
"values": [
{"a": "A", "b": 28, "c": "X"},
{"a": "A", "b": 55, "c": "Y"},
{"a": "B", "b": 55, "c": "Y"},
{"a": "B", "b": 91, "c": "Z"}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "a", "type": "ordinal"},
"y": {"field": "b", "type": "quantitative"},
"color": {
"field": "c",
"type": "nominal",
"scale": {
"domain": ["X", "Z", "Y"],
"range": ["darkgray", "lightgray", "orange"]
}
}
}
} It might be intuitive that this scale ordering also affect the order of where the orange bars appear in the stack as well, so that orange being bottom-most item in the legend also results in orange being the bottom-most mark in the stacked bars. |
I agree with @marcprux , it would be intuitive if changing the legend order also changed the stacking order, maybe by automatically doing the calculate transform linked above? Ideally with an optional argument in case the color legend order sometimes needs to be changed independently of the stacking order. |
Another good reason for automatically ordering according the a custom color sort, is that this is the current behavior if the color sort is set to |
It took me embarrassingly long for me to realize that |
We currently do not have a way to specify manual order of stacking group (e.g.,
color
ordetail
channel) in stacked charts.Note: As seen in #1732 (comment), users might expect that color's scale domain also affect how stack is ordered.
For example, in the following spec.
The scale domain only determines how the domain values are mapped to first 5 color in the default color palette, but does not affect order of the stacking.
TODOs:
design how we support manual order for stacking
sortby
ofstack
transform.decide if we want to use
color
/size
/detail
'ssort
by default.cc: @dhimmel
The text was updated successfully, but these errors were encountered: