How can I use facets in Vega-lite with sub aggregations?

Hi all!
I am trying to make this sub aggregation turn into a Vega-lite bar visualization in Kibana:

GET index_do/_search
{
  "size" : 0,
       "aggs": {
              "sube": {
                    "terms": {
                          "field": "iName.keyword"
                           
  }, 
        "aggs":{
            "transacoec": {
                       "terms": {
                           "field": "mo.keyword"
                      }
              }
        }
     }
   }
}

This query runs in dev tools and return the correct buckets. Then I put it in vega and I can't run with the "facet" option.

I don't think this is enough information to help you. Can you please try the steps listed here under "asking for help with Vega"? https://www.elastic.co/guide/en/kibana/current/vega-reference.html#asking-for-help-with-a-vega-spec

1 Like

Hi @wylie, thank you so much for the quick help and the instructions. Just to be more precise, I want to do faceting to make what is called small multiples/trellis chart. Here is my gist:

Here is my Vega-lite code in Kibana. I already tried changing the property field and tried wrapping code in a spec tag, and declaring the row field inside encoding. I commented this parts in the code, as I am not sure what to do.

That information was helpful, and I've identified a few misunderstandings that will help you with defining Vega specs in the future.

  1. Like I wrote in the Vega-Lite tutorial, you should be applying a flatten transform because Vega-Lite requires it: https://www.elastic.co/guide/en/kibana/current/vega-lite-tutorial-create-your-first-visualizations.html

  2. Your facet was using the wrong name. The JSON response contains key, not iName.keyword.

  3. Your encoding contains the wrong level of keys. Once you transform the results, you need to encode the inner level like flattened_name.key and flattened_name.doc_count

Putting this together, here is the spec that I made based on your request https://gist.github.com/wylieconlon/e72b30da35615fc6c59b3a0eef779117

1 Like

Thank you so much @wylie, it is working. But I can't manage to size my small multiples.
I was able to split by row and columns, but it is so big that the plots don't fit in a dashboard. Tried this below, but no success:

config: {
view: {
  width: 100
   height: 100
}
  }
  
  
  autosize: {
  type: fit
  
  }

View composition doesn't work well with autosizing, but you can set height and width inside your spec property next to your mark

@wylie thanks again for the help.
I'm unable to find how to declare spec property (although it was commented in my code, I didn't use). It returns me "Invalid specification".
I'm using vega-lite schema, is it possible? Or spec is just for vega schema?

In the JSON that I previously sent you, there is a property called spec. Inside that you can provide a height or width

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.