将数据分成几行的最佳方法?

时间:2019-10-27 14:07:10

标签: vega vega-lite

我有如下所示的数据: 2021 43466.822中型变体

2021 43510.982高变体

2021 43416.407低变体

2021 43468.429恒定生育力

2021 43580.45即时更换

并需要获取图表: https://image.prntscr.com/image/eBKqmOUsSa_6PBlomh5Erg.png

我尝试了transform fold选项,但是它没有帮助。并且为此做很多层-将包含很多代码。有什么聪明的方法吗?另外,我将需要一个如图所示的图例。

    vegalite({
      height:300,
      autosize: "fit",
      width:width,
      title: {text:"Ukraine Population Prospects",
              subtitle:"Total population, million"
             },
      data: {
              url:"https://gist.githubusercontent.com/turiy/005f2ce11637fefcde8e9d6efdb0c2e6/raw/19e67bb3a6d63e7fd9f49a596e5d24404469bd63/population_prospects.csv"},
      transform: [{"calculate": "datum.population/1000", "as": "population"},{fold:["medium variant","high variant", "low variant", "constant fertility","instant replacement", "momentum", "zero migration", "constant mortality", "no change"]}],
      layer: [
        { mark: "line",
          encoding:{      
            "x": {
              "timeUnit": "utcyear",
              "field": "year",
              "type": "temporal",
              "axis": {
                "values":[1950,1991,2020,2100],
                "domain": false,
                "gridDash": {"value": [1,1]}
              }
            },
            "y": {
              "field": "population",
              "type": "quantitative",
              "scale": {"domain": [15,55]},
              "axis": {
                "domain": false ,
                "gridDash": {"value": [1,1]}
              }
            }
          },
          color: {"value":"#0000ff"},
          transform:[{filter:{"timeUnit": "utcyear", "field": "year", "range": [1950, 2020]}}]
        },
        {
          mark: "line",legend:{title:"low variant"},
          encoding:{      
            x: {
              "timeUnit": "utcyear",
              "field": "year",
              "type": "temporal",
              "axis": {
                "values":[1950,1991,2020,2100],
                "domain": false,
                "gridDash": {"value": [1,1]}}
            },
            y: {
              "field": "population",
              "type": "quantitative",
              "scale": {"domain": [15,55]},
              "axis": {
                "domain": false ,
                "gridDash": {"value": [1,1]}
              }
            },
            legends:{
              "orient": "top-right",
              "stroke": "color",
              "title": "Origin",
              "encode": {
             "symbols": {
              "update": {
                "fill": {"value": ""},
                "strokeWidth": {"value": 2},
                "size": {"value": 64}
              }
            }
          }
            },
            color: {"field": "key", "type":"nominal"}

          },    
          transform:[{filter:{"timeUnit": "year", "field": "year", "range": [2020, 2100]}},
                     {filter:{field:"type", "equal":"low variant"}}]
         }


      ]})

我变得像这样https://image.prntscr.com/image/3Y9WNk4SQzGYWDr2JKWV9A.png

1 个答案:

答案 0 :(得分:0)

如果您的变体在示例数据集中按名称列在列中,则可以使用详细编码将其分成不同的行(vega editor link):

{
  "data": {
    "url": "https://gist.githubusercontent.com/turiy/005f2ce11637fefcde8e9d6efdb0c2e6/raw/19e67bb3a6d63e7fd9f49a596e5d24404469bd63/population_prospects.csv"
  },
  "mark": "line",
  "encoding": {
    "detail": {"type": "nominal", "field": "type"},
    "x": {"type": "quantitative", "field": "year"},
    "y": {"type": "quantitative", "field": "population"}
  }
}

enter image description here

如果您使用color而不是detail,则每行将使用不同的颜色,并包含图例。

要在图表的右侧添加标签,可以使用带有聚合转换的文本标记。像这样的东西:

{
  "data": {
    "url": "https://gist.githubusercontent.com/turiy/005f2ce11637fefcde8e9d6efdb0c2e6/raw/19e67bb3a6d63e7fd9f49a596e5d24404469bd63/population_prospects.csv"
  },
  "layer": [
    {
      "mark": "line",
      "encoding": {
        "detail": {"type": "nominal", "field": "type"},
        "x": {"type": "quantitative", "field": "year"},
        "y": {"type": "quantitative", "field": "population"}
      }
    },
    {
      "transform": [
        {"filter": "datum.type != 'estimate'"},
        {
          "aggregate": [{"op": "argmax", "field": "year", "as": "rightmost"}],
          "groupby": ["type"]
        }
      ],
      "mark": {"type": "text", "align": "left"},
      "encoding": {
        "text": {"type": "nominal", "field": "rightmost.type"},
        "x": {"type": "quantitative", "field": "rightmost.year"},
        "y": {"type": "quantitative", "field": "rightmost.population"}
      }
    }
  ],
  "width": 400
}

enter image description here