Vega Lite中的多个数据集

发布于 2025-02-11 19:24:54 字数 3836 浏览 0 评论 0 原文

我正在尝试使用Vega Lite构建数值数据的直方图。 Right now I am prototyping the visualization using a very simple mock dataset (Also available 这里):

{
    "data": {
    "fill": [
        {"count": 30000, "level": "filled"},
        {"count": 50000, "level": "missing"}
    ],
    "histogram": [
        {"bin_end": 20, "bin_start": 0, "count": 1000},
        {"bin_end": 30, "bin_start": 20, "count": 20000}
    ]
    },
    "metadata": {}
}

上面的数据格式是预定的,不幸的是,我无法更改它,因为它来自API。我正在尝试绘制数据的直方图部分,以绘制数据的直方图和数据的填充部分,以绘制简单的条形图。像这样:

“所需结果”

我知道我可以使用“属性”选项来访问这样的嵌套数据,如 vega文档的本节,只要我只绘制其中一个图表,如下所示:

Example 1 in Vega Editor: Histogram only

Example 2 in Vega Editor: Barplot only

但是,当我尝试将它们都放在一起时,它根本不起作用。我在下面得到了怪异的图表,在那里看来Barplot的数据完全不存在。

以及使用内置数据查看器内置的VEGA编辑器检查数据时,似乎只有直方图数据正在读取。

此外,此行为似乎取决于订单,因为在HCONCAT块中切换图表的顺序更改图表:

​/vega.github.io/editor/#/url/vega-lite/N4IgJAzgxgFgpgWwIYgFwhgF0wBwqgegIDc4BzJAOjIEtMYBXAI0poHsDp5kTykBaADZ04JAKyUATJQAMlAFYQ2AOxAAaEFBUAzGmTShiNOAHcDmlZhrKGbBhAAScPVjQBmGTI1blVm3YgAdRoAE3o0ABZPAF9ojRgfKCRMNABtUBDklFRQbTYAJ2QUnJBMAE8cODQQRRV1EBx8tkr88urMzCpdQUEQOJAGfN70LFx8IloITGo6RiZ7OHyfTDhfSi0EAm4kMiUCbQB2Jgi3AA4ARiY4E6Y3CLgoa5DTpCQD06gATjcANigDkJiAj5JAmAgyJhIOAyc4HI4vSRIMQhNxuOCSNxQGQhc4-CIhCInT4hSQRF5IT7abQEBBsKAAazUtVU-VWWhC1n0JS0ggK5l0cEEIWqgjgpF6GnKlWqyjYCGsSF6-QAHvzjEKRWLBfUpVV0LL5cpFX0NGU1YLhegtAxfDqKnqQABHBhIXx0ZI0Uh9frIfL06qQ-ImjJZfkFIrmXXVZn1RrNRZtdAdKgwGhTNhkEEIE0DIbVUZ4QgESbTWj0ZgLJaWVbTDZbRA7PaHY5nS7XNy3e6PfHk95fX7-QHA0HgyHQ2HwpCI5Go9GY7G4-GEtzE0nkynU2kMplKFkaNlsDnKLmgVUlJjWcwX5TKOCWzD5BhwfoCjXoa8AfSmSFa9Wgir1UAo3QYRbx-b0NGVSRzTfEBP1WYV+jNEpX0tCwbRSSV7WqZ1XSsTorC9WINF9f133A6IAF1oiAA" rel="nofollow noreferrer">Inverted Chart

Am I在这里缺少什么?这是Vegalite的某种限制吗?

I'm trying to build a visualization for histograms of numerical data using Vega Lite. Right now I am prototyping the visualization using a very simple mock dataset (Also available here):

{
    "data": {
    "fill": [
        {"count": 30000, "level": "filled"},
        {"count": 50000, "level": "missing"}
    ],
    "histogram": [
        {"bin_end": 20, "bin_start": 0, "count": 1000},
        {"bin_end": 30, "bin_start": 20, "count": 20000}
    ]
    },
    "metadata": {}
}

