Vega-Lite 上的导数变换

Derivative transform on Vega-Lite

我有一个数据集,我需要绘制它的导数函数。有没有办法在 Vega-Lite 中执行一系列的导数?也许是一个转换函数或者也许是计算函数?有没有办法手动完成,X(t) - X(t-1)?

您可以使用 window transform to determine the adjacent value, followed by a calculate transform 构建您想要的运算来计算值之间的差异。

这是一个例子 (vega editor link):

{
  "data": {
    "values": [
      {"x": 0, "y": 0},
      {"x": 1, "y": 0.8},
      {"x": 2, "y": 0.9},
      {"x": 3, "y": 0.1},
      {"x": 4, "y": -0.8},
      {"x": 5, "y": -1},
      {"x": 6, "y": -0.3},
      {"x": 7, "y": 0.7},
      {"x": 8, "y": 1},
      {"x": 9, "y": 0.4},
      {"x": 10, "y": -0.5}
    ]
  },
  "transform": [
    {
      "window": [{"op": "last_value", "field": "y", "as": "y1"}],
      "frame": [0, 1],
      "sort": [{"field": "x", "order": "ascending"}]
    },
    {"calculate": "datum.y1 - datum.y", "as": "dy"}
  ],
  "layer": [
    {
      "mark": "line",
      "encoding": {
        "x": {"type": "quantitative", "field": "x"},
        "y": {"type": "quantitative", "field": "y"}
      }
    },
    {
      "mark": "line",
      "encoding": {
        "color": {"value": "red"},
        "x": {"type": "quantitative", "field": "x"},
        "y": {"type": "quantitative", "field": "dy"}
      }
    }
  ],
  "config": {"view": {"width": 400, "height": 300}}
}