# Argmax in case of ties - Vegalite chart

**URL:** <https://discuss.elastic.co/t/argmax-in-case-of-ties-vegalite-chart/321515>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [December 19, 2022, 5:53am UTC](https://discuss.elastic.co/t/argmax-in-case-of-ties-vegalite-chart/321515 "2022-12-19T05:53:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Siva\_Kumar\_Menta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/siva_kumar_menta/32/104159_2.png) [@Siva\_Kumar\_Menta](https://discuss.elastic.co/u/Siva_Kumar_Menta)\
**Post date:** [December 19, 2022, 5:53am UTC](https://discuss.elastic.co/t/argmax-in-case-of-ties-vegalite-chart/321515/1 "2022-12-19T05:53:22Z")

</div>

Hi All,

I have Elasticsearch index whose structure is as shown below.

My requirement is I want to create chart with highest duration (ms) corresponding to highest frequency for each SNI# by using argmax function in Vegalite script . As my data contains ties for highest frequency for each SNI#, not getting highest duration (ms) for highest frequency in each SNI#.

Ex: for SNI# (0x03 ESC (FR-1B)) , highest frequency is 6 ; maximum duration with h  
highest frequency is 96 but getting 93.

Your suggestions please to achieve my objective.

Script:

```auto
{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "autosize": {"type": "fit", "contains": "padding"},
  "data": {
    "url": {
      "index": "index01",
      "%context%": true,
      "body": {"size": 1000}
    },
    "format": {"property": "hits.hits"}
  }, 
 "transform": [{
    "sort": [
      {"field": "ECU_SNI", "order": "descending"},
      {"field": "Frequency", "order": "descending"},
      {"field": "ms", "order": "descending"}
    ],
    "window": [{
      "op": "rank",
      "as": "rank"
    }],
    "groupby": ["ECU_SNI"]
  },
  {"filter": "datum.rank == 1"}],
   "encoding": {
    "x": {
      "field": "_source.ECU_SNI","title": "ECU_SNI"
      "scale": {"padding": 0},
      "axis": {"labelAngle": -25, "labelLimit": 1000, "title": "ECU-SNI"}
    }
  },
  "layer": [
    {
      "mark": {
        "color": "#85A9C5",
        "type": "bar",
        "tooltip": true,
        "point": false
      },
      "encoding": {
        "y": {
          "aggregate": {"argmax": "_source.Frequency"},
          "field": "_source.ms",
          "type": "quantitative",
          "title": "[ms]",
          "axis": {"titleColor": "#85C5A6"}
        },
        "y2": {"aggregate": "max", "field": "_source.Frequency","title": "Frequency"}
      }
    },
    {
      "mark": {
        "opacity": 10,
        "type": "line",
        "width": 4,
        "color": "#85C5A6",
        "tooltip": true
      },
      "encoding": {
        "y": {
          "aggregate": "max",
          "field": "_source.Frequency",
          "title": "Frequency",
          "axis": {"titleColor": "#85A9C5"}
        }
      }
    }
  ],
  "resolve": {"scale": {"y": "independent"}},
  "config": {}
}

```

Index data:

```auto
{"index": {}}
{"SNI#": "0x03 ESC (FR-1B)", "ms": 93, "Frequency": 2, "avgFreq": 95, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x03 ESC (FR-1B)", "chart3_flag": 0, "chart2_flag": 0.0}
{"index": {}}
{"SNI#": "0x03 ESC (FR-1B)", "ms": 95, "Frequency": 1, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x03 ESC (FR-1B)", "chart3_flag": 0, "chart2_flag": 0.0}
{"index": {}}
{"SNI#": "0x03 ESC (FR-1B)", "ms": 96, "Frequency": 2, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x03 ESC (FR-1B)", "chart3_flag": 1, "chart2_flag": 1.0}
{"index": {}}
{"SNI#": "0x15 Airbag_6D (FR-1B)", "ms": 0, "Frequency": 6, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x15 Airbag_6D (FR-1B)", "chart3_flag": 0, "chart2_flag": 1.0}
{"index": {}}
{"SNI#": "0x15 Airbag_6D (FR-1B)", "ms": 1, "Frequency": 6, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x15 Airbag_6D (FR-1B)", "chart3_flag": 0, "chart2_flag": 0.0}
{"index": {}}
{"SNI#": "0x15 Airbag_6D (FR-1B)", "ms": 2, "Frequency": 2, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x15 Airbag_6D (FR-1B)", "chart3_flag": 0, "chart2_flag": 0.0}

```

Thanks & Regards,  
Siva Kumar

---

<div class="post-metadata">

**Author:** ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)\
**Post date:** [December 19, 2022, 9:14am UTC](https://discuss.elastic.co/t/argmax-in-case-of-ties-vegalite-chart/321515/2 "2022-12-19T09:14:55Z")

</div>

Hi @Siva_Kumar_Menta

I've tried your Vega spec with the given data and to me it looks correct: (note I've just used static data, so `_source.` has been omitted):

