I want each subplot to sort the labels based on the value defining the size of the bar.
See example image:
data = {'label': ['A','A','B','B'], 'variable': ['x', 'y', 'x', 'y'], 'value':[2,4,3,1]}
df = pd.DataFrame.from_dict(data)
selector = alt.selection_single(empty='all', fields=['label'])
bar = alt.Chart(df,title='My Plot').mark_bar().encode(
alt.Y('label', sort=alt.EncodingSortField(field="value", op="mean", order='ascending'), axis=alt.Axis(title='Label')),
alt.X('value:Q', axis=alt.Axis(format='%', title='Value')),
alt.Row('variable', title='Variable'),
color=alt.condition(selector, alt.value('orange'), alt.value('lightgray')),
tooltip=[alt.Tooltip('label', title='Label'),
alt.Tooltip('value:Q', format='.2%', title='Value'),]
).add_selection(selector)
chart = (bar).properties(width=700, height=300)
display(chart)
In the example, the labels (A, B) are now sorted based on the mean of all values for those labels. I want the order to be B-A for label X and A-B for label Y (so descending based on the value of the label showed in the row of the Altair plot).
resolve
to make facet use independent scale. (See my answer.) :) – Belier