Sort Y axis by Sum (yield) of Waseca with descending order

Viewed 133

I'm attempting to sort Y axis based on the descending order of sum(yield) of Waseca site. How can I do that?

Thanks a lot.

https://altair-viz.github.io/gallery/horizontal_stacked_bar_chart.html

Source code:

import altair as alt
from vega_datasets import data

source = data.barley()

alt.Chart(source).mark_bar().encode(
    x='sum(yield)',
    y='variety',
    color='site'
)
1 Answers

You can use the sort parameter within the encoding you want to sort as per https://altair-viz.github.io/gallery/bar_chart_sorted.html

import altair as alt
from vega_datasets import data

source = data.barley()

alt.Chart(source).mark_bar().encode(
    x='sum(yield)',
    y=alt.Y('variety', sort='-x'),
    color='site'
)

enter image description here

Related