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.

color=alt.Color(sort=[...]) only sorts legend and colors but not the color slice position in stacked bar graphs.

See original GitHub issue

First of all, amazing work! I really like this library!

Now with the issue: “Color slices do not respect the sort order.”

I am trying to change the order of slices of a stacked colored bar.

By default “color slice position IN the bars”, “color assignment” and “legend order” are all sorted in alphabetical ascending order.

# load an example dataset
from vega_datasets import data
cars = data.cars()

# plot the dataset, referencing dataframe column names
import altair as alt
alt.Chart(cars).mark_bar().encode(
  x=alt.X('Miles_per_Gallon', bin=True),
  y='count()',
  color=alt.Color('Origin:N'),
)

Imgur

When I try to sort with color=alt.Color(sort=[...])

# load an example dataset
from vega_datasets import data
cars = data.cars()

# plot the dataset, referencing dataframe column names
import altair as alt
alt.Chart(cars).mark_bar().encode(
  x=alt.X('Miles_per_Gallon', bin=True),
  y='count()',
  color=alt.Color('Origin:N',
                    sort=['Europe', 'USA', 'Japan'], # SORTING
                    ),
)

only “color assignment” and “legend order” respect the sort. Note how USA still comes last in the “color slice position IN the bars”:

Imgur

Is it possible to change how the colors in the bar are plotted without having to pre-append numbers and force a particular alphabetical order to the “color slice position IN the bars”?

EDIT (Based on comment below): Correct! By color slice position IN the bars I mean stack order

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jakevdpcommented, Jun 24, 2020

The order can only be assigned to a field. You can use a transform to specify arbitrary orders; something like this:

order= ['Europe', 'USA', 'Japan']

alt.Chart(cars).transform_calculate(
  order=f"-indexof({order}, datum.Origin)"
).mark_bar().encode(
  x=alt.X('Miles_per_Gallon', bin=True),
  y='count()',
  color=alt.Color('Origin:N', sort=order),
  order="order:Q"
)

visualization (7)

1reaction
jakevdpcommented, Jun 24, 2020

I don’t quite undertand what you mean by “color slice position”, but perhaps you want to adjust the stack order of the bars? If so, you can do this using the order channel. See https://altair-viz.github.io/gallery/stacked_bar_chart_sorted_segments.html for an example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sorting based on the alt.Color field in Altair - Stack Overflow
The chart is currently sorted according to the species column in alphabetical order, but I would like it sorted by the group so...
Read more >
Sorting a Legend Independently from the ... - Knowledge Base
Issue. The fields in the color legend of a worksheet cannot be sorted in a different order than the dimension fields reflected in...
Read more >
altair pie chart - La Calabrisella 2
It's easy to make 2D, 3D, or doughnut-style pie charts in Microsoft Excel—no design knowledge necessary! Here's how to do it.
Read more >
Sorting bar segments by value - TIBCO Product Documentation
Right-click the bar chart, and select Properties in the opened menu. The Properties pop-over is shown. Click Layout and sorting. The Layout and...
Read more >
Sorting in a stacked bar chart - Microsoft Power BI Community
Solved: Hi .. I have a stacked bar chart where I sort the x axis by date which works fine. However each bar...
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