向vega-lite散点图添加回归线

时间:2019-07-22 14:21:17

标签: vega vega-lite

我正在尝试在我的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, {})

我已经在此小提琴中包含了所有代码:https://jsfiddle.net/MayaGans/qdj20Lws/任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

Vega-Lite不具有任何内置功能来计算回归线。但是,如果您已预先计算了回归线,则可以使用分层图表将其放置在同一轴上。例如:

{
  "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"}
      }
    }
  ]
}

enter image description here