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.

Any way to sort two layers together?

See original GitHub issue

I have a sorted mark_bar() and would like to add a layer of mark_tick or top of this, while preserving the sorted order.

This sorting works:

from vega_datasets import data
df = data.movies()
df['WW_Margin'] = (df['Worldwide_Gross'] - df['Production_Budget']) / 1e6
  
lower_box = 'q1(WW_Margin):Q'
upper_box = 'q3(WW_Margin):Q'

bar_plot = alt.Chart(df).mark_bar(size=5.0).encode(
    alt.X('Major_Genre:O',
#          sort=alt.SortField(field='WW_Margin', op='median', 
#                             order='descending')
         ),
    y=lower_box,
    y2=upper_box,
)

median_tick = bar_plot.mark_tick(
    color='white',
    size=5.0
).encode(
    y='median(WW_Margin):Q',
)

bar_plot

image

But when I add a mark_tick layer, the sorting or the bars is gone, and it reverts to alphabetical sorting.

from vega_datasets import data
df = data.movies()
df['WW_Margin'] = (df['Worldwide_Gross'] - df['Production_Budget']) / 1e6
  
lower_box = 'q1(WW_Margin):Q'
upper_box = 'q3(WW_Margin):Q'

bar_plot = alt.Chart(df).mark_bar(size=5.0).encode(
    alt.X('Major_Genre:O',
          sort=alt.SortField(field='WW_Margin', op='median', 
                             order='descending')
         ),
    y=lower_box,
    y2=upper_box,
)

median_tick = bar_plot.mark_tick(
    color='white',
    size=5.0
).encode(
    y='median(WW_Margin):Q',
)

bar_plot + median_tick #note below that sorting is gone

image

This is quite possibly related to #698 which in turn refers to a Vega-lite issue(vega/vega-lite#2177). Will this get resolved when a newer version of Vega-lite is incorporated into Altair? Thanks much.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jakevdpcommented, Oct 10, 2019

The order of the axis and the order in which the points are connected are distinct concepts. If you want to change the order in which the points are connected, you can use the order channel. Details in the docs here: https://altair-viz.github.io/user_guide/encoding.html#ordering-marks

0reactions
jakevdpcommented, Oct 10, 2019

Also, ordering of axis for layered charts based on different datasets is still not supported (open the javascript console and you’ll see the relevant warnings output by Vega-Lite). You’ll need to source both charts from the same dataset if you want the orders to be propagated to the layered chart.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to do Multiple Level Data Sorting in Excel
This technique works the same way with a minor difference – you sort the second level first and then move to the first...
Read more >
How to Do Multi level sorting in Excel (by two Columns)
In this video, I'll show you how to do a multi- level sorting in Excel. This means that you sort data by 2...
Read more >
How to sort data by multiple columns in Excel
Click Add Level, choose Region from the Then By dropdown (Figure C), and click OK to execute the sort.
Read more >
Sort data in a table
Custom Sort - sorts data in multiple columns by applying different sort criteria. Here's how to do a custom sort: Select Custom Sort....
Read more >
How to Perform Multi-Level Data Sorting in Excel?
Step 4: In the Sort box one row of Level is inbuilt by Excel so, click two times on Add Level to add...
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