简体   繁体   中英

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

Given a stacked bar plot, how can I add labels with the values of each of the bar?

Below is an example of what I'm trying to do: 堆积条示例

You can do this by layering a bar mark and a text mark. For example ( 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"
        }
      }
    }
  ]
}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM