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

**URL:** https://discuss.elastic.co/t/how-can-i-use-facets-in-vega-lite-with-sub-aggregations/260920
**Category:** Kibana
**Tags:** vega
**Created:** [January 12, 2021, 11:41pm UTC](https://discuss.elastic.co/t/how-can-i-use-facets-in-vega-lite-with-sub-aggregations/260920 "2021-01-12T23:41:05Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![marcosvrrs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcosvrrs/32/77073_2.png) [@marcosvrrs](https://discuss.elastic.co/u/marcosvrrs)
#### Post date: [January 12, 2021, 11:41pm UTC](https://discuss.elastic.co/t/how-can-i-use-facets-in-vega-lite-with-sub-aggregations/260920/1 "2021-01-12T23:41:05Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)
#### Post date: [January 13, 2021, 12:06am UTC](https://discuss.elastic.co/t/how-can-i-use-facets-in-vega-lite-with-sub-aggregations/260920/2 "2021-01-13T00:06:09Z")

</div>

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](https://www.elastic.co/guide/en/kibana/current/vega-reference.html#asking-for-help-with-a-vega-spec)

---

<div class="post-metadata">

### Author: ![marcosvrrs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcosvrrs/32/77073_2.png) [@marcosvrrs](https://discuss.elastic.co/u/marcosvrrs)
#### Post date: [January 13, 2021, 11:34am UTC](https://discuss.elastic.co/t/how-can-i-use-facets-in-vega-lite-with-sub-aggregations/260920/3 "2021-01-13T11:34:07Z")

</div>

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:

> <https://gist.github.com/marcosvrrs/2246b50ae3a6e902e8561b36f0d0d383>

---

<div class="post-metadata">

### Author: ![marcosvrrs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcosvrrs/32/77073_2.png) [@marcosvrrs](https://discuss.elastic.co/u/marcosvrrs)
#### Post date: [January 13, 2021, 12:01pm UTC](https://discuss.elastic.co/t/how-can-i-use-facets-in-vega-lite-with-sub-aggregations/260920/4 "2021-01-13T12:01:36Z")

</div>

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.

> <https://gist.github.com/marcosvrrs/6f4460c7d794f447f58e465c6dcbe42d>

---

<div class="post-metadata">

### Author: ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)
#### Post date: [January 13, 2021, 4:02pm UTC](https://discuss.elastic.co/t/how-can-i-use-facets-in-vega-lite-with-sub-aggregations/260920/5 "2021-01-13T16:02:41Z")

</div>

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](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](https://gist.github.com/wylieconlon/e72b30da35615fc6c59b3a0eef779117)

---

<div class="post-metadata">

### Author: ![marcosvrrs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcosvrrs/32/77073_2.png) [@marcosvrrs](https://discuss.elastic.co/u/marcosvrrs)
#### Post date: [January 13, 2021, 9:10pm UTC](https://discuss.elastic.co/t/how-can-i-use-facets-in-vega-lite-with-sub-aggregations/260920/6 "2021-01-13T21:10:25Z")

</div>

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
  
  }
```

---

<div class="post-metadata">

### Author: ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)
#### Post date: [January 13, 2021, 9:27pm UTC](https://discuss.elastic.co/t/how-can-i-use-facets-in-vega-lite-with-sub-aggregations/260920/7 "2021-01-13T21:27:50Z")

</div>

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

---

<div class="post-metadata">

### Author: ![marcosvrrs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcosvrrs/32/77073_2.png) [@marcosvrrs](https://discuss.elastic.co/u/marcosvrrs)
#### Post date: [January 14, 2021, 1:05pm UTC](https://discuss.elastic.co/t/how-can-i-use-facets-in-vega-lite-with-sub-aggregations/260920/8 "2021-01-14T13:05:49Z")

</div>

@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?

---

<div class="post-metadata">

### Author: ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)
#### Post date: [January 14, 2021, 4:29pm UTC](https://discuss.elastic.co/t/how-can-i-use-facets-in-vega-lite-with-sub-aggregations/260920/9 "2021-01-14T16:29:24Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [February 11, 2021, 4:29pm UTC](https://discuss.elastic.co/t/how-can-i-use-facets-in-vega-lite-with-sub-aggregations/260920/10 "2021-02-11T16:29:26Z")

</div>

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