# Vega stacked bar plot group by

**URL:** https://discuss.elastic.co/t/vega-stacked-bar-plot-group-by/214833
**Category:** Kibana
**Tags:** vega
**Created:** [January 13, 2020, 1:02pm UTC](https://discuss.elastic.co/t/vega-stacked-bar-plot-group-by/214833 "2020-01-13T13:02:52Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![AstroSlazak](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@AstroSlazak](https://discuss.elastic.co/u/AstroSlazak)
#### Post date: [January 13, 2020, 1:02pm UTC](https://discuss.elastic.co/t/vega-stacked-bar-plot-group-by/214833/1 "2020-01-13T13:02:52Z")

</div>

Hi  
I have an an scripted field "TotalCapacity" which return string "Error" or "OK". I want to count how many of "Error" and "Ok" i have in different timestamp. I have many returns of "TotalCapacity" for one vehicle so i want to group by "vehicleid" i know that this is posible in VEGA but i dont know how to do it.

I want something like on pic, but grouped by "vehicle id"

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/4/d40a83e366fdf6daf814251cae0a4d42ce9963b5.png)  
This is my Vega code but does not work:

```
{
$schema: https://vega.github.io/schema/vega-lite/v2.json
data: {
 url: {
  %timefield%: @timestamp
  %context%: true
  index: rti-warnings*
  body: {
    size: 0
    aggs: {
      report: {
        date_histogram: {
          field: @timestamp
          interval: {%autointerval%: true}
		  }
        aggs: {
          Total: {
            cardinality: {field: "TotalCapacity"}
            }
          Vehicle: {
           cardinality: {field: "vehicleid"}
          }
        }
      }
    }
    
    
  }
}
format: {property: "aggregations.report.buckets"}
}
mark: bar
transform: [
{
  aggregate: [
    {op: "count", field: "Total", as: "Sum"}
  ]
  groupby: ["Vehicle.value"]
}
]
encoding: {
x: {field: "key", timeUnit: "date", type: "temporal", axis: {title: false} }
y: { field: "Sum", type: "quantitative" }
}
}
```

---

<div class="post-metadata">

### Author: ![Midas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/midas/32/50275_2.png) [@Midas](https://discuss.elastic.co/u/Midas)
#### Post date: [February 6, 2020, 4:08pm UTC](https://discuss.elastic.co/t/vega-stacked-bar-plot-group-by/214833/2 "2020-02-06T16:08:49Z")

</div>

AstroSlazak,

Are you looking to do this solely in Vega-Lite? Or would doing this in Vega work for you? If you are good with Vega, I can help you out pretty easily there.

-Midas

---

<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: [March 5, 2020, 4:08pm UTC](https://discuss.elastic.co/t/vega-stacked-bar-plot-group-by/214833/3 "2020-03-05T16:08:49Z")

</div>

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