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.

sort channel not working when I add text on top of my bars

See original GitHub issue

This sorts like I want.

import altair as alt
from vega_datasets import data

barley = data.barley()
alt.Chart(cars.head(10)).mark_bar().encode(
    x='Miles_per_Gallon:Q',
    y=alt.Y('Name:N', sort=alt.SortField(field="Miles_per_Gallon", op="sum", order="descending"))
)

visualization 24

This does not.

bars = alt.Chart(cars.head(10)).mark_bar().encode(
    x='Miles_per_Gallon:Q',
    y=alt.Y('Name:N', sort=alt.SortField(field="Miles_per_Gallon", op="sum", order="descending"))
)

text = bars.mark_text(
    align='left',
    baseline='middle',
    dx=3,
).encode(text='Miles_per_Gallon:Q')

bars + text

visualization 25

Why?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
jakevdpcommented, Jul 21, 2020

I’m not sure why this is happening; I think it’s a vega-lite bug. Here’s a simplified vega-lite spec showing the issue (Open the Chart in the Vega Editor):

{
  "data": {"url": "https://vega.github.io/vega-datasets/data/barley.json"},
  "transform": [
    {
      "joinaggregate": [{"op": "sum", "field": "yield", "as": "order"}],
      "groupby": ["variety"]
    }
  ],
  "encoding": {
    "x": {
      "field": "yield",
      "aggregate": "sum",
      "type": "quantitative",
      "stack": "zero"
    },
    "y": {
      "field": "variety",
      "type": "nominal",
      "sort": {"field": "order", "order": "descending"}
    },
    "order": {"type": "nominal", "field": "site"}
  },
  "layer": [
    {
      "mark": "bar",
      "encoding": {"color": {"type": "nominal", "field": "site"}}
    },
    {
      "transform": [{"filter": "true"}],
      "mark": {"type": "text", "color": "white", "dx": -15, "dy": 3},
      "encoding": {
        "text": {
          "type": "quantitative",
          "aggregate": "sum",
          "field": "yield",
          "format": ".1f"
        }
      }
    }
  ],
  "width": 600
}

If you delete the filter line, the sort works as expected.

I think it’s probably some issue with unioned domains, though strangely there is no warning in the Vega editor.

As a workaround, you can set resolve_scale(y='independent'), which preserves the sort:

import altair as alt
from vega_datasets import data

source=data.barley.url

bars = alt.Chart(source).transform_joinaggregate(
    order='sum(yield)', groupby=['variety']  
).mark_bar().encode(
    x=alt.X('sum(yield):Q', stack='zero'),
    y=alt.Y('variety:N', sort=alt.SortField('order', order='descending')),
    color=alt.Color('site:N'),
    order = 'site:N'
)

text = bars.mark_text(
    dx=-15, dy=3,
).transform_filter(
    {'field': 'site', 'oneOf': ['Crookston', 'Duluth']}
).encode(
    color=alt.value('white'),
    text=alt.Text('sum(yield):Q', format='.1f'),
    y=alt.Y('variety:N', sort=alt.SortField('order', order='descending'), axis=None),
)

(bars + text).resolve_scale(y='independent')

visualization (17)

1reaction
jakevdpcommented, Dec 18, 2019

The default aggregation is set within Vega-Lite. It might be worth a feature request there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Order bar chart in Altair? - python - Stack Overflow
The sort keyword needs to be applied to the axis being sorted - here it's ... Simple alternative, specify the channel you'd like...
Read more >
Adjust your sidebar preferences - Slack
Sort your conversations · From your desktop, click your profile picture in the top right. · Select Preferences from the menu. · Click...
Read more >
How to Sort Your Bar Charts - Depict Data Studio
Option B: Re-Sort the Bars within the Chart · Click on the category labels on the left. · Hold your mouse over the...
Read more >
Sort and arrange items in the Finder on Mac - Apple Support
The top of a Finder window showing View option buttons for a folder. Your settings for sorting and arranging items in a folder...
Read more >
Order Bars of ggplot2 Barchart in R (4 Examples)
Thank you for the comment. I think the reason for this is the last line of your code (i.e. '+ guides(fill = FALSE)')....
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