```auto
{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "autosize": {"type": "fit", "contains": "padding"},
  "data": {
    "values": [
{"SNI#": "0x03 ESC (FR-1B)", "ms": 93, "Frequency": 2, "avgFreq": 95, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x03 ESC (FR-1B)", "chart3_flag": 0, "chart2_flag": 0.0}
{"SNI#": "0x03 ESC (FR-1B)", "ms": 95, "Frequency": 1, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x03 ESC (FR-1B)", "chart3_flag": 0, "chart2_flag": 0.0}
{"SNI#": "0x03 ESC (FR-1B)", "ms": 96, "Frequency": 2, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x03 ESC (FR-1B)", "chart3_flag": 1, "chart2_flag": 1.0}
{"SNI#": "0x15 Airbag_6D (FR-1B)", "ms": 0, "Frequency": 6, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x15 Airbag_6D (FR-1B)", "chart3_flag": 0, "chart2_flag": 1.0}
{"SNI#": "0x15 Airbag_6D (FR-1B)", "ms": 1, "Frequency": 6, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x15 Airbag_6D (FR-1B)", "chart3_flag": 0, "chart2_flag": 0.0}
{"SNI#": "0x15 Airbag_6D (FR-1B)", "ms": 2, "Frequency": 2, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x15 Airbag_6D (FR-1B)", "chart3_flag": 0, "chart2_flag": 0.0}
    ]
  }, 
 "transform": [{
    "sort": [
      {"field": "ECU_SNI", "order": "descending"},
      {"field": "Frequency", "order": "descending"},
      {"field": "ms", "order": "descending"}
    ],
    "window": [{
      "op": "rank",
      "as": "rank"
    }],
    "groupby": ["ECU_SNI"]
  },
  {"filter": "datum.rank == 1"}
  ],
   "encoding": {
    "x": {
      "field": "ECU_SNI","title": "ECU_SNI"
      "scale": {"padding": 0},
      "axis": {"labelAngle": -25, "labelLimit": 1000, "title": "ECU-SNI"}
    }
  },
  "layer": [
    {
      "mark": {
        "color": "#85A9C5",
        "type": "bar",
        "tooltip": true,
        "point": false
      },
      "encoding": {
        "y": {
          "aggregate": {"argmax": "Frequency"},
          "field": "ms",
          "type": "quantitative",
          "title": "[ms]",
          "axis": {"titleColor": "#85C5A6"}
        },
        "y2": {"aggregate": "max", "field": "Frequency","title": "Frequency"}
      }
    },
    {
      "mark": {
        "opacity": 10,
        "type": "line",
        "width": 4,
        "color": "#85C5A6",
        "tooltip": true
      },
      "encoding": {
        "y": {
          "aggregate": "max",
          "field": "Frequency",
          "title": "Frequency",
          "axis": {"titleColor": "#85A9C5"}
        }
      }
    }
  ],
  "resolve": {"scale": {"y": "independent"}},
  "config": {}
}

```

