Skip to content

How to impute zero values in a histogram? #5403

Open

Description

When creating a histogram, any bins with zero entries are left out of the data stream. This leads to strange results if you attempt to visualize a histogram with a stepped area chart. For example:

{
  "mark": {"type": "area", "interpolate": "step"},
  "encoding": {
    "x": {"type": "quantitative", "bin": true, "field": "x"},
    "y": {"type": "quantitative", "aggregate": "count"}
  },
  "data": {
    "values": [
      {"x": 0},
      {"x": 0},
      {"x": 0},
      {"x": 5},
      {"x": 5},
      {"x": 7},
      {"x": 10},
      {"x": 10}
    ]
  }
}

visualization - 2019-09-23T225703 917

Compare this to the bar chart, which is roughly what I would expect the area chart to look like:

{
  "mark": "bar",
  "encoding": {
    "x": {"type": "quantitative", "bin": true, "field": "x"},
    "y": {"type": "quantitative", "aggregate": "count"}
  },
  "data": {
    "values": [
      {"x": 0},
      {"x": 0},
      {"x": 0},
      {"x": 5},
      {"x": 5},
      {"x": 7},
      {"x": 10},
      {"x": 10}
    ]
  }
}

visualization - 2019-09-23T225804 851

The reason this happens is that the aggregation ignores bins that contain no points, as can be seen in this version of the chart:

{
  "mark": {"type": "line", "point": true},
  "encoding": {
    "x": {"type": "quantitative", "bin": true, "field": "x"},
    "y": {"type": "quantitative", "aggregate": "count"}
  },
  "data": {
    "values": [
      {"x": 0},
      {"x": 0},
      {"x": 0},
      {"x": 5},
      {"x": 5},
      {"x": 7},
      {"x": 10},
      {"x": 10}
    ]
  }
}

visualization - 2019-09-23T225930 088

Is there any way to specify that the binned aggregate should actually return zero in bins that contain zero points?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions