Altair / Vega-Lite刻度线图表:过滤顶部来自聚合字段的K条

时间:2018-06-18 09:21:51

标签: python vega vega-lite altair

我正在可视化具有例如分类字段和时间字段的数据集。我想创建一个条形图,显示不同类别的时间分布,这些类别按照“升序”/“降序”顺序排序,具体取决于它们的基数。这可以通过altair

来实现
import pandas as pd
import altair as alt

data = {0:{'Name':'Mary', 'Sport':'Tennis', 'competition':'2018/06/01'},
    1:{'Name':'Cal', 'Sport':'Tennis','competition':'2018/06/05'},
    2:{'Name':'John', 'Sport':'Tennis','competition':'2018/05/28'},
    3:{'Name':'Jane', 'Sport':'Tennis','competition':'2018/05/20'},
    4:{'Name':'Bob', 'Sport':'Golf','competition':'2018/03/01'},
    5:{'Name':'Jerry', 'Sport':'Golf','competition':'2018/03/03'},
    6:{'Name':'Gustavo', 'Sport':'Golf','competition':'2018/02/28'},
    7:{'Name':'Walter', 'Sport':'Swimming','competition':'2018/01/01'},
    8:{'Name':'Jessy', 'Sport':'Swimming','competition':'2018/01/03'},
    9:{'Name':'Patric', 'Sport':'Running','competition':'2018/02/01'},
    10:{'Name':'John', 'Sport':'Shooting','competition':'2018/04/01'}}

df = pd.DataFrame(data).T

alt.Chart(df).mark_tick().encode(
    x='yearmonthdate(competition):T',
    y=alt.Y('Sport:N',
        sort=alt.SortField(field='count(Sport:N)', order='ascending', op='sum')
    ),
)

Strip plot

现在假设我只对前三个类别感兴趣。按照“Altair/Vega-Lite bar chart: filter top K bars from aggregated field”的公认解决方案,这次情节不会出现:

alt.Chart(df).mark_tick().encode(
    x='yearmonthdate(competition):T',
    y=alt.Y('Sport:N',
        sort=alt.SortField(field='count', order='ascending', op='sum')
    ),
).transform_aggregate(
    count='count()',
    groupby=['Sport']
).transform_window(
    window=[{'op': 'rank', 'as': 'rank'}],
    sort=[{'field': 'count', 'order': 'descending'}]
).transform_filter('datum.rank <= 3')

Filtered Strip plot

请注意,即使y标签订单也不符合预期。

1 个答案:

答案 0 :(得分:0)

更深入地阅读(并理解)文档,我认为我可以说,altair / Vega-Lite当前(2018年6月)的要求是不可行的。这是我的解释...

对数据执行聚合转换,等同于在SQL查询上添加GROUP BY子句,因此我们不再能够将任何“原始”数据字段以其“未聚合”形式关联到编码通道:当我尝试在competition频道中引用x时,因此是undefined

我可以尝试使用查找转换来“自我加入”,但是即使在这种情况下,最终结果也不是我想要的结果,因为这等效于left join,因此我只得到一个值每个聚合类。

alt.Chart(df).mark_tick().encode(
    x=alt.X(field='competition',type='temporal', timeUnit='yearmonthdate'),
    y=alt.Y('Sport:N',
        sort=alt.SortField(field='count', order='ascending', op='sum')
    ),
).transform_aggregate(
    countX='count()',
    groupby=['Sport']
).transform_window(
    window=[{'op': 'rank', 'as': 'rank'}],
    sort=[{'field': 'countX', 'order': 'descending'}]
).transform_filter('datum.rank <= 3').transform_lookup(
    lookup='Sport',
    from_=alt.LookupData(data=df, key='Sport',
                         fields=['competition'])
)

Plot after join

我发现Vega当前支持实现我想要的结果所必需的东西,而Vega-LiteAltair中都不支持:正是JoinAggregate变换一种或多种汇总的结果“扩展”原始数据。

对于以下输入数据:

[
  {"foo": 1, "bar": 1},
  {"foo": 1, "bar": 2},
  {"foo": null, "bar": 3}
]

联接聚合转换:

{
  "type": "joinaggregate",
  "fields": ["foo", "bar", "bar"],
  "ops": ["valid", "sum", "median"],
  "as": ["v", "s", "m"]
}

产生输出:

[
  {"foo": 1, "bar": 1, "v": 2, "s": 6, "m": 2},
  {"foo": 1, "bar": 2, "v": 2, "s": 6, "m": 2},
  {"foo": null, "bar": 3, "v": 2, "s": 6, "m": 2}
]