Skip to content

Diverging color scale with domainMid not in the domain is awkward #4151

@kanitw

Description

@kanitw

Bug Description

Example: if the data range from 19 to 91, but the domainMid is -20, it's producing a funny scale

{
  "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
  "description": "A simple bar chart with embedded data.",
  "data": {
    "values": [
      {"a": "A", "b": 28}, {"a": "B", "b": 55}, {"a": "C", "b": 43},
      {"a": "D", "b": 91}, {"a": "E", "b": 81}, {"a": "F", "b": 53},
      {"a": "G", "b": 19}, {"a": "H", "b": 87}, {"a": "I", "b": 52}
    ]
  },


    "mark": "bar",
    "encoding": {
      "x": {"field": "a", "type": "nominal", "axis": {"labelAngle": 0}},
      "y": {"field": "b", "type": "quantitative"},
      "color": {
        "field": "b", "type": "quantitative",
        "scale": {"type": "linear", "scheme": "redblue", "domainMid": -20}
      }
    }
}
Image

Expected:

the mid should be mapped to the middle of the ramp

Image

Note that we if I add another layer with datum = -20

{
  "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
  "description": "A simple bar chart with embedded data.",
  "data": {
    "values": [
      {"a": "A", "b": 28}, {"a": "B", "b": 55}, {"a": "C", "b": 43},
      {"a": "D", "b": 91}, {"a": "E", "b": 81}, {"a": "F", "b": 53},
      {"a": "G", "b": 19}, {"a": "H", "b": 87}, {"a": "I", "b": 52}
    ]
  },
  "layer": [{

    "mark": "bar",
    "encoding": {
      "x": {"field": "a", "type": "nominal", "axis": {"labelAngle": 0}},
      "y": {"field": "b", "type": "quantitative"},
      "color": {
        "field": "b", "type": "quantitative",
        "scale": {"type": "linear", "scheme": "redblue", "domainMid": -20}
      }
    }
  }, {
    "mark": "text",
    "encoding": {
      "color": {"datum": -20}
    }
  }]
}

then the color legend looks nice.

Image

Because the in the Vega output we now have

"domain": {"fields": [{"data": "data_0", "field": "b"}, [-20]]},

instead of just "domain": {"data": "data_0", "field": "b"}.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions