Length of bar and text are not matching in bar visualization

Hi,

In below spec though same field (a_count) has been used for x-axis and text, text is shown correctly where as length of bars is incorrect (x-axis). May I pls know where the issue is?

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "width":600,
    "height":500,
  "data": {
    "values": [
      {"name": "a", "count": 3},
      {"name": "b", "count": 3},
      {"name": "b", "count": 3},
      {"name": "e", "count": 3},
      {"name": "e", "count": 3},
      {"name": "e", "count": 3},
      {"name": "g", "count": 1},
      {"name": "g", "count": 1},
       {"name": "a", "count": 3},
      {"name": "b", "count": 3},
      {"name": "b", "count": 3},
      {"name": "e", "count": 3},
      {"name": "e", "count": 3},
      {"name": "e", "count": 3},
      {"name": "g", "count": 1},
      {"name": "g", "count": 1}
    ]
  },
   "transform": [
        {
   "joinaggregate": [{
      "op": "count",
      "field": "name",
      "as": "b_count"
    }],
    "groupby": ["name"]
  },
    {
    "joinaggregate": [{
      "op": "distinct",
      "field": "count",
      "as": "no_count"
    }]
    },
    {"calculate": "datum.b_count/datum.no_count", "as": "a_count"}
   ],
  "encoding": {
    "y": {"field": "name", "type": "nominal", "sort": "-x", "title": null},
    "x": {"field": "a_count", "type": "quantitative", "title": null}
  },
  "layer": [
    {
      "mark": "bar",
      "encoding": {
        "color": {"field": "a_count", "type": "quantitative", "legend": null}
      }
    },
    {
      "mark": {
        "type": "text",
        "fontSize": 17,
        "align": "left",
        "dx": 5,
        "aria": false
      },
      "encoding": {
        "text": {"field": "a_count", "type": "quantitative"}
      }
    }
  ]
}

output :

image

I would probably recommend to debug the Vega visualization pipeline using the debug tools.
You can access the debug tools via Inspect => View: Request => View: Vega debug. The different steps of the pipeline are visible as data_X in the Data sets table.

This is what I see when opening the debug tools for your a_count and b_count values:

Hi @Marco_Liberati

Due to usage of joinaggregate transform function (for name field), a_count is repeated as per input instead of showing corresponding values only once. Because of this whenever a_count is used for x-axis it's adding all the values for particular parameter.

For example, a is repeated twice, due to which a bar length is shown as 2, where as I am expecting to show it as 1.

In case aggregate function is used in place of joinagrregate :

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",

  "data": {
    "values": [
      {"name": "a", "count": 3},
      {"name": "b", "count": 3},
      {"name": "b", "count": 1},
      {"name": "e", "count": 3},
      {"name": "e", "count": 3},
      {"name": "e", "count": 3},
      {"name": "g", "count": 1},
      {"name": "g", "count": 1},
       {"name": "a", "count": 1},
      {"name": "b", "count": 3},
      {"name": "b", "count": 3},
      {"name": "e", "count": 3},
      {"name": "e", "count": 3},
      {"name": "e", "count": 3},
      {"name": "g", "count": 1},
      {"name": "g", "count": 1}
    ]
  },
   "transform": [
        {
   "aggregate": [{
      "op": "count",
      "field": "name",
      "as": "b_count"
    }],
    "groupby": ["name"]
  },
    {
    "joinaggregate": [{
      "op": "distinct",
      "field": "count",
      "as": "no_count"
    }]
    },
    {"calculate": "datum.b_count/datum.no_count", "as": "a_count"}
 
   ],
  "encoding": {
    "y": {"field": "name", "type": "nominal", "sort": "-x", "title": null},
    "x": {"field": "a_count", "type": "quantitative", "title": null}
  },
  "layer": [
    {
      "mark": "bar",
      "encoding": {
        "color": {"field": "a_count", "type": "quantitative", "legend": null}
      }
    },
    {
      "mark": {
        "type": "text",
        "fontSize": 17,
        "align": "left",
        "dx": 5,
        "aria": false
      },
      "encoding": {
        "text": {"field": "a_count", "type": "quantitative"}
      }
    }
  ]
}

Here I am getting distinct values (no_count) as 1, whereas it should be 2.

May I know if there is any solution for this one?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.