Vega-Lite上的导数变换

时间:2019-02-10 00:39:29

标签: transform vega vega-lite

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

1 个答案:

答案 0 :(得分:1)

您可以使用window transform来确定相邻的值,然后使用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}}
}

enter image description here