Search code examples
vegavega-lite

How to start bar from 1 instead of 0 in Vega Lite?


I'm plotting the diff time series [1.1, 0.9, 1.2, ...].

And as the values are relative multipliers, the middle is 1 and not 0.

Is there a way to tell Vega Lite to start bar with 1?

So for the value 1.1 the bar will be start: 1, end: 1.1 and for 0.9 it will be start: 1, end: 0.9?


Solution

  • You can use a y2 encoding with datum set to 1. For example (vega editor):

    {
      "data": {
        "values": [
          {"x": "A", "y": 0.9},
          {"x": "B", "y": 0.8},
          {"x": "C", "y": 1.1},
          {"x": "D", "y": 1.2},
          {"x": "E", "y": 0.9},
          {"x": "F", "y": 1.3}
        ]
      },
      "mark": "bar",
      "encoding": {
        "x": {"type": "nominal", "field": "x"},
        "y": {
          "type": "quantitative",
          "field": "y",
          "scale": {"domain": [0.6, 1.4]}
        },
        "y2": {"datum": 1}
      }
    }
    

    enter image description here