changing colour of marks based on data values [vega-lite]

I understand that I can use "color": {"value": "#ff9900"} to change the colours of the marks but is it possible to have different colours for different range of values? Can I use if statements to achieve that and if so, could you tell me how or point me to a Notebook or tutorial that illustrates this please?

1 Like

In Vega-Lite you can specify the color encoding just like any other, with a key accessor, a scale etc.

For example in https://observablehq.com/@mbostock/vega-lite-scatterplot

you could change the encoding definition to

  encoding: {
    color: {field: "Origin", type: "ordinal", scale: {range: ["black", "steelblue", "orange"]}},
    x: {field: "Horsepower", type: "quantitative"},
    y: {field: "Miles_per_Gallon", type: "quantitative"},
  }

I’m sorry I don’t know enough about vega-lite for your other questions; I’ve add “vega lite” in their titles in the hope that it will attract other people who can help :slight_smile:

Thanks, Philippe, for the edit.

I’m afraid that’s not what I’m looking for. Please allow me to clarify my question… Say my data was:

0: Object {caseId: "1", total: "10"}
1: Object {caseId: "2", total: "5"}
3: Object {caseId: "3", total: "9"}
4: Object {caseId: "4", total: "3"}
5: Object {caseId: "5", total: "2"}

And I wanted the mark to be blue where total <=5 and red where total > 5, how or where do I specify this condition?

in the same notebook try

"color": {
  "condition": {"test": "datum['Horsepower'] < 100", "value": "black"},
  "value": "red"
},

see https://vega.github.io/vega-lite/docs/condition.html

It works! Thank you! Now I’ll just have to figure out the formatting so it’ll work in my script.

1 Like