简体   繁体   English

使用 JSON 和 Vega-lite 在散点图上绘制多个点

[英]plotting multiple points on scatterplot using JSON and Vega-lite

I am trying to create a scatterplot using webservice API endpoints provided by my university and Vega-lite visualization library.我正在尝试使用我的大学和 Vega-lite 可视化库提供的 webservice API 端点创建散点图。 The goal is to have hour of day plotted on the x-axis and the count of instances on the y axis by using the following.目标是使用以下命令在 x 轴上绘制一天中的小时数,在 y 轴上绘制实例数。

    {
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "A scatterplot showing horsepower and miles per gallons for various cars.",
  "data": {"url": "https://zagster-service.herokuapp.com/rides/count/per_hour"},
  "mark": "point",
  "encoding": {
    "x": {"field": "0", "type": "quantitative"},
    "y": {"field": "1", "type": "quantitative"}
  }
}

I have tried to follow several examples found online and have only able to figure out how to plot one point at a time using the above code or manually input each point on the graph, however doing it manually is not an option for my purposes.我试图遵循在线找到的几个示例,并且只能弄清楚如何使用上述代码一次绘制一个点或手动输入图表上的每个点,但是手动执行不是我的目的的选择。 My JSON file looks is as follows where the hours of day are represented by 0-23 and count of each instance is right next to it.我的 JSON 文件如下所示,其中一天中的小时数由 0-23 表示,每个实例的计数就在它旁边。

{"0":429,"1":231,"2":130,"3":85,"4":42,"5":1,"7":1,"8":17,"9":16,"10":795,"11":425,"12":921,"13":846,"14":1795,"15":1789,"16":2119,"17":1630,"18":1942,"19":1637,"20":1636,"21":1054,"22":843,"23":710}

I have been trying to figure this out for a while and need some help going in the right direction我一直在努力解决这个问题,需要一些帮助才能朝着正确的方向前进

Data in vega-lite is expected to be specified as a list of records; vega-lite 中的数据应指定为记录列表; eg rather than例如而不是

{"0":429,"1":231,"2":130}

it should be它应该是

[{"x": "0", "y": 429}, {"x": "1", "y": 231}, {"x": "2", "y": 130}]

If your data source cannot be modified, it is possible to use the fold transform to reshape your data.如果您的数据源无法修改,则可以使用折叠变换来重塑您的数据。 It would look something like this ( view in vega editor ):它看起来像这样( 在 vega 编辑器中查看):

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "A scatterplot showing horsepower and miles per gallons for various cars.",
  "data": {"url": "https://zagster-service.herokuapp.com/rides/count/per_hour"},
  "transform": [
    {
      "fold": ["0", "1", "2", "3", "4", "5", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"],
      "as": ["x", "y"]
    }
  ],
  "mark": "point",
  "encoding": {
    "x": {"field": "x", "type": "quantitative"},
    "y": {"field": "y", "type": "quantitative"}
  }
}

在此处输入图片说明

Unfortunately, there's no way to fold data like this without explicitly listing all the entries to be folded.不幸的是,没有明确列出所有要折叠的条目,就无法像这样折叠数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM