在 Vega-Lite 中创建自定义条形图

smpa01

我是新手Vega-Lite,我正在尝试复制与此类似的图表

所需图表

以下是我的代码,它不会生成我需要的内容。

{
   "$schema":"https://vega.github.io/schema/vega-lite/v5.json",
   "width":800,
   "height":600,
   "autosize":{
      "type":"fit",
      "contains":"padding"
   },
   "data":{
      "url":"data/cars.json"
   },
   "mark":{
      "type":"rect",
      "tooltip":true,
      "strokeWidth":0.1,
      "stroke":"white"
   },
   "encoding":{
      "x":{
         "field":"Horsepower",
         "type":"quantitative",
         "bin":{
            "maxbins":100
         },
         "axis":{
            "labelAngle":0
         }
      },
      "y":{
         "aggregate":"count",
         "field":"Horsepower",
         "type":"quantitative"
      }
   }
}

这是它的作用

源代码

我需要做什么才能获得我想要的输出?这里已经有人问过一个关于如何在 Altair 中重现 Unsub 直方图的问题?但它是在 中完成的python altair,我不想这样做。我想单独使用Vega-Lite.

先感谢您。

杰克夫

您可以使用bin 转换窗口转换来生成创建此类图表所需的字段。例如(在编辑器中打开):

{
  "data": {"url": "data/cars.json"},
  "transform": [
    {"bin": {"maxbins": 50}, "field": "Horsepower", "as": "Horsepower"},
    {"window": [{"op": "count", "as": "index"}], "groupby": ["Horsepower"]}
  ],
  "mark": {
    "type": "rect",
    "tooltip": true,
    "strokeWidth": 0.3,
    "stroke": "white"
  },
  "encoding": {
    "x": {"field": "Horsepower", "type": "quantitative", "bin": "binned"},
    "x2": {"field": "Horsepower_end"},
    "y": {
      "field": "index",
      "type": "ordinal",
      "scale": {"reverse": true},
      "title": "count"
    }
  },
  "width": 400,
  "height": 300
}

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章