首页 > 解决方案 > Vega-lite default bar width strange

问题描述

I'm seeing the following oddly styled chart. I understand I can explicitly change the padding etc., but the default vega-lite layout is usually pretty good. I'm confused what I'm doing that's leading to this sub-normal behavior. Thanks! Here is the code in the vega-lite editor

e

I understand that I can also change x's type to ordinal to make the styling better, though I'm not sure I understand still why it is the difference I see. I need the type to be quantitative so I get the min/max brush bound, as opposed to the set.

Also I actually do not even know how to manually set the bar width after reading the documentation here https://vega.github.io/vega-lite/docs/scale.html. If anyone might have a working example that would be great.

Thanks.

标签: vega-lite

解决方案


正如@marcprux 所提到的,有预装箱支持,因此您不必在此处重复装箱转换。但是,目前 prebinned 支持同时需要bin_startbin_end

现在您可以修改规范以派生一个新的 bin_end 字段并将其与 x2 一起使用。

{
  "data": ...
  "transform": [{
    "calculate": "datum.ShareWomen_bin+0.1",
    "as": "ShareWomen_bin_end"
  }],
  "mark": "bar",
  "encoding": {
    "x": {"bin": {"binned": true, "step": 0.1}, "field": "ShareWomen_bin", "type": "quantitative", "title": "ShareWomen_bin"},
    "x2": {"field": "ShareWomen_bin_end"},
    "y": {"field": "count", "type": "quantitative"}
  }
}

喜欢这个规范

在此处输入图像描述

我可以看到我们不应该要求派生 bin_end ,因此创建了一个问题来跟踪此功能请求:https ://github.com/vega/vega-lite/issues/6086 。

顺便说一句,定量比例仅影响条形位置。

要直接设置条形大小,可以在标记定义中使用 size 属性:

mark: {type: "bar", size: 5}

推荐阅读