# Sum operator in stacked bar vega

**URL:** <https://discuss.elastic.co/t/sum-operator-in-stacked-bar-vega/263722>\
**Category:** Kibana\
**Created:** [February 9, 2021, 11:49am UTC](https://discuss.elastic.co/t/sum-operator-in-stacked-bar-vega/263722 "2021-02-09T11:49:08Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Pranusha119](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@Pranusha119](https://discuss.elastic.co/u/Pranusha119)\
**Post date:** [February 10, 2021, 4:28am UTC](https://discuss.elastic.co/t/sum-operator-in-stacked-bar-vega/263722/3 "2021-02-10T04:28:38Z")

</div>

Hi @wylie

1. The main visualization I require is % graph by dividing sum of StatusOpen/ sum of StatusSent, which was not giving expected output in vega-lite. So just wanted to check ouput for simple graph ( with sum of single field).

2. Tried the same spec using sample data, it was giving correct results

```auto
{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "Vega-Lite version of bar chart from https://observablehq.com/@d3/learn-d3-scales.",
  "width": 400,
  "data": {
    "values": [
      {"name": ":tangerine:", "count": 20, "gcount": "A", "hcount":21},
      {"name": ":tangerine:", "count": 21, "gcount": "A", "hcount": 8},
       {"name": ":tangerine:", "count": 8, "gcount": "B", "hcount": 8},
          {"name": ":tangerine:", "count": 11, "gcount": "B", "hcount": 8},
      {"name": ":banana:", "count": 10, "gcount": "C", "hcount": 5},
      {"name": ":grapes:", "count": 11, "gcount": "D", "hcount": 5},
      {"name": ":grapes:", "count": 10, "gcount": "E", "hcount": 20},
      {"name": ":watermelon:", "count": 11, "gcount": "F", "hcount": 21}
    ]
  },
  "transform": [
    
      {
        "aggregate": [{"op": "sum", "field": "count","as": "aud_count"}],
              "groupby": ["name","gcount"]
      },
        
    ],
  "encoding": {
    "x": {"field": "name", "type": "nominal", "title": null},
    "y": {"field": "aud_count", "type": "quantitative", "title": null},
    "color": {
        "field": "gcount",
        "type": "nominal",
        "title": "Sentiment",
        "scale":{"scheme": "paired"}
      }

  },
  "layer": [
  {
    "mark": "bar"
  }, {
    "mark": {
      "type": "text",
      "align": "left",
      "dx": -6,
      "dy": -5
    },
    "encoding": {
      "text": {"field": "aud_count", "type": "quantitative"}
    }
  }]
} 

```

Output :

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/8/581e4844017a4210dcddfcd0a465415471370b6f.png)

---

_[View the full topic](https://discuss.elastic.co/t/sum-operator-in-stacked-bar-vega/263722)._