For the `"0x03 ESC (FR-1B)"` a high duration of 96 is associated with the max Frequency of 2.  
For the `"0x15 Airbag_6D (FR-1B)"` a high duration of 2 is associated with the max Frequency of 6.

 ![Screenshot 2022-12-19 at 10.12.01](https://us1.discourse-cdn.com/elastic/original/3X/1/b/1bbfaa6116b9cf28592b49a5c1e6f3658b6a5315.png)

Did I understand correctly what you are looking for?

---

<div class="post-metadata">

**Author:** ![Siva\_Kumar\_Menta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/siva_kumar_menta/32/104159_2.png) [@Siva\_Kumar\_Menta](https://discuss.elastic.co/u/Siva_Kumar_Menta)\
**Post date:** [December 19, 2022, 9:42am UTC](https://discuss.elastic.co/t/argmax-in-case-of-ties-vegalite-chart/321515/3 "2022-12-19T09:42:39Z")

</div>

your understanding is correct.  
But for SNI "0x15 Airbag\_6D (FR-1B)", result is not coming as expected (Maximum Frequency 6 and corresponding highest 'ms' supposed to be 1 but coming as 0].  
Any suggestions please.

---

<div class="post-metadata">

**Author:** ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)\
**Post date:** [December 19, 2022, 10:31am UTC](https://discuss.elastic.co/t/argmax-in-case-of-ties-vegalite-chart/321515/4 "2022-12-19T10:31:09Z")

</div>

It returns 1 in the chart above:

 ![Screenshot 2022-12-19 at 11.30.37](https://us1.discourse-cdn.com/elastic/original/3X/e/0/e00651c2067ec3b1e130293b32c9b07e69293659.png)

---

<div class="post-metadata">

**Author:** ![Siva\_Kumar\_Menta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/siva_kumar_menta/32/104159_2.png) [@Siva\_Kumar\_Menta](https://discuss.elastic.co/u/Siva_Kumar_Menta)\
**Post date:** [December 20, 2022, 6:08am UTC](https://discuss.elastic.co/t/argmax-in-case-of-ties-vegalite-chart/321515/5 "2022-12-20T06:08:50Z")

</div>

> [@Marco\_Liberati](#):
>
> ```auto
> {
> "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
> "autosize": {"type": "fit", "contains": "padding"},
> "data": {
> "values": [
> {"SNI#": "0x03 ESC (FR-1B)", "ms": 93, "Frequency": 2, "avgFreq": 95, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x03 ESC (FR-1B)", "chart3_flag": 0, "chart2_flag": 0.0}
> {"SNI#": "0x03 ESC (FR-1B)", "ms": 95, "Frequency": 1, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x03 ESC (FR-1B)", "chart3_flag": 0, "chart2_flag": 0.0}
> {"SNI#": "0x03 ESC (FR-1B)", "ms": 96, "Frequency": 2, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x03 ESC (FR-1B)", "chart3_flag": 1, "chart2_flag": 1.0}
> {"SNI#": "0x15 Airbag_6D (FR-1B)", "ms": 0, "Frequency": 6, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x15 Airbag_6D (FR-1B)", "chart3_flag": 0, "chart2_flag": 1.0}
> {"SNI#": "0x15 Airbag_6D (FR-1B)", "ms": 1, "Frequency": 6, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x15 Airbag_6D (FR-1B)", "chart3_flag": 0, "chart2_flag": 0.0}
> {"SNI#": "0x15 Airbag_6D (FR-1B)", "ms": 2, "Frequency": 2, "avgFreq": 0, "vehicle": "TE1484421", "filedate": "2022-10-18", "communication_type": "FR1B", "threshold": 200, "ECU_SNI": "FR1B - 0x15 Airbag_6D (FR-1B)", "chart3_flag": 0, "chart2_flag": 0.0}
> ]
> }, 
> "transform": [{
> "sort": [
> {"field": "ECU_SNI", "order": "descending"},
> {"field": "Frequency", "order": "descending"},
> {"field": "ms", "order": "descending"}
> ],
> "window": [{
> "op": "rank",
> "as": "rank"
> }],
> "groupby": ["ECU_SNI"]
> },
> {"filter": "datum.rank == 1"}
> ],
> "encoding": {
> "x": {
> "field": "ECU_SNI","title": "ECU_SNI"
> "scale": {"padding": 0},
> "axis": {"labelAngle": -25, "labelLimit": 1000, "title": "ECU-SNI"}
> }
> },
> "layer": [
> {
> "mark": {
> "color": "#85A9C5",
> "type": "bar",
> "tooltip": true,
> "point": false
> },
> "encoding": {
> "y": {
> "aggregate": {"argmax": "Frequency"},
> "field": "ms",
> "type": "quantitative",
> "title": "[ms]",
> "axis": {"titleColor": "#85C5A6"}
> },
> "y2": {"aggregate": "max", "field": "Frequency","title": "Frequency"}
> }
> },
> {
> "mark": {
> "opacity": 10,
> "type": "line",
> "width": 4,
> "color": "#85C5A6",
> "tooltip": true
> },
> "encoding": {
> "y": {
> "aggregate": "max",
> "field": "Frequency",
> "title": "Frequency",
> "axis": {"titleColor": "#85A9C5"}
> }
> }
> }
> ],
> "resolve": {"scale": {"y": "independent"}},
> "config": {}
> }
> 
> ```

Now, I'm also getting same result. Will validate with more data and let you know any issues.

Thanks,  
Siva Kumar

---

<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:** [January 17, 2023, 6:09am UTC](https://discuss.elastic.co/t/argmax-in-case-of-ties-vegalite-chart/321515/6 "2023-01-17T06:09:11Z")

</div>

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