color=alt.Color(sort=[...]) only sorts legend and colors but not the color slice position in stacked bar graphs.
See original GitHub issueFirst of all, amazing work! I really like this library!
Now with the issue: “Color slices do not respect the sort order.”
I am trying to change the order of slices of a stacked colored bar.
By default “color slice position IN the bars”, “color assignment” and “legend order” are all sorted in alphabetical ascending order.
# load an example dataset
from vega_datasets import data
cars = data.cars()
# plot the dataset, referencing dataframe column names
import altair as alt
alt.Chart(cars).mark_bar().encode(
x=alt.X('Miles_per_Gallon', bin=True),
y='count()',
color=alt.Color('Origin:N'),
)
When I try to sort with color=alt.Color(sort=[...])
…
# load an example dataset
from vega_datasets import data
cars = data.cars()
# plot the dataset, referencing dataframe column names
import altair as alt
alt.Chart(cars).mark_bar().encode(
x=alt.X('Miles_per_Gallon', bin=True),
y='count()',
color=alt.Color('Origin:N',
sort=['Europe', 'USA', 'Japan'], # SORTING
),
)
only “color assignment” and “legend order” respect the sort. Note how USA still comes last in the “color slice position IN the bars”:
Is it possible to change how the colors in the bar are plotted without having to pre-append numbers and force a particular alphabetical order to the “color slice position IN the bars”?
EDIT (Based on comment below):
Correct! By color slice position IN the bars
I mean stack order
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
The
order
can only be assigned to a field. You can use a transform to specify arbitrary orders; something like this:I don’t quite undertand what you mean by “color slice position”, but perhaps you want to adjust the stack order of the bars? If so, you can do this using the
order
channel. See https://altair-viz.github.io/gallery/stacked_bar_chart_sorted_segments.html for an example.