Skip to content
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

Sorting of alt.Columns should be similar to alt.Colors? #940

Closed
brainstorm opened this issue Jun 12, 2018 · 11 comments
Closed

Sorting of alt.Columns should be similar to alt.Colors? #940

brainstorm opened this issue Jun 12, 2018 · 11 comments
Labels
enhancement vega: vega-lite Requires upstream action in `vega-lite`

Comments

@brainstorm
Copy link

brainstorm commented Jun 12, 2018

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:

skarmavbild 2018-06-12 kl 10 49 10

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 #245, #899, #898 and #397, I just wanted this usecase to be noted ;)

@brainstorm
Copy link
Author

Apparently this issue is closely related to vega-lite vega/vega-lite#3854, AFAICT.

@jakevdp
Copy link
Collaborator

jakevdp commented Jun 12, 2018

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'])
)

visualization 9

@jakevdp jakevdp closed this as completed Jun 12, 2018
@brainstorm
Copy link
Author

brainstorm commented Jun 12, 2018

@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, ...).

@jakevdp
Copy link
Collaborator

jakevdp commented Jun 12, 2018

Ah, my mistake. Sorry.

@jimioke
Copy link

jimioke commented Nov 26, 2018

@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.

@jakevdp
Copy link
Collaborator

jakevdp commented Nov 27, 2018

This was fixed in the most recent Vega-Lite release, I believe. Update your renderer, and things should work properly.

@jakevdp jakevdp closed this as completed Nov 27, 2018
@jimmywan
Copy link
Contributor

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.

@jakevdp
Copy link
Collaborator

jakevdp commented Nov 28, 2018

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.

@andreipoe
Copy link

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:

image

Note that the first two column are not the ones specified.

@jakevdp
Copy link
Collaborator

jakevdp commented Oct 23, 2019

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.

@jan-matthis
Copy link

jan-matthis commented Oct 1, 2020

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement vega: vega-lite Requires upstream action in `vega-lite`
Projects
None yet
Development

No branches or pull requests

6 participants