Search code examples
vega-lite

Vega lite: having two separate datasets with separate filters


I was wondering how I could have a sort of hconcat in vega lite, but with two separate datasets. I want to have the top 10 and lower 10 values displayed next to each other with different constraints but I am having issues with the filter.


Solution

  • Data and transforms can be specified at any level of the chart, and they are passed down to subcharts when applicable. Here is an example adapted from Filtering Top K Items in the example gallery, which applies a different filter to each concatenated chart (view in editor):

    {
      "data": {
        "values": [
          {"student": "A", "score": 100},
          {"student": "B", "score": 56},
          {"student": "C", "score": 88},
          {"student": "D", "score": 65},
          {"student": "E", "score": 45},
          {"student": "F", "score": 23},
          {"student": "G", "score": 66},
          {"student": "H", "score": 67},
          {"student": "I", "score": 13},
          {"student": "J", "score": 12},
          {"student": "K", "score": 50},
          {"student": "L", "score": 78},
          {"student": "M", "score": 66},
          {"student": "N", "score": 30},
          {"student": "O", "score": 97},
          {"student": "P", "score": 75},
          {"student": "Q", "score": 24},
          {"student": "R", "score": 42},
          {"student": "S", "score": 76},
          {"student": "T", "score": 78},
          {"student": "U", "score": 21},
          {"student": "V", "score": 46}
        ]
      },
      "transform": [
        {
          "window": [{"op": "rank", "as": "rank"}],
          "sort": [{"field": "score", "order": "descending"}]
        }
      ],
      "vconcat": [
        {
          "transform": [{"filter": "datum.rank <= 3"}],
          "mark": "bar",
          "encoding": {
            "x": {"field": "score", "type": "quantitative"},
            "y": {
              "field": "student",
              "type": "nominal",
              "sort": {"field": "score", "op": "average", "order": "descending"}
            }
          }
        },
        {
          "transform": [{"filter": "datum.rank > 19"}],
          "mark": "bar",
          "encoding": {
            "x": {"field": "score", "type": "quantitative"},
            "y": {
              "field": "student",
              "type": "nominal",
              "sort": {"field": "score", "op": "average", "order": "descending"}
            }
          }
        }
      ]
    }
    

    enter image description here

    Similarly, if you wanted a different data source in each subchart, you could specify the "data" property in the subcharts.