3

I am quite a newbie when it comes to vega and would appreciate any help. I am currently creating a risk matrix with the two dimensions "damage potential" and "risk exposure". The ultimate goal is to use in within PowerBI.

My current status can be seen here: https://vega.github.io/editor/#/gist/d534904674d14f005e7cb08811dc8b62/spec.json

Now I face two main challenges:

  • How is it possible to color the fields so that the matrix looks like this: risk matrix picture
  • How can I populate the fields where there is no number so far (in this case: Damage: medium; Exposure: medium) with a 0?

Any help is welcome. :)

Davide Bacci
  • 16,647
  • 3
  • 10
  • 36
Fabi Need
  • 57
  • 5

1 Answers1

2

Here you go.

Editor.

enter image description here

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "url": "https://gist.githubusercontent.com/Hatico90/ec1d58ef5fe5d91cf6438e66fcd40bf0/raw/c5737c70fd45807a8435f2a97fe17fbc03bddf2b/riskmatrix",
    "format": {"type": "json"}
  },
  "width": 500,
  "height": 500,
  "transform": [
    {
      "pivot": "Exposure",
      "groupby": ["Damage"],
      "value": "Index",
      "op": "count"
    },
    {"fold": ["high", "low", "medium"]}
  ],
  "encoding": {
    "y": {
      "field": "key",
      "type": "ordinal",
      "scale": {"domain": ["high", "medium", "low"]}
    },
    "x": {
      "field": "Damage",
      "type": "ordinal",
      "scale": {"domain": ["low", "medium", "high"]}
    }
  },
  "layer": [
    {
      "mark": "rect",
      "data": {
        "values": [
          {"x": "low", "y": "low", "t": "green"},
          {"x": "low", "y": "medium", "t": "green"},
          {"x": "low", "y": "high", "t": "yellow"},
          {"x": "medium", "y": "low", "t": "green"},
          {"x": "medium", "y": "medium", "t": "yellow"},
          {"x": "medium", "y": "high", "t": "red"},
          {"x": "high", "y": "low", "t": "yellow"},
          {"x": "high", "y": "medium", "t": "red"},
          {"x": "high", "y": "high", "t": "red"}
        ]
      },
      "encoding": {
        "color": {
          "type": "nominal",
          "field": "t",
          "scale": {"range": {"field": "t"}},
          "legend": null
        },
        "y": {
          "field": "y",
          "type": "ordinal",
          "scale": {"domain": ["high", "medium", "low"]}
        },
        "x": {
          "field": "x",
          "type": "ordinal",
          "scale": {"domain": ["low", "medium", "high"]}
        }
      }
    },
    {
      "mark": {"type": "text"},
      "encoding": {"text": {"field": "value", "type": "quantitative"}}
    }
  ],
  "config": {"axis": {"grid": true, "tickBand": "extent"}}
}
Davide Bacci
  • 16,647
  • 3
  • 10
  • 36
  • Thanks David, this works like a charm. Small remark: When using it in PowerBI the aggregate instead of the pivot function enables correct cross-filtering. – Fabi Need May 23 '22 at 19:01
  • 1
    Yes, that's a draw back of reshaping the data in Vega (to produce the zero). As an alternative, you could create a dimension table for damage and exposure and make sure you have a measure which always returns a value (including zero) and then cross filter will work again. – Davide Bacci May 23 '22 at 21:31
  • That is a greatidea. I am also facing another obstacle in PowerBI regarding this matrix. My goal is to highlight the digit (or the cell as a whole) when I click on it. I have prepared two different versions in PowerBI: https://github.com/Hatico90/test/blob/main/Matrix.pbix?raw=true . In one version (with the aggregation function) the highlighting does not work. Without the aggregation function it works, but of course I don't have the right numbers. Any idea how I can solve the problem? – Fabi Need May 24 '22 at 10:47
  • 1
    I suggest adding another question explaining precisely what the desired behaviour is. Myself or someone else can probably solve it. – Davide Bacci May 24 '22 at 11:05
  • Good idea, the new question can be found here: https://stackoverflow.com/questions/72363343/deneb-powerbi-risk-matrix-project – Fabi Need May 24 '22 at 12:58