alleykati
alleykati

Reputation: 41

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

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!

Upvotes: 4

Views: 702

Answers (1)

jakevdp
jakevdp

Reputation: 86310

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

Upvotes: 3

Related Questions