将回归线添加到 vega-lite 散点图

Adding regression line to vega-lite scatterplot

我正在尝试向我的 vega-lite 图中添加一条简单的回归线,但我不确定 where to integrate this code 有什么想法吗?

let spec = {
      "data": { "values": iris },
      "mark": "point",
      "encoding": {
        "x": {"field": "Sepal_Length","type": "quantitative"},
        "y": {"field": "Sepal_Width","type": "quantitative"}
      }
    }

vegaEmbed("#vis", spec, {})

我已将所有代码包含在此 fiddle 中:https://jsfiddle.net/MayaGans/qdj20Lws/ 感谢您的帮助!

Vega-Lite 没有任何 built-in 计算回归线的能力。但是如果你有回归线pre-computed,你可以使用分层图表将它们放在同一个轴上。例如:

{
  "layer": [
    {
      "data": {"url": "data/iris.json"},
      "mark": "point",
      "transform": [
        {"filter": "datum.species == 'setosa'"}
      ],
      "encoding": {
        "x": {"type": "quantitative", "field": "sepalWidth"},
        "y": {"type": "quantitative", "field": "sepalLength"}
      }
    },
    {
      "data": {
        "values": [
          {"x": 0, "y": 2},
          {"x": 5, "y": 6.5}
        ]
      },
      "mark": "line",
      "encoding": {
        "x": {"type": "quantitative", "field": "x"},
        "y": {"type": "quantitative", "field": "y"}
      }
    }
  ]
}