Reputation: 395
I was wondering if it is possible to display both the top and bottom 10 objects (quantitative) next to each other in vega lite.
I know how to get them individually but not both:
"transform": [
{
"window": [{"op": "rank", "as": "rank"}],
"sort": [{"field": "Days_to_maturity", "order": "descending"}]
},
{"filter": "datum.rank <= 10"}
],
Upvotes: 2
Views: 926
Reputation: 86463
Yes, you can do something like this:
"transform": [
{
"window": [{"op": "rank", "as": "rank"}],
"sort": [{"field": "Days_to_maturity", "order": "descending"}]
},
{"joinaggregate": [{"op": "max", "field": "rank", "as": "total"}]}
{"filter": "(datum.rank <= 10) || (datum.rank > datum.total - 10)"}
],
Note that the rank
aggregate will give equal values the same rank, so this may not display exactly 10 at each end if there are duplicate values in your data. If you want to just display the first and last 10 rows regardless of duplicates, you can change to a count
window with ignorePeers
:
"transform": [
{
"window": [{"op": "count", "as": "rank"}],
"sort": [{"field": "Days_to_maturity", "order": "descending"}],
"ignorePeers": true
},
{"joinaggregate": [{"op": "max", "field": "rank", "as": "total"}]},
{"filter": "(datum.rank <= 10) || (datum.rank > datum.total - 10)"}
],
You can find more details on window transform options in the Window Transform docs.
Upvotes: 1