Vega-lite默认条宽奇怪

问题描述 投票:1回答:2

我看到以下样式奇怪的图表。我知道我可以显式更改padding等,但是默认的vega-lite布局通常非常好。我很困惑自己正在做的事情导致这种不正常的行为。谢谢! Here is the code in the vega-lite editor

e

[我知道我也可以将x的type更改为ordinal,以使样式更好,尽管我不确定我仍然理解为什么这是我看到的差异。我需要类型是定量的,以便获得最小/最大画笔范围,而不是集合。

而且我实际上甚至在阅读此处https://vega.github.io/vega-lite/docs/scale.html的文档后,甚至都不知道如何手动设置条宽度。如果任何人都有一个可行的例子,那就太好了。

谢谢。

vega-lite
2个回答
0
投票

由于您将“ x”声明为定量字段,因此不存在沿轴的点均匀分布的假设。例如,您可以在其他数据点之间添加一些数据点:

      {"ShareWomen_bin": 0.83, "count": 40, "is_overview": true},
      {"ShareWomen_bin": 0.87, "count": 70, "is_overview": true},

您会看到它们呈现在其他条之间:

enter image description here

正如您提到的,您可以指定条形图应编码为序数值。另一种解决方案是将其保留为定量,但指定将其装箱,在这种情况下,小节也将呈现为顺序的:

    "x": {"field": "ShareWomen_bin", "type": "quantitative", "bin": true},

enter image description here

由于您的数据似乎已经被装箱,因此您应该阅读有关vega-lite如何支持预装箱的数据的信息:https://vega.github.io/vega-lite/docs/bin.html#binned


0
投票

正如@marcprux所提到的,已经有预先绑定的支持,因此您不必在这里重复bin转换。但是,当前预绑定支持同时需要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"}
  }
}

像这个spec

enter image description here

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

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

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

mark: {type: "bar", size: 5}
© www.soinside.com 2019 - 2024. All rights reserved.