The data format above is predetermined and unfortunately I am not able to change it as it comes from an API. I'm trying to plot the histogram section of the data to plot, well, an histogram, and the fill section of the data to plot a simple bar chart. Something like this:

Desired Outcome

I understand that I can use the "property" option to access nested data like this, as document in this section of Vega documentation, and this works as long as I am only plotting one of the charts, as shown by the examples below:

Example 1 in Vega Editor: Histogram only

Example 2 in Vega Editor: Barplot only

However, when I try to put both of them together it simply does not work. I get the weird chart below, where it seems that the data for the barplot is completely absent.

Weird chart

Link to vega editor for weird chart

And when inspecting the data using Vega Editor built in Data Viewer it seems that only the histogram data is being read.

Data viewer

Furthermore, this behavior seems to be order dependent, as switching the order of the charts in the HConcat block changes which chart gets messed up:

enter image description here

Inverted Chart

Am I missing something here? Is this some sort of limitation of Vegalite?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你不解释 2025-02-18 19:24:54

您缺少名称属性,因此看起来数据只是被最后检索到的任何内容所覆盖。干得好。

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.2.0.json",
  "config": {"view": {"continuousHeight": 300, "continuousWidth": 400}},
  "hconcat": [
    {
      "data": {"name": "a",
        "format": {"type": "json", "property": "data.histogram"},
        "url": "https://gist.githubusercontent.com/hemagso/f7b4381be43b34ece4d8aa78c936c7d5/raw/0bae0177b8a2a5d33e23c0d164d4439d248aa9ff/mock,json"
      },
      "encoding": {
        "x": {
          "bin": {"binned": true},
          "field": "bin_start",
          "scale": {"type": "linear"}
        },
        "x2": {"field": "bin_end"},
        "y": {"field": "count", "type": "quantitative"}
      },
      "mark": "bar"
    },
    {
      "data": {"name": "b",
        "format": {"type": "json", "property": "data.fill"},
        "url": "https://gist.githubusercontent.com/hemagso/f7b4381be43b34ece4d8aa78c936c7d5/raw/0bae0177b8a2a5d33e23c0d164d4439d248aa9ff/mock,json"
      },
      "encoding": {
        "color": {"field": "level", "type": "nominal"},
        "x": {"field": "level", "type": "nominal"},
        "y": {"field": "count", "type": "quantitative"}
      },
      "mark": "bar"
    }
  ]
}

You're missing the name property so it looks like the data was simply overwritten by whatever was retrieved last. Here you go.
enter image description here

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.2.0.json",
  "config": {"view": {"continuousHeight": 300, "continuousWidth": 400}},
  "hconcat": [
    {
      "data": {"name": "a",
        "format": {"type": "json", "property": "data.histogram"},
        "url": "https://gist.githubusercontent.com/hemagso/f7b4381be43b34ece4d8aa78c936c7d5/raw/0bae0177b8a2a5d33e23c0d164d4439d248aa9ff/mock,json"
      },
      "encoding": {
        "x": {
          "bin": {"binned": true},
          "field": "bin_start",
          "scale": {"type": "linear"}
        },
        "x2": {"field": "bin_end"},
        "y": {"field": "count", "type": "quantitative"}
      },
      "mark": "bar"
    },
    {
      "data": {"name": "b",
        "format": {"type": "json", "property": "data.fill"},
        "url": "https://gist.githubusercontent.com/hemagso/f7b4381be43b34ece4d8aa78c936c7d5/raw/0bae0177b8a2a5d33e23c0d164d4439d248aa9ff/mock,json"
      },
      "encoding": {
        "color": {"field": "level", "type": "nominal"},
        "x": {"field": "level", "type": "nominal"},
        "y": {"field": "count", "type": "quantitative"}
      },
      "mark": "bar"
    }
  ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文