Filtering an aggregated chart with another aggregation field

 ̄綄美尐妖づ 提交于 2020-03-03 09:45:27

问题


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}
      }
    }
  ]
}

I feel I'm missing something link the GroupBy.ngroup from pandas.DataFrame


回答1:


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}
      }
    }
  ]
}



来源:https://stackoverflow.com/questions/60151507/filtering-an-aggregated-chart-with-another-aggregation-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!