首页 > 解决方案 > 轻推/抖动:用分类 X 轴和 3 组图例绘制点

问题描述

我有一个看起来像这样的 vegalite 情节。有 3 个操作系统(参见图例),我正在为每个版本绘制一个评分器。我想在 y 轴上为给定版本“微调”/抖动 3 个操作系统的 x 值,以便它们不在一条线上。在 R 的格中,这将是“抖动”。有没有办法修改 vegalite 规格来做到这一点?我正在使用 R 的 vegawidget 并自己创建规范。

非常感谢

标签: categorical-datavega-litejitter

解决方案


Jitter or offset encodings are not yet implemented in Vega-Lite; see https://github.com/vega/vega-lite/issues/4703 for the relevant feature request.

In the meantime, the best way to approximate what you want is to use a column encoding, along with an x encoding built from a randomly-generated jitter. The transform and encoding might look something like this:

  "transform": [{"calculate": "random()", "as": "jitter"}],
  "encoding": {
    "size": {"value": 65},
    "column": {"field": "cv", "type": "ordinal", "spacing": 0},
    "x": {
      "field": "jitter",
      "type": "quantitative",
      "axis": {"title": null, "labels": false},
      "scale": {"domain": [-1, 2]}
    },
    "y": {"field": "c", "type": "quantitative"},
    "color": {"field": "os", "type": "nominal"}
  }

A simplified example using your data can be seen here: enter image description here

From there you can customize grids, ticks, labels, and the rest to get it to look how you would like. It's imperfect, but it's the only way to currently get this kind of behavior within the Vega-Lite grammar.


推荐阅读