observablehq / plot

A concise API for exploratory data visualization implementing a layered grammar of graphics
https://observablehq.com/plot/
ISC License
4.25k stars 173 forks source link

Feature request: detect ordinal domain in color when ordering by same channel #2000

Closed jimjam-slam closed 6 months ago

jimjam-slam commented 6 months ago

(Possibly a duplicate of #1121, but I'm not 100% sure!)

When I make a barchart in which the stacking channel (fill or color) is ordinal, it can be tricky to get the legend and the stacking order to line up. Although there are many cases where simple alphanumerical order or frequency is the right call, often (especially with survey responses) it's easier to just list out the levels.

I had assumed that if I provided an array of values to color.domain (which orders the legend glyphs), the stacking order would reflect this.

I imagine there are cases where you do still want to stack by frequency even when your legend is ordered (and sort can ), but I think if you provide the same channel to both fill/color and sort, users would benefit from Plot using any levels provided by color.domain. The workaround is to provide a sorting function that re-uses those levels, but I think this is a common enough scenario that it would be helpful for Plot to default to this behaviour.

Here's an example notebook:

https://observablehq.com/@jimjamslam/stack-a-barchart-on-ordinal-data

Thank you!

Fil commented 6 months ago

Congratulations on hitting #2000 :)

The solution is to use order: freqLevels. ("If an array of z values are specified, they should enumerate the z values for all series in the desired order")