While trying to apply a custom ordering to the columns chart spec here:
logplot = alt.Chart(dataset_curated).mark_bar().encode(
alt.X('technology:N', scale=alt.Scale(rangeStep=15), axis=alt.Axis(title='')),
y = 'log:Q',
color = alt.Color('sample:N'),
column = 'chromosome:N'
#column = alt.Column('chromosome:N', sort=alt.SortField(order=chr_array))
)
The column strings seem to be ordered lexicographically, not ideal for i.e, chromosomes:

So my pandas Categorical ordering, which works for raw dataframe ordering (chromosomes ordered by array's positions):
chr_array = ['chr'+str(x) for x in range(1,23)]
chr_array.append('chrX')
chr_array.append('chrY')
chr_array.append('chrM')
dataset_curated["chromosome"] = pd.Categorical(dataset_curated["chromosome"], chr_array)
dataset_curated = dataset_curated.sort_values("chromosome")
Is ignored by Altair's column ordering :_/ Also, according to the API reference for alt.Column, that only supports ascending and descending sort= arguments, unlike i.e alt.Colors which supports a manually ordered List(String).
I'm currently trying to figure out some workaround while reading through https://github.com/altair-viz/altair/issues/245, https://github.com/altair-viz/altair/pull/899, https://github.com/altair-viz/altair/issues/898 and https://github.com/altair-viz/altair/issues/397, I just wanted this usecase to be noted ;)
Apparently this issue is closely related to vega-lite https://github.com/vega/vega-lite/pull/3854, AFAICT.
Altair data is encoded as JSON, which does not have a categorical type, so all pandas categories will be converted to simple strings.
If you'd like to specify the explicit order in which a nominal category will appear, you can use the sort argument. For example:
import altair as alt
import pandas as pd
data = pd.DataFrame({'a': list('ABC'),
'b': [28, 55, 43]})
alt.Chart(data).mark_bar().encode(
x='b:Q',
y=alt.Y('a:O', sort=['B', 'A', 'C'])
)

@jakevdp, the type of sorting I am trying to accomplish is in the columns, not on the Y axis, pretty much like ordering age in here:
https://altair-viz.github.io/gallery/grouped_bar_chart.html
Changing a:N to a:O does not help, the order of the chromosomes in the column= argument of the chart shows up wrong (lexicographical ordering) when the pandas Dataframe is properly sorted (manually ordered as in: chr1, chr2, chr3, ... instead of chr1, chr10, chr11, ...).
Ah, my mistake. Sorry.
@brainstorm Not sure if you still have this issue. I was also trying to figure it out for a while but @jakevdp's approach actually works in the column field as well. Just include sort=[...] and list the column names in your desired order.
This was fixed in the most recent Vega-Lite release, I believe. Update your renderer, and things should work properly.
This was fixed in the most recent Vega-Lite release, I believe. Update your renderer, and things should work properly.
Exactly how do I go about doing this?
Upgrade ipyvega? Is that just for notebook?
Do I need to do the following for JupyterLab?
According to this, I can install my own jupyterlab vega3-extension as follows:
jupyter labextension install @jupyterlab/vega3-extension
But according to this README, this extension is deprecated?
I still go back/forth between vanilla notebooks and JupyterLab since a few things occasionally seem to be more flaky in one than the other, and vice-versa.
For jupyter notebook, you need to update ipyvega.
For jupyterlab, vega-lite is bundled with jupyterlab itself. If you update to the most recent version of jupyterlab you'll get the most recent version of the vega-lite renderer.
If you go back and forth between both, you'll need to update both.
Hello,
I'm having the same issue with ordering the column in a grouped bar chart. It looks like ascending or descending sort orders are respected, but not custom lists. For example, if I open the sample grouped bar chart in the Vega editor and I add custom sorting to the columns, the column order produced is different than the default, but it doesn't follow the sorting array.
To illustrate, using:
"encoding": {
"color": {"type": "nominal", "field": "year"},
"column": {"type": "nominal", "field": "site",
"sort": ["Waseca", "Duluth"]},
"x": {"type": "ordinal", "field": "year"},
"y": {"type": "quantitative", "aggregate": "sum", "field": "yield"}
},
... where the sort parameter is the only change I made, produces:

Note that the first two column are not the ones specified.
Confirmed that sorting doesn't work in this case. It appears to be a bug in Vega-Lite: I'd suggest filing an issue there.
I ran into the same issue as @andreipoe. To save the next person from searching -- there are two issues in Vega-Lite that seem related:
Most helpful comment
For jupyter notebook, you need to update ipyvega.
For jupyterlab, vega-lite is bundled with jupyterlab itself. If you update to the most recent version of jupyterlab you'll get the most recent version of the vega-lite renderer.
If you go back and forth between both, you'll need to update both.