Vega

Pivot: del formato largo al formato ancho

El inverso de fold: pivot crea una columna por categoría, lo que permite calcular un ratio entre dos series que estaban inicialmente apiladas en filas.

Requisitos

Vega-Lite v5

Vega-Lite
{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      { "date": "2026-06-01", "statut": "ok", "n": 9200 },
      { "date": "2026-06-01", "statut": "error", "n": 110 },
      { "date": "2026-06-02", "statut": "ok", "n": 9550 },
      { "date": "2026-06-02", "statut": "error", "n": 95 },
      { "date": "2026-06-03", "statut": "ok", "n": 8900 },
      { "date": "2026-06-03", "statut": "error", "n": 310 },
      { "date": "2026-06-04", "statut": "ok", "n": 9100 },
      { "date": "2026-06-04", "statut": "error", "n": 520 },
      { "date": "2026-06-05", "statut": "ok", "n": 9700 },
      { "date": "2026-06-05", "statut": "error", "n": 180 },
      { "date": "2026-06-06", "statut": "ok", "n": 7200 },
      { "date": "2026-06-06", "statut": "error", "n": 85 },
      { "date": "2026-06-07", "statut": "ok", "n": 7050 },
      { "date": "2026-06-07", "statut": "error", "n": 70 },
      { "date": "2026-06-08", "statut": "ok", "n": 9900 },
      { "date": "2026-06-08", "statut": "error", "n": 130 }
    ]
  },
  "transform": [
    { "pivot": "statut", "value": "n", "groupby": ["date"] },
    { "calculate": "datum.error / (datum.ok + datum.error)", "as": "taux_erreur" }
  ],
  "mark": { "type": "line", "color": "#ff5f56", "point": true },
  "encoding": {
    "x": { "field": "date", "type": "temporal", "title": null },
    "y": {
      "field": "taux_erreur", "type": "quantitative",
      "axis": { "format": ".1%" }, "title": "Taux d'erreur"
    }
  }
}
Vega-LitepivotRatioReshape

Snippets relacionados

Volver al Data Lab