-
Notifications
You must be signed in to change notification settings - Fork 774
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
Faceted charts with an aggregation don't support a custom sort order of the facets/columns #3386
Comments
As Joel suggested in the comments on the linked Q, using a different channel ie xOffset orders as spcified when import altair as alt
from vega_datasets import data
source = data.barley()
alt.Chart(source).mark_bar().encode(
alt.X('site:N', sort=["Waseca", "Morris", "University Farm",
"Grand Rapids", "Crookston", "Duluth"]),
y='sum(yield):Q',
color='year:N',
xOffset='year:O',
) |
Thanks for following up here @bertiebaggio ! I can see that things work as expected in the issue you linked. After some exploring, it seems to be an issue with aggregating and sorting facets at the same time. I had no idea this was an existing bug, but it has been reported in VL already here vega/vega-lite#5366; you might be able to ask for workaround there and thumbs up that issue. If you have the time you can also help to troubleshoot it. Do this by opening the Chart in the VegaEditor and then clicking the "Vega" spec tab at the bottom. Compare the Vega spec for the working non-aggregated faceted chart and with of the non-working aggregated facet to figure out what needs to be change in how Vega-Lite translates the aggregate spec to Vega. This can take some time, but sometimes there are small differences that are easy to spot and reporting them in the issue helps encouraging a fix. I'll close this issue since there is nothing we can do from the altair side of things to fix it, but a not so elegant workaround could involve sorting the dataframe and then breaking the altair default alphabetical sort: import altair as alt
from vega_datasets import data
source = data.barley()
source['site'] = pd.Categorical(source['site'], ["Waseca", "Morris", "University Farm", "Grand Rapids", "Crookston", "Duluth"])
source = source.sort_values('site')
alt.Chart(source).mark_bar().encode(
x='year:O',
y='sum(yield):Q',
color='year:N',
# Passing `None` to `sort` did not work for me, it kept it as alphabetical.
# A list seems to work in the sense that it breaks the sort order and falls back on the order in the df
column=alt.Column('site:N').sort(['sdaf'])
) |
Yes, it makes sense to close it here since it does seem to be squarely a VL issue. Thanks for taking the time to dig into this further; good catch on the aggregation, that sailed past me! Good tip on manually reordering the data as another workaround. I'll see about troubleshooting / exploring the Vega/VL translation boundary when I get a chance :) |
This issue arose as a result of a question I posted on Stack Overflow. @joelostblom directed me here if I could repro using the example datasets, which I can. That said, I am still ready to believe this is a PEBKAC issue if someone tells me I am using
altair.Column
wrong!More context, a previous issue here:
#1826
However, passing a lit of values to the
sort
parameter results in a column order that matches the data order:Results in:
I bet this year's barley harvest the next row in that dataframe says "Duluth" !
Is this a Vega Lite Issue?
This does look like potentially a Vega-Lite issue, since the sort order is reproduced in the Vega Lite JS object / JSON but also not honoured in the chart:
However, I would be grateful if someone more experienced than me could confirm that before I raise the issue there, which I am happy to do :)
Please follow these steps to make it more efficient to solve your issue:
vega_datasets
.The text was updated successfully, but these errors were encountered: