首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >即使聚合函数是非总和函数,也会创建堆叠条形图

即使聚合函数是非总和函数,也会创建堆叠条形图
EN

Stack Overflow用户
提问于 2020-08-11 18:15:23
回答 1查看 71关注 0票数 0

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

下面是我的代码:

代码语言:javascript
复制
{
  "$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"}}]
    }
  ]
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-12 05:36:11

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

代码语言:javascript
复制
{
  "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之前,操作将按这些编码进行分组。在取每个组中的Total的平均值之前,按Total的唯一值进行分组可能不是您所希望的。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63356071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档