question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

custom sorting categorical axis with layers - working in VL3 Editor

See original GitHub issue

Hi Jake,

Not sure if you’ve seen the recent slack discussion on this but I wanted to let you know that the following works in the VL3 Editor but not in Altair. I’m referring to the sorting of the Y axis labels.

df=pd.DataFrame({'Org': ['school','school','school','school', 'board', 'board', 'board', 'board'],
                 'variable': ['rose', 'dropped', 'maintained', 'failed','rose', 'dropped', 'maintained', 'failed'],
                'percent': [25,25,25,25,25,25,25,25], 
                 'position': [12.5, 37.5,62.5, 87.5,12.5, 37.5,62.5, 87.5]})
    
bars=alt.Chart().mark_bar().encode(
    x=alt.X('sum(percent)', title='Percentage of Students'),
    y=alt.Y('Org', sort=['school'], title=None),
    color=alt.Color('variable', title=None),
    order='junk:Q'
)

text=alt.Chart().mark_text(align='center', baseline='middle').encode(
    y=alt.Y('Org', sort=['school'], title=None),
    x='position',
    text='percent')

alt.layer(bars, text, data=df).transform_calculate(
    junk="if(datum.variable === 'maintained', 0, if(datum.variable === 'rose', 1, if(datum.variable === 'dropped', 2, 2)))")

visualization 28

The VL folks showed me how to get this working and I thought perhaps it would translate to Altair 2.2. I assume it is simply a VL2 versus VL3 difference but wanted to bring it to your attention in case it is helpful for you to know.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
jtbakercommented, Jan 28, 2019

As of v2.3.0 with VL 3, axes may now be sorted by passing a custom sort array to the channel as such:

chart = alt.Chart(df, height=210).mark_rect().encode(
    x=alt.X("hour:N", title='Hour'), 
    y=alt.Y("dayofweek:O", sort=["Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", "Sat"],
            title="Day of Week"),
    color=alt.Color(
        "cnt:Q", 
        scale=alt.Scale( range=['lightyellow','red']), 
        legend=alt.Legend(title='Count')
    ),
    tooltip=[
        alt.Tooltip("cnt:Q", title="Count")
    ]
)

image

1reaction
afonitcommented, Sep 18, 2018

In the ‘bars’ chart, for the y put this:

y=alt.Y('Org', sort=alt.EncodingSortField(field='Org', op='sum', order='descending'), title=None),

That now sorts ‘board’ before ‘school’ on the y-axis.

Read more comments on GitHub >

github_iconTop Results From Across the Web

8.4 Changing the Order of Items on a Categorical Axis
For a categorical (or discrete) axis – one with a factor mapped to it – the order of items can be changed by...
Read more >
Altair Stacked Area With Custom Sorting
For line and trail marks, this property determines the sort order ... sorting categorical axis with layers - working in VL3 Editor hot...
Read more >
Untitled
#tans Zedd e hayley letra, Dc brushless motor working principle. ... X edit digitech windows 7, Nvidia shield kodi 16, Neem tree buy?...
Read more >
Untitled
Kaan work mp3, Fan milk lagos nigeria, Nicholas goodden street photography, ... Turismo culturale in grecia, Matschig englisch, Custom 338 bullets!
Read more >
GPU Applications Catalog
Custom models and data streams. Multi-GPU. Single Node ... work of business analysis, empowering ... Provides a list of matches sorted by confidence....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found