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

Custom order of points drawing a line #1096

Closed
jmansilla opened this issue Aug 15, 2018 · 6 comments
Closed

Custom order of points drawing a line #1096

jmansilla opened this issue Aug 15, 2018 · 6 comments

Comments

@jmansilla
Copy link

Hi there folks.
@jakevdp I'm happy I found your comment here #689

ordering of points in a line as well. I used that in an example here: 2098f8d

Right now I'm having trouble drawing a line exactly because of the ordering issue.
My problem is that I have to specify the ordering by providing the values, more or less like this:

alt.Chart(data).mark_line().encode(
        x='x:Q',
        y='y:Q',
        order=alt.Order('topic:N', sort=['Some Topic', 'Amazing Topic', 'Some Other', 'Minor Topic'])
    )

but I'm getting:

altair.vegalite.v2.schema.channels.Order->sort, validating 'anyOf'

['Some Topic', 'Amazing Topic', 'Some Other', 'Minor Topic'] is not valid under any of the given schemas

Is there any workaround for this?

@jakevdp
Copy link
Collaborator

jakevdp commented Aug 15, 2018

The order field can only use ascending or descending... I'm not certain it's currently possible to do what you want in Vega-Lite.

@kanitw
Copy link
Member

kanitw commented Aug 15, 2018

You can derive a new field and sort by that field e.g., something like:

{
  calculate: "{'Some Topic': 0, 'Amazing Topic': 1, ...}[datum.topic]"
  as: "topic_order"
}

@kanitw
Copy link
Member

kanitw commented Aug 15, 2018

If you think this custom order should be supported, please file an issue in Vega-Lite repo.

@jmansilla
Copy link
Author

Thanks folks.

Solved.

@ihightower
Copy link

can someone explain how this is solved clearly with example code. I also want to sort the stacked bars in a custom order. Lower stack say is 'Leave', Middle stack is 'Roster', Upper stack is 'OT' for Work Types. just ascending and descending for sort order doesn't cut it.

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 1, 2020

Currently you can control the stack order using an order encoding, and the legend order using the sort:

import altair as alt
import pandas as pd

df = pd.DataFrame({
    'label': ['Leave', 'OT', 'Roster'],
    'hours': [5, 6, 7]
})

alt.Chart(df).transform_calculate(
    order="{'Leave': 0, 'Roster': 1, 'OT': 2}[datum.label]"
).mark_bar().encode(
    x='hours',
    color=alt.Color('label', sort=['Leave', 'Roster', 'OT']),
    order='order:Q'
)

visualization - 2020-03-01T063033 916

When vega/vega-lite#1734 is addressed, this will be easier.

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

No branches or pull requests

4 participants