【问题标题】:How to add text mark to a Vega Lite grouped bar chart如何将文本标记添加到 Vega Lite 分组条形图
【发布时间】:2020-09-18 14:10:14
【问题描述】:

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

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

但是发生的情况是“层”属性不被接受,或者条被压缩成一个条。如何做到这一点?

【问题讨论】:

    标签: data-visualization vega-lite


    【解决方案1】:

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

    例如(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}
      }
    }
    

    【讨论】:

    • 该解决方案有效并给出了预期的结果。使用带有分层视图的规范是我以前没有做过的事情,感谢您的解释!。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 2022-11-03
    • 2021-06-12
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多