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

ordering of legend labels #1059

Closed
Alcampopiano opened this issue Jul 30, 2018 · 12 comments
Closed

ordering of legend labels #1059

Alcampopiano opened this issue Jul 30, 2018 · 12 comments
Labels

Comments

@Alcampopiano
Copy link

Hi Jake,

Can we change around the order of the legend entries?

Say, put Google at the top in this example from the docs:

import altair as alt
from vega_datasets import data

stocks = data.stocks()

alt.Chart(stocks).mark_line().encode(
    x='date',
    y='price',
    color='symbol'
)

visualization 12

@jakevdp
Copy link
Collaborator

jakevdp commented Jul 30, 2018

You can specify the order using the sort property of the channel. Categories will appear in the specified order, and any remaining categories will be appended in arbitrary order. So if all you care is that GOOG is first, you can do this:

import altair as alt
from vega_datasets import data

stocks = data.stocks()

alt.Chart(stocks).mark_line().encode(
    x='date',
    y='price',
    color=alt.Color('symbol', sort=['GOOG'])
)

visualization 29

@Alcampopiano
Copy link
Author

Oh this is great. I should have guessed that. Thanks very much.

@Alcampopiano
Copy link
Author

Alcampopiano commented Aug 2, 2018

Hi Jake,

I'm trying to follow your instructions here for reording the legend labels. For some reason the "stock prices" example works, but when using my own data I get an error despite the same data structure and Altair code. What am I missing here? Note that the json works fine in the Vega editor for both examples.

this works

import altair as alt
from vega_datasets import data

stocks = data.stocks()

alt.Chart(stocks).mark_line().encode(
    x='date',
    y='price',
    color=alt.Color('symbol', sort=['GOOG', 'IBM'])
)

this doesn't

df

(I'm hiding some of the data here but you can see the structure)

alt.Chart(data_melt).mark_line().encode(
    x='year',
    y='percentage of students',
    color=alt.Color('percent categories', sort=['applied to university only', 'applied to college only'])
)

Javascript Error: Expression parse error: "datum.percent categories === 'applied to university only' ? 0 : datum.percent categories === 'applied to college only' ? 1 : 2". This usually means there's a typo in your chart specification. See the JavaScript console for the full traceback.

@Alcampopiano Alcampopiano reopened this Aug 2, 2018
@jakevdp
Copy link
Collaborator

jakevdp commented Aug 2, 2018

I can't reproduce this error. If I run the following it works without an issue:

import altair as alt
import pandas as pd

data = pd.DataFrame({'year': range(2010, 2016),
                     'percentage of students': range(10, 16),
                     'percent categories': 3 * ['applied to university only', 'applied to college only']})

alt.Chart(data).mark_line().encode(
    x='year',
    y='percentage of students',
    color=alt.Color('percent categories', sort=['applied to university only', 'applied to college only'])
)

@Alcampopiano
Copy link
Author

How odd. If I run the code you just sent I get the same error.

@jakevdp
Copy link
Collaborator

jakevdp commented Aug 2, 2018

You may need to update your frontend to get a newer version of vega/vega-lite.

@jakevdp
Copy link
Collaborator

jakevdp commented Aug 2, 2018

This error is not coming from Python, it is coming from Javascript, so it is the javascript library version that is relevant here. That library version is not controlled by Altair, but controlled by the extension your frontend (jupyterlab, jupyter notebook, colab, etc.) is using to render Altair's outputs.

@Alcampopiano
Copy link
Author

Okay thanks. I will upgrade JL and try again.

@Alcampopiano
Copy link
Author

I updated JL and your example works but only if no layering present. If I layer some dots, the legend goes back to its original ordering.

import altair as alt
import pandas as pd

data = pd.DataFrame({'year': range(2010, 2016),
                     'percentage of students': range(10, 16),
                     'percent categories': 3 * ['applied to university only', 'applied to college only']})


base=alt.Chart(data).encode(    
    x='year:O',
    y='percentage of students',
    color=alt.Color('percent categories', sort=['applied to university only', 'applied to college only'])
)

line=base.mark_line()
dot=base.mark_circle()
both=line+dot
both

visualization 13

@jakevdp
Copy link
Collaborator

jakevdp commented Aug 2, 2018

That is a known issue in Vega-Lite: see vega/vega-lite#2177 and #820

@Alcampopiano
Copy link
Author

Ah I see. Thanks for letting me know.

kindofluke pushed a commit to kindofluke/altair that referenced this issue Aug 6, 2018
… improvements to demonstrate column and legend sorting.

Documentation includes two new examples showing how encoded channels can be sorted based on their data type or with an explicit order.
@jakevdp
Copy link
Collaborator

jakevdp commented Jan 31, 2020

This should be fixed as of Altair 4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants