首页 > 解决方案 > 如何在 vega-lite 的折线图中画一条线?

问题描述

我有一条从索引绘制的曲线

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.4.json",
    "title": {
    "text": "Receiver Operating Characteristics - Area Under Curve",
    "anchor": "middle",
    "fontSize": 16,
    "frame": "group",
    "offset": 4
  },
  "data": {
    "url" : {
        "%context%": true,
        "index": "roccurve_index2",
        "body": {
          "size":10000,
          "_source": ["lr_fpr", "lr_tpr"],
        }
      }  
      "format": {"property": "hits.hits"},
    },
  "mark": {
    "type": "line",
    "point": true
  },
  "encoding": {
    "x": {"field": "_source.lr_fpr", "type": "quantitative", "title":"False Positive Rate"},
    "y": {"field": "_source.lr_tpr", "type": "quantitative", "title":"True Positive Rate"}
  }
}

情节看起来像 在此处输入图像描述

现在我需要为 0 和 1 之间的基本模型绘制一条基线,例如 在此处输入图像描述

这可能吗,并将其设为带有图例的虚线,显示名称为 Base Model、RF Model

标签: vega-litevega

解决方案


是的,可以使用分层视图

我将使用折线图示例来修改和添加另一条虚线。原图: https ://vega.github.io/editor/#/examples/vega-lite/line

这是修改后的图表,我对直线使用了明确的值:

在此处输入图像描述

https://vega.github.io/editor/#/gist/152fbe5f986ba78e422bb3430628f010/spec.json

层解决方案

当您使用分层视图时,您可以在同一个图表和相同的 x 和 y 轴中放置多条线

  "layer" : [
      {
         //mark #1
      },
      {
        //mark #2
      }
  ]

虚线

可以使用strokeDash属性来实现。请参阅此示例: 具有不同笔划破折号的折线图


推荐阅读