group-by - 使用另一个聚合字段过滤聚合图表
问题描述
我正在尝试制作类似于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
解决方案
您可以按照 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}
}
}
]
}
推荐阅读
- sql - SQL Server 维护订单索引(产品阵容)
- arrays - 在 F# 中的数组中创建重复序列的最佳方法是什么?
- file-upload - HttpEndRequest 错误处理和重试
- entity-framework - 使用 Entity Framework Core 设置更新级联约束
- javascript - 使用递归的反向链表问题
- iota - iota 如何防止数据篡改/数据不变性?
- mysql - SQL/HiveQL:有没有办法计算 JOIN ON 的数量
没有分组依据的每行匹配? 我想在两个表上执行 LEFT (OUTER) JOIN 并在结果表中有一列表示右表中的记录满足左表的 JOIN ON 条件的次数。
考虑下表:
Students Absences name ID ID date Kyle 1 1 7/04 C
- javascript - 表单标签被忽略
- matlab - 删除 xy 刻度但保留 xy 标签的子图
- android - Admob 奖励视频未在 Android 应用中加载