如何将文本标记添加到 Vega Lite 分组条形图

How to add text mark to a Vega Lite grouped bar chart

我无法将文本标记层添加到分组条形图,我尝试编辑 Vega Lite example 以在每个条形图的顶部添加总和标签:

https://vega.github.io/editor/#/examples/vega-lite/bar_grouped

但结果是 "layer" 属性 未被接受,或者柱状图被压成一个柱状图。如何做到这一点?

当您想向多面图表添加文本标记时,要使用的模式是 Layered view within a Facet Operator

例如(vega editor):

{
  "data": {"url": "data/population.json"},
  "transform": [
    {"filter": "datum.year == 2000"},
    {"calculate": "datum.sex == 2 ? 'Female' : 'Male'", "as": "gender"}
  ],
  "width": {"step": 12},
  "facet": {"field": "age", "type": "ordinal"},
  "spec": {
    "encoding": {
      "y": {
        "aggregate": "sum",
        "field": "people",
        "type": "quantitative",
        "axis": {"title": "population", "grid": false}
      },
      "x": {"field": "gender", "type": "nominal", "axis": {"title": ""}}
    },
    "layer": [
      {
        "mark": "bar",
        "encoding": {
          "color": {
            "field": "gender",
            "type": "nominal",
            "scale": {"range": ["#675193", "#ca8861"]}
          }
        }
      },
      {
        "mark": {
          "type": "text",
          "dx": -5,
          "angle": 90,
          "baseline": "middle",
          "align": "right"
        },
        "encoding": {
          "text": {
            "aggregate": "sum",
            "field": "people",
            "type": "quantitative",
            "format": ".3s"
          }
        }
      }
    ]
  },
  "config": {
    "view": {"stroke": "transparent"},
    "facet": {"spacing": 10},
    "axis": {"domainWidth": 1}
  }
}