Search code examples
vega-lite

How to use zero=false in vega-lite when also using a color encoding?


I am trying to figure out how to not have my y-axis start at zero? It works in general for me, but if I add the color encoding (see below) it is not working anymore and instead I get to see the zero.

    {
      "data": {"name": "d"},
      "mark": {"type": "bar"},
      "encoding": {
            "color": {"type": "nominal", "field": "group"},
            "x":     {"type": "nominal", "field": "model"},
            "y": {
              "type": "quantitative",
              "field": "inf_f1",
              "scale": {"zero": false}
            }
          },
      "$schema": "https://vega.github.io/schema/vega-lite/v4.0.2.json",
      "datasets": {
        "d": [
          {
            "model": "lr-bow",
            "inf_f1": 0.7991841662090597,
            "group" : "A"
          },
          {
            "model": "fcn-bow",
            "inf_f1": 0.8220151833558302,
            "group" : "B"
          }
        ]
      }
    }

This is how to should not look like


Solution

  • The reason the scale includes zero is that bars are stacked by default, and each bar has an implicit stacked zero-height bar for the group that does not appear, but does affect the automatically chosen axis limits. You can address this by setting stack to "none" in the y encoding (view in editor):

    {
      "data": {"name": "d"},
      "mark": {"type": "bar"},
      "encoding": {
        "color": {"type": "nominal", "field": "group"},
        "x": {"type": "nominal", "field": "model"},
        "y": {
          "type": "quantitative",
          "field": "inf_f1",
          "stack": "none",
          "scale": {"zero": false}
        }
      },
      "datasets": {
        "d": [
          {"model": "lr-bow", "inf_f1": 0.7991841662090597, "group": "A"},
          {"model": "fcn-bow", "inf_f1": 0.8220151833558302, "group": "B"}
        ]
      }
    }
    

    enter image description here