即使聚合函数是非汇总函数,也会创建堆积条形图

Stacked bar chart is created even though aggregation function is non-summative

我有一个串联图,它有一个主线图,主线图有一个画笔选择工具,允许用户平移线和点并更改其他 4 个图上的数据。对于其他图表之一,我试图取折线图数据的平均值,但它不起作用。我没有给我一个单一的条形图,而是得到了堆叠条形图和错误:“即使聚合函数是非求和的(“均值”)也应用了堆叠”。

这是我的代码:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "title": "This is kinda sick yo",
  "data": {
    "url": "data/test3.csv"
  },
  "hconcat": [
    {
      "encoding": {
        "color": {
          "condition": {
            "selection": "brush",
            "title": "Species",
            "field": "Species",
            "type": "nominal",
            "scale": {"range": ["green", "#FFFF00", "red"]}
          },
          "value": "lightgray"
        },
        "x": {
          "field": "Variable",
          "type": "nominal",
          "axis": {"labelAngle": -45, "title": "Element",
        "grid": false}
        },
        "y": {
          "title": "Total",
          "field": "Total",
          "type": "quantitative"
        },
          "tooltip": [
      {"field": "Variable", "type": "nominal"},
      {"field": "Total", "type": "quantitative"},
    ]
      },
      "width": 550,
      "height": 300,
      "mark": {"type": "line", "point": "true"},
      "selection": {"brush": {"encodings": ["x"], "type": "interval"}},
      "transform": [{"filter": {"selection": "click"}}]
    },
      {
      "encoding": {
        "color": {
          "condition": {
            "selection": "click",
            "field": "Total",
            "type": "quantitative",
            "scale": {"range": ["green", "#FFFF00", "red"]}
          },
          "value": "lightgray"
        },
        "y": {"field": "Total", "aggregate": "average"},
        "x": {"title": "Species", "field": "Species", "type": "nominal"},
          "tooltip": [
      {"field": "Species", "type": "nominal"},
      {"field": "Total", "type": "quantitative", "aggregate": "average"},
      {"field": "Variable", "type": "nominal"}
    ]
      },
      "height": 300,
      "width": 80,
      "mark": "bar",
      "selection": {"click": {"encodings": ["color"], "type": "multi"}},
      "transform": [{"filter": {"selection": "brush"}}, ]
    },
    {
      "encoding": {
        "color": {
          "condition": {
            "selection": "click",
            "field": "Sex",
            "type": "nominal",
            "scale": {"range": ["#993162", "#75b0a2", "grey"]},
            "legend": null
          },
          "value": "lightgray"
        },
        "y": {"field": "Fisher Sex Value", "type": "quantitative", "aggregate": "mean"},
        "x": {"title": "Sex", "field": "Sex", "type": "nominal"},
          "tooltip": [
      {"field": "Sex", "type": "nominal"},
      {"field": "Fisher Sex Value", "type": "quantitative", "aggregate": "mean"},
    ]
      },
      "height": 300,
      "width": 75,
      "mark": "bar",
      "selection": {"click": {"encodings": ["color"], "type": "multi"}},
      "transform": [{"filter": {"selection": "brush"}}]
    },
      {
      "encoding": {
        "color": {
          "condition": {
            "selection": "click",
            "field": "Sex",
            "type": "nominal",
            "scale": {"range": ["#993162", "#75b0a2", "grey"]},
            "legend": null
          },
          "value": "lightgray"
        },
        "y": {"field": "Mink Sex Value", "type": "quantitative", "aggregate": "mean"},
        "x": {"title": "Sex", "field": "Sex", "type": "nominal"},
          "tooltip": [
      {"field": "Sex", "type": "nominal"},
      {"field": "Mink Sex Value", "type": "quantitative", "aggregate": "mean"},
    ]
      },
      "height": 300,
      "width": 75,
      "mark": "bar",
      "selection": {"click": {"encodings": ["color"], "type": "multi"}},
      "transform": [{"filter": {"selection": "brush"}}]
    },
      {
      "encoding": {
        "color": {
          "condition": {
            "selection": "click",
            "field": "Sex",
            "type": "nominal",
            "scale": {"range": ["#993162", "#75b0a2", "grey"]}
          },
          "value": "lightgray"
        },
        "y": {"field": "Otter Sex Value", "type": "quantitative", "aggregate": "mean"},
        "x": {"title": "Sex", "field": "Sex", "type": "nominal"},
          "tooltip": [
      {"field": "Sex", "type": "nominal"},
      {"field": "Otter Sex Value", "type": "quantitative", "aggregate": "mean"},
    ]
      },
      "height": 300,
      "width": 75,
      "mark": "bar",
      "selection": {"click": {"encodings": ["color"], "type": "multi"}},
      "transform": [{"filter": {"selection": "brush"}}]
    }
  ]
}

第一个图是折线图,第二个图是聚合失败的图,我得到了堆栈。Here is an image of what the graph looks like currently。任何帮助将不胜感激。

Vega-Lite 的编码聚合将按您在一组编码中指定的未聚合字段隐式分组。第二个图表编码的简化版本如下所示:

{
  "encoding": {
  "color": {"field": "Total"},
  "y": {"field": "Total", "aggregate": "average"},
  "x": {"field": "Species"},
  "tooltip": [
    {"field": "Species"},
    {"field": "Total", "aggregate": "average"},
    {"field": "Variable"}
  ]

未聚合的编码是 ["Total", "Species", "Variable"],因此在计算每个组内 Totalaverage 之前,操作将 group-by 这些。在每组中取 Total 的平均值之前按 Total 的唯一值分组可能不是您所希望的。

也许从该图表中删除颜色编码会给您带来更有意义的结果。