Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vega-Lite layered chart: how to get tick marks and tick labels to span the entire axis?

Tags:

vega-lite

I am working on a layered chart where I have both bars and rule lines. The issue I'm having is that on the x-axis, the tick marks and tick labels only appear under the bars and do not span the entire axis, making it so that there are no tick marks and labels underneath where the rule lines are located. Here is an example of what I'm seeing (link to Vega editor):

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "data": {"url": "data/movies.json"},
  "transform": [
  {"calculate": "2*datum.IMDB_Rating", "as": "UpperLimit"}
  ],
  "layer": [
    {
      "mark": "bar",
      "encoding": {
        "x": {"bin": true, "field": "IMDB_Rating", "type": "quantitative"},
        "y": {"aggregate": "count", "type": "quantitative"}
      }
    },
    {
      "mark": "rule",
      "encoding": {
        "x": {
          "aggregate": "max",
          "field": "UpperLimit",
          "type": "quantitative"
        },
        "color": {"value": "red"},
        "size": {"value": 5}
      }
    }
  ]
}

Image of issue

How do I get the tick marks and labels to span the entire axis? Thanks in advance for the help!

like image 854
alleykati Avatar asked Nov 03 '25 14:11

alleykati


1 Answers

When you use a bin transform within an encoding, Vega-Lite adjusts the default axis properties to match the binning. You can re-adjust these manually via the encoding's scale and axis properties, but I think a simpler way is to move the bin transform to the chart's transform specification. Here is an example (Vega Editor):

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "data": {"url": "data/movies.json"},
  "transform": [
    {"calculate": "2*datum.IMDB_Rating", "as": "UpperLimit"},
    {
      "bin": true,
      "field": "IMDB_Rating",
      "as": ["IMDB_Rating_0", "IMDB_Rating_1"]
    }
  ],
  "layer": [
    {
      "mark": "bar",
      "encoding": {
        "x": {
          "field": "IMDB_Rating_0",
          "type": "quantitative",
          "bin": "binned"
        },
        "x2": {"field": "IMDB_Rating_1"},
        "y": {"aggregate": "count", "type": "quantitative"}
      }
    },
    {
      "mark": "rule",
      "encoding": {
        "x": {
          "aggregate": "max",
          "field": "UpperLimit",
          "type": "quantitative"
        },
        "color": {"value": "red"},
        "size": {"value": 5}
      }
    }
  ]
}

enter image description here

like image 145
jakevdp Avatar answered Nov 06 '25 11:11

jakevdp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!