Vega-Lite - 如何在每个 nar 中绘制带有标签的堆叠条?

Vega-Lite - How to plot stacked bar with labels in each nar?

给定堆积条形图,我如何添加带有每个条形值的标签?

下面是我正在尝试做的一个例子:

您可以通过将条形标记和文本标记分层来实现。例如 (open in editor):

{
  "data": {"url": "data/barley.json"},
  "width": 400,
  "encoding": {
    "x": {
      "type": "quantitative",
      "aggregate": "sum",
      "field": "yield",
      "stack": "zero"
    },
    "y": {"type": "nominal", "field": "variety"}
  },
  "layer": [
    {
      "mark": "bar",
      "encoding": {"color": {"type": "nominal", "field": "site"}}
    },
    {
      "mark": {"type": "text", "color": "white", "dx": -15, "dy": 3},
      "encoding": {
        "detail": {"type": "nominal", "field": "site"},
        "text": {
          "type": "quantitative",
          "aggregate": "sum",
          "field": "yield",
          "format": ".1f"
        }
      }
    }
  ]
}