首页 > 解决方案 > 使用另一个聚合字段过滤聚合图表

问题描述

我正在尝试制作类似于K-top示例的内容。

除了过滤掉和显示相同的聚合字段数据之外,我想要:

我在这里创建了一个可观察的笔记本构建我的测试用例,这就是我的目标。

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "data": {"url": "data/seattle-weather.csv"},
  "transform": [
    {"timeUnit": "month", "field": "date", "as": "month_date"},
    {
      "joinaggregate": [
        {"op": "mean", "field": "precipitation", "as": "mean_precipitation"},
        {"op": "max", "field": "precipitation", "as": "max_precipitation"}
      ],
      "groupby": ["month_date"]
    },
    {
      "aggregate": [
        {"as": "aggregation", "field": "precipitation", "op": "mean"}
      ],
      "groupby": ["month_date"]
    },
    {"window": [{"op": "row_number", "as": "rank"}]},
    {"calculate": "datum.rank <= 100? datum.month_date : null", "as": "dates"},
    {"filter": "datum.dates != null"}
  ],
  "encoding": {
    "x": {"field": "dates", "type": "ordinal", "timeUnit": "month"}
  },
  "layer": [
    {
      "mark": {"type": "bar"},
      "encoding": {
        "y": {
          "aggregate": "max",
          "field": "precipitation",
          "type": "quantitative"
        }
      }
    },
    {
      "mark": "tick",
      "encoding": {
        "y": {
          "aggregate": "mean",
          "field": "precipitation",
          "type": "quantitative"
        },
        "color": {"value": "red"},
        "size": {"value": 15}
      }
    }
  ]
}

我觉得我错过了一些链接GroupBy.ngroup来自pandas.DataFrame

标签: group-byfilteringvega-litealtair

解决方案


您可以按照 Vega-Lite 的Filtering Top-K Items示例以及额外的聚合转换来执行此操作。这是一个从上面调整您的规范的示例(vega 编辑器):

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "title": "Top Months by Mean Precipitation",
  "data": {"url": "data/seattle-weather.csv"},
  "transform": [
    {"timeUnit": "month", "field": "date", "as": "month_date"},
    {
      "aggregate": [
        {"op": "mean", "field": "precipitation", "as": "mean_precipitation"},
        {"op": "max", "field": "precipitation", "as": "max_precipitation"}
      ],
      "groupby": ["month_date"]
    },
    {
      "window": [{"op": "row_number", "as": "rank"}],
      "sort": [{"field": "mean_precipitation", "order": "descending"}]
    },
    {"filter": "datum.rank < 10"}
  ],
  "encoding": {
    "x": {
      "field": "month_date",
      "type": "ordinal",
      "timeUnit": "month",
      "title": "month (descending by max precip)",
      "sort": {
        "field": "max_precipitation",
        "op": "average",
        "order": "descending"
      }
    }
  },
  "layer": [
    {
      "mark": {"type": "bar"},
      "encoding": {
        "y": {
          "field": "mean_precipitation",
          "type": "quantitative",
          "title": "precipitation (mean & max)"
        }
      }
    },
    {
      "mark": "tick",
      "encoding": {
        "y": {"field": "max_precipitation", "type": "quantitative"},
        "color": {"value": "red"},
        "size": {"value": 15}
      }
    }
  ]
}

在此处输入图像描述


推荐阅读