Skip to content

Legend ordering #1953

Answered by mbostock
Hvass-Labs asked this question in Q&A
Dec 19, 2023 · 2 comments · 5 replies
Discussion options

You must be logged in to vote

Per Scales:

For an ordinal scale, the domain defaults to the sorted union (all distinct values in natural order) of associated values; see the sort mark option to change the order.

The color scale here is ordinal (assuming that the Name column is full of string names), so that’s why you’re getting alphabetical order for the legend. If you want a custom order, you can either use the sort option on the lineY mark, or you can set the domain option on the color scale definition. E.g.,

Plot.plot({
  color: {
    legend: true,
    domain: ["small", "medium", "large"]
  },
  marks: [
    Plot.lineY(data, {x: "Date", y: "Value", stroke: "Name"}),
  ]
});

As @Fil said, if you want help, it’s gen…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@Hvass-Labs
Comment options

@mbostock
Comment options

@Hvass-Labs
Comment options

@mbostock
Comment options

@Hvass-Labs
Comment options

Answer selected by Hvass-Labs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants