使用另一个聚合字段过滤聚合图表 [英] Filtering an aggregated chart with another aggregation field

查看:156
本文介绍了使用另一个聚合字段过滤聚合图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成类似于

I'm trying to produce something similar to the K-top example.

除了要过滤掉并显示相同的汇总字段数据外,我要:

Except that instead of filtering out and displaying the same aggregated field data, I want:

  • 显示一种类型的汇总数据(每日最高温度)
  • 并过滤另一个汇总字段(每日温度的平均值)
  • to display one type of aggregated data (the max of daily temps)
  • and filter on another aggregation field ( the mean of daily temps)

我已经在此处创建了一个可观察的笔记本,以构建我的测试用例,这是到现在为止我知道了.

I've created an observable notebook here to build my test case, and this is how far I got.

{
  "$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}
      }
    }
  ]
}

我觉得我缺少从pandas.DataFrame

推荐答案

您可以按照Vega-Lite的过滤Top-K项目示例以及额外的汇总转换.下面是一个例子,从上述(蔬菜编辑器):

You can do this following Vega-Lite's Filtering Top-K Items example along with an extra aggregate transform. Here is an example adapting your spec from above (vega editor):

{
  "$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}
      }
    }
  ]
}

这篇关于使用另一个聚合字段过滤聚合图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