# Vega heatmap chart for matrix stats (correlation)

**URL:** <https://discuss.elastic.co/t/vega-heatmap-chart-for-matrix-stats-correlation/226099>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [April 1, 2020, 5:32pm UTC](https://discuss.elastic.co/t/vega-heatmap-chart-for-matrix-stats-correlation/226099 "2020-04-01T17:32:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vincent\_Maury](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vincent_maury/32/59973_2.png) [@Vincent\_Maury](https://discuss.elastic.co/u/Vincent_Maury)\
**Post date:** [April 1, 2020, 5:32pm UTC](https://discuss.elastic.co/t/vega-heatmap-chart-for-matrix-stats-correlation/226099/1 "2020-04-01T17:32:43Z")

</div>

Hi  
I struggled to display a heatmap showing the correlation of fields (calling the matrix\_stats agg from Elasticsearch).  
Many many thanks to my colleague Yuri that helped me on this 👍  
Here is the code that works:

```
{
  "$schema": "https://vega.github.io/schema/vega/v4.3.json",
  "title": "A heatmap showing the correlation of features",
  "data": [
    {
      "name": "feature_correlation",
      "url": {
        %context%: true
        index: fraud*
        body: {
          size: 0
          aggs: {
              statistics: {
                  matrix_stats: {
                      fields: ["amount", "oldBalanceOrig", "newBalanceOrig", "oldBalanceDest", "newBalanceDest", "errorBalanceOrig", "errorBalanceDest"]
                  }
              }
          }
        }
      }
      format: {property: "aggregations.statistics.fields"}
      "transform": [
        {"type": "formula", "expr": "datum.correlation.amount", "as": "amount"}
		{"type": "formula", "expr": "datum.correlation.oldBalanceOrig", "as": "oldBalanceOrig"}
		{"type": "formula", "expr": "datum.correlation.newBalanceOrig", "as": "newBalanceOrig"}
		{"type": "formula", "expr": "datum.correlation.oldBalanceDest", "as": "oldBalanceDest"}
		{"type": "formula", "expr": "datum.correlation.newBalanceDest", "as": "newBalanceDest"}
		{"type": "formula", "expr": "datum.correlation.errorBalanceOrig", "as": "errorBalanceOrig"}
		{"type": "formula", "expr": "datum.correlation.errorBalanceDest", "as": "errorBalanceDest"}
        {"type": "fold", "fields": ["amount", "oldBalanceOrig", "newBalanceOrig", "oldBalanceDest", "newBalanceDest", "errorBalanceOrig", "errorBalanceDest"]}
        {"type": "project", "fields": ["name", "key", "value"]}

	  ]
	}
  ],

  "scales": [
    {
      "name": "x",
      "type": "band",
      "domain": {"data": "feature_correlation", "field": "name"},
      "range": "width"
    },
    {
      "name": "y",
      "type": "band",
      "domain": {"data": "feature_correlation", "field": "key"},
      "range": "height"
    },
    {
      "name": "color",
      "type": "linear",
      "range": {"scheme": "yelloworangebrown"},
      "domain": {"data": "feature_correlation", "field": "value"},
      "zero": false, "nice": true
    }
  ],

  "axes": [
    {"orient": "bottom", "scale": "x", "domain": false},
    {"orient": "left", "scale": "y", "domain": false}
  ],

  "legends": [
    {
      "fill": "color",
      "type": "gradient",
      "title": "Correlation",
      "titleFontSize": 12,
      "titlePadding": 4,
      "gradientLength": {"signal": "height - 16"}
    }
  ],

  "marks": [
    {
      "type": "rect",
      "from": {"data": "feature_correlation"},
      "encode": {
        "enter": {
          "x": {"scale": "x", "field": "name"},
          "y": {"scale": "y", "field": "key"},
          "width": {"scale": "x", "band": 1},
          "height": {"scale": "y", "band": 1},
		  "tooltip": {"signal": "datum.name + ' - ' + datum.key + ': ' + format(datum.value, '.3f')"}
        },
        "update": {
          "fill": {"scale": "color", "field": "value"}
        }
      }
    }
  ]
}

```

and the result:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/9/0/90f226136eb6e25ebec99511142644131db0ba38.png)

Happy Vega!

---

<div class="post-metadata">

**Author:** ![tshayan](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@tshayan](https://discuss.elastic.co/u/tshayan)\
**Post date:** [April 1, 2020, 6:08pm UTC](https://discuss.elastic.co/t/vega-heatmap-chart-for-matrix-stats-correlation/226099/2 "2020-04-01T18:08:20Z")

</div>

This is great. Thanks for sharing!

---

<div class="post-metadata">

**Author:** ![Vincent\_Maury](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vincent_maury/32/59973_2.png) [@Vincent\_Maury](https://discuss.elastic.co/u/Vincent_Maury)\
**Post date:** [April 2, 2020, 3:16pm UTC](https://discuss.elastic.co/t/vega-heatmap-chart-for-matrix-stats-correlation/226099/3 "2020-04-02T15:16:11Z")

</div>

and enhanced by a split !

> [@Vega chart for matrix stats (correlation) split!](https://discuss.elastic.co/t/vega-chart-for-matrix-stats-correlation-split/226238):
>
> Hey following [Vega heatmap chart for matrix stats (correlation)](https://discuss.elastic.co/t/vega-heatmap-chart-for-matrix-stats-correlation/226099) Here is a vega displaying a heatmap of correlation matrix (matrix stats Elasticsearch api call) split by a key (isFraud in this example). A bit long, but it's worth it! { "$schema": "https://vega.github.io/schema/vega/v4.3.json", "title": "A heatmap showing the correlation of features", "background": "white", "padding": 0, "data": [ { "name": "feature\_correlation", "url": { "%context%": true, …

---

<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:** [April 30, 2020, 3:16pm UTC](https://discuss.elastic.co/t/vega-heatmap-chart-for-matrix-stats-correlation/226099/4 "2020-04-30T15:16:14Z")

</div>

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