But for some graphs where I only have 12th grade data the legend would show 8th and 10th grade as well. I just wanted to show 12th grade in the legend. Is there an easy way to do this?
So I tried using the filtered data as the domain and I left the range the same.
The problem here is that it makes 12th grade green instead of red. How would I go about keeping my same color scheme even when the domain changes from three grades to just one grade?
Thanks for getting back to me on this. Your snippet has been a good help. Could you expand on the domain a little bit more in the color: section? I have a single data file that I filter in differently depending on the figure weāre trying to show. How would I declare the domain?
Iāve written a minimum example of the type of problem Iām dealing with:
// Define the color scheme
colors = new Map([
["8th Grade", "green"],
["10th Grade", "blue"],
["12th Grade", "red"]
]);
// Plot a filtered version of the data
Plot.plot({
marks: [
Plot.barY(
data,
Plot.groupX(
{
y: "sum"
},
{
filter: d => d.grade != "10th Grade",
x: "x",
y: "y",
fill: "grade"
}
)
),
Plot.ruleY([0])
],
// Legend
color: {
domain,
range: Array.from(domain, (c) => colors.get(c)),
legend: true
}
})
Note that the filter option does not change the colorās domain; you will have to filter the data instead: Plot.dot(data.filter(ā¦)), {fill: āgradeā}) etc.
I see, so it seems that my issue centers around filtering as an option instead of using a full on data filter. I think this is plenty to go off for now. Thanks for taking the time to contribute.
Note that if you filter on the value (as in the pseudo example above), thereās no reason to create a fake chart and throw it away, since you could filter on the domain directly; my first suggestion would be more performant in that case: