vega - 如何在 VegaLite 中指定具有单个值的规则线?

标签 vega vega-lite

可以指定规则作为对数据的编码。但有时它太冗长了。有没有更短的方法来指定一个数字?

在下面的示例中,我想用 y=1 绘制一条水平线,它需要我指定一个计算转换。想知道是否可以使用更紧凑的东西,例如:

"layer": [
  ...,
  { "mark": { "type": "rule", "y": 1 }, // Specify ruler with just a single line
  ...
]

Playground

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "transform":[
    { "calculate": "1", "as": "one" }
  ],
  "layer": [
    {
      "mark": { "type": "bar" },
      "encoding": {
        "x": { "field": "diff", "type": "quantitative" },
        "y": { "field": "diff", "type": "quantitative" }
      }
    },
    {
      "mark": { "type": "rule" },
      "encoding": {
        "y": { "field": "one", "type": "quantitative" }
      }
    }
  ],
  "data": {
    "values": [
      { "diff": 1 },
      { "diff": 2 },
      { "diff": 3 }
    ]
  }
}

最佳答案

您可以在编码中使用"value" 字段指定单个值;例如

{
  "mark": { "type": "rule" },
  "encoding": {
    "y": { "value": 133 }
  }
}

( Playground )

但请注意,此值是轴域中的值,而不是数据域中的值;即它代表图表左上角的像素。

如果要指定数据域中的值,则没有这种简写形式。您必须使用转换(就像您在问题中所做的那样)或为您的图层定义一个新的数据集。我认为最紧凑的方式是这样的:

{
  "mark": { "type": "rule" },
  "data": {"values": {"y": 1}},
  "encoding": {
    "y": { "field": "y" }
  }
}

关于vega - 如何在 VegaLite 中指定具有单个值的规则线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59741643/

相关文章:

vega - 在 Vega 中连接多个数据源

Vegalite 日线图摆动

javascript - 无法通过本地服务器运行Vega-Lite可视化示例

elasticsearch - 查看Vega + Kibana中不是 `doc_count`的所有内容

vega - 如何在 vega 中以编程方式选择项目?

prometheus - 在 Vega Lite 中使用二维数组数据代替表格

python - Altair 条件颜色散点图

python-3.x - Vega-Lite 的某些投影在 Altair 中不起作用

python - 如何使用 vega-lite 创建与此类似的条形图?

kibana - 在 Vega/Kibana 中仅显示一个数字