Encoding 如何在Vega Lite中编码基于表的数据?

Encoding 如何在Vega Lite中编码基于表的数据?,encoding,data-visualization,vega,vega-lite,Encoding,Data Visualization,Vega,Vega Lite,首先,很难准确描述我所说的基于表格的数据是什么意思,因为在某种程度上,织女星的所有输入数据都是基于表格的,但这个例子应该说明: 大多数(如果不是所有的话)用于多折线图的Vega Lite都使用以下数据: "data": { "values": [ {"id": 0, "symbol": "A", "value": 4}, {"id": 1, "symbol": "A", "value": 2}, {"id": 0, "symbol": "B", "value": 3}

首先,很难准确描述我所说的基于表格的数据是什么意思,因为在某种程度上,织女星的所有输入数据都是基于表格的,但这个例子应该说明:

大多数(如果不是所有的话)用于多折线图的Vega Lite都使用以下数据:

"data": {
  "values": [
    {"id": 0, "symbol": "A", "value": 4},
    {"id": 1, "symbol": "A", "value": 2},
    {"id": 0, "symbol": "B", "value": 3},
    {"id": 1, "symbol": "B", "value": 8}
  ]
}
用这样的Ecode给A和B的线条上色很简单

"mark": "line",
"encoding": {
  "x": {"field": "id", "type": "quantitative"},
  "y": {"field": "value", "type": "quantitative"},
  "color": {"field": "symbol", "type": "nominal"}
}
"data": {
  "values": [
    {"id": 0, "A": 4, "B": 3},
    {"id": 1, "A": 2, "B": 8}
  ]
}
但是如果我想用这种基于表格的数据形式产生相同的结果,该怎么办

"mark": "line",
"encoding": {
  "x": {"field": "id", "type": "quantitative"},
  "y": {"field": "value", "type": "quantitative"},
  "color": {"field": "symbol", "type": "nominal"}
}
"data": {
  "values": [
    {"id": 0, "A": 4, "B": 3},
    {"id": 1, "A": 2, "B": 8}
  ]
}
一,。如何将基于表格的数据编码到一个彩色的多折线图中

一个基本的编码可以是为每个字段创建折线图,并将它们层叠在一起

但是有了这个,我不知道如何给线条涂上不同的颜色,或者如何创造一个传奇


二,。这种类型的输入数据是否与vega/vega lite的设计方式一致?

vega lite使用的数据通常称为长格式或面向列的数据。您询问的数据类型通常称为宽格式或面向行的数据。Altair的文档中简要讨论了这一点,Altair是vega lite的Python包装器:

在当前版本的Vega Lite v2.X中,您唯一的选择是使用外部工具将数据源修改为面向列。这将在Vega Lite的v3.0版本中发生变化,该版本添加了用于在图表规范中将面向行的数据转换为面向列的数据的

因此,在Vega Lite 3中,可以像这样使用折叠变换:


vega lite处理的数据通常称为长格式或面向列的数据。您询问的数据类型通常称为宽格式或面向行的数据。Altair的文档中简要讨论了这一点,Altair是vega lite的Python包装器:

在当前版本的Vega Lite v2.X中,您唯一的选择是使用外部工具将数据源修改为面向列。这将在Vega Lite的v3.0版本中发生变化,该版本添加了用于在图表规范中将面向行的数据转换为面向列的数据的

因此,在Vega Lite 3中,可以像这样使用折叠变换:


另一个有点乏味的解决方案是使用层并为n列创建n个层

{
  "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
  "data": {"url": "data/seattle-weather.csv", "format": {"type": "csv"}},
  "layer": [{
    "mark": {"type": "line", "color": "orange"},
    "encoding": {
      "x": {"timeUnit": "yearmonthdate", "field": "date", "type": "temporal"},
      "y": {"field": "temp_max", "type": "quantitative"}
    }
  }, {
    "mark": {"type": "line", "color": "red"},
    "encoding": {
      "x": {"timeUnit": "yearmonthdate", "field": "date", "type": "temporal"},
      "y": {"field": "temp_min", "type": "quantitative"}
    }
  }]
}

未来对层重复的支持可能会使这成为一个更合理的解决方案

另一个有点繁琐的解决方案是使用层并为n列创建n个层

{
  "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
  "data": {"url": "data/seattle-weather.csv", "format": {"type": "csv"}},
  "layer": [{
    "mark": {"type": "line", "color": "orange"},
    "encoding": {
      "x": {"timeUnit": "yearmonthdate", "field": "date", "type": "temporal"},
      "y": {"field": "temp_max", "type": "quantitative"}
    }
  }, {
    "mark": {"type": "line", "color": "red"},
    "encoding": {
      "x": {"timeUnit": "yearmonthdate", "field": "date", "type": "temporal"},
      "y": {"field": "temp_min", "type": "quantitative"}
    }
  }]
}

未来对层重复的支持可能会使这成为一个更合理的解决方案

多谢各位!对数据表的澄清也很有帮助。我脑子里有一张照片,但记不起正确的术语。非常感谢!对数据表的澄清也很有帮助。我脑子里有一张照片,但记不起正确的术语。