Skip to content

Vega-Lite conditional colour based on values #8814

Answered by mattijn
naveenwills asked this question in Q&A
Discussion options

You must be logged in to vote

You can make use of a threshold including domain and range for the scale definition:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {"url": "data/cars.json"},
  "mark": "rect",
  "encoding": {
    "y": {"field": "Origin", "type": "nominal"},
    "x": {"field": "Cylinders", "type": "ordinal"},
    "color": {
      "aggregate": "mean",
      "field": "Horsepower",
      "scale": {
        "type": "threshold",
        "domain": [80, 110, 140],
        "range": ["blue", "orange", "brown", "red"]
      },
      "type": "quantitative",
      "legend":{"orient":"top"}
    }
  }
}

Open the Chart in the Vega Editor

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@naveenwills
Comment options

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