如何使用Vega-Lite在轴上绘制几个变量?

时间:2017-07-18 07:15:50

标签: vega vega-lite

按照Vega-Lite的西雅图天气教程,很容易按月绘制平均温度:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "data": {
    "url": "https://vega.github.io/vega-lite/data/seattle-weather.csv"
  },
  "mark": "line",
  "encoding": {
    "x": {
      "timeUnit": "month",
      "field": "date",
      "type": "temporal"
    },
    "y": {
      "aggregate": "mean",
      "field": "temp_min",
      "type": "quantitative"
    }
  }
}

此数据集还包含temp_max变量。如何在y轴上绘制temp_mintemp_max

1 个答案:

答案 0 :(得分:4)

您可以按https://vega.github.io/vega-lite/docs/layer.html中所述使用分层。

{
  "data": {"url": "data/seattle-weather.csv"},
  "layer": [
    {
      "mark": "line",
      "encoding": {
        "x": {
          "timeUnit": "month",
          "field": "date",
          "type": "temporal"
        },
        "y": {
          "aggregate": "mean",
          "field": "temp_min",
          "type": "quantitative"
        }
      }
    },
    {
      "mark": "line",
      "encoding": {
        "x": {
          "timeUnit": "month",
          "field": "date",
          "type": "temporal"
        },
        "y": {
          "aggregate": "mean",
          "field": "temp_max",
          "type": "quantitative"
        }
      }
    }
  ]
}

layered chart