Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditions in color encodings are not reflected in legend #9497

Open
1 task done
rlm1023 opened this issue Dec 12, 2024 · 10 comments
Open
1 task done

Conditions in color encodings are not reflected in legend #9497

rlm1023 opened this issue Dec 12, 2024 · 10 comments
Labels

Comments

@rlm1023
Copy link

rlm1023 commented Dec 12, 2024

Bug Description

If you have a color encoding with a conditional re-coloring on it for particular values, those re-colorings are not reflected in the legend.

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {"url": "data/cars.json"},
  "mark": "point",
  "encoding": {
    "x": {"field": "Horsepower", "type": "quantitative"},
    "y": {"field": "Miles_per_Gallon", "type": "quantitative"},
    "color": {
      "field": "Origin",
      "type": "nominal",
      "condition": {"test": "datum['Origin'] === 'USA'", "value": "red"},
      "scale": {"scheme": "category10"}
    }
  }
}

Produces:
Image

Repro

Checklist

  • I checked for duplicate issues.
@hydrosquall
Copy link
Member

hydrosquall commented Jan 20, 2025

I see how this seems like a bug at first, but it may be a feature request as this isn't a regression, it was just not built before.

Conditions were added independently of legends, and can be expressed in many forms. Since this form is using an expression test rather than a field predicate , Vega-lite is not readily aware that "origin" is being used in the test and that one of dots can be recolored.

I would suggest to think of the current legend as the color to use as long as no conditions are met. We could implement legends for conditional rules as a future enhancement, but it's currently not doable.

If you wish to change the color for a specific country in a graph and have that reflected in the legend, you can do so with a custom (scale.range) instead of using a condition. Would that work for your case?

@rlm1023
Copy link
Author

rlm1023 commented Jan 21, 2025

Yeah, I figure it might not be exactly a bug.

I've gotten around it so far by fully specifying the scale's domain/range, rather than using a condition. But that requires me to consume the entire dataset to build out those arrays. If there's a better way to do that, I'd be very curious how (I'm not familiar with scale.scheme.range, so if you have an example, I'd be thankful!).

@rlm1023
Copy link
Author

rlm1023 commented Jan 21, 2025

I should also add that I've used (I think) the field predicate version of the condition, and seen the same issue.

Repro

@hydrosquall
Copy link
Member

It sounds like you might already have tried this if you've already set a custom range, but just in case here's a custom range example

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {"url": "data/cars.json"},
  "mark": "point",
  "encoding": {
    "x": {"field": "Horsepower", "type": "quantitative"},
    "y": {"field": "Miles_per_Gallon", "type": "quantitative"},
    "color": {"field": "Origin", "type": "nominal", "scale": {
      "range": [
        "orange",
        "blue",
        "lightgray"
      ]
    }}
  }
}

RE: field predicate: we don't use the conditions in any form for the legends, but thanks for linking that too. I made the point about the condition form mainly to say that if we were to implement it, it may be easier to label with the predicate form than the "arbitrary expression" form.

@rlm1023
Copy link
Author

rlm1023 commented Jan 22, 2025

Yeah in that case it doesn't pin the color to specific values right? We're essentially using conditions as "these values should map to these colors, but the rest can come from the scheme".

@hydrosquall
Copy link
Member

hydrosquall commented Jan 22, 2025 via email

@rlm1023
Copy link
Author

rlm1023 commented Jan 22, 2025

Yeah, that's actually what I wound up doing to fix legends. So we now define explicit domains / ranges. The problem with that is having to iterate large data sets to generate those arrays, and vega-lite seems to struggle when they get in to the thousands / tens of thousands.

But I can work with that for now.

@hydrosquall
Copy link
Member

hydrosquall commented Jan 23, 2025 via email

@rlm1023
Copy link
Author

rlm1023 commented Jan 23, 2025

All of them, so thousands. This is likely not a realistic use case, as coloring by even 1000 values would likely not be a very useful visualization, and at that point we'd take it back to design. So in general, I'd expect the number to be generally small.

@hydrosquall
Copy link
Member

hydrosquall commented Jan 24, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants