【问题标题】:Nested axis labels for grouped bar chart in Vega LiteVega Lite 中分组条形图的嵌套轴标签
【发布时间】:2022-11-03 16:11:43
【问题描述】:

我正在使用 Vega-Lite 构建分组条形图。每个数据点都有一个count、一个sample 和一个fdr 值。这些条按sample 分组,在这些组中,我希望每个fdr-value 一个条。我能够使用以下规范完成所有这些工作(click here 用于 Vega 编辑器中的版本):

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      {"count": 87, "organism": "Thaliana", "sample": "S01", "fdr": "0.1%"},
      {"count": 231, "organism": "Spider", "sample": "S01", "fdr": "0.1%"},
      {"count": 40, "organism": "Thaliana", "sample": "S01", "fdr": "1%"},
      {"count": 560, "organism": "Spider", "sample": "S01", "fdr": "1%"},
      {"count": 11.33, "organism": "Thaliana", "sample": "S02", "fdr": "0.1%"},
      {"count": 88.67, "organism": "Spider", "sample": "S02", "fdr": "0.1%"},
      {"count": 70, "organism": "Thaliana", "sample": "S02", "fdr": "1%"},
      {"count": 30, "organism": "Spider", "sample": "S02", "fdr": "1%"}
    ]
  },
  "width": "container",
  "layer": [
    {
      "mark": "bar",
      "encoding": {
        "x": {
          "aggregate": "sum",
          "field": "count",
          "stack": "normalize",
          "title": "Distribution (%)",
          "sort": "descending",
          "axis": {"labelFontSize": 14, "titleFontSize": 20}
        },
        "y": {
          "field": "sample",
          "title": "Sample name",
          "axis": {
            "labelFontSize": 14,
            "titleFontSize": 20,
            "labelLimit": 500,
            "labelExpr": "datum.label"
          }
        },
        "yOffset": {"field": "fdr", "scale": {"paddingInner": 0.1}},
        "color": {"field": "organism", "legend": {"title": "Species"}}
      }
    }
  ],
  "config": {
    "legend": {
      "orient": "bottom",
      "direction": "vertical",
      "columns": 4,
      "labelFontSize": 14,
      "titleFontSize": 16,
      "labelLimit": 250,
      "symbolSize": 150
    }
  }
}

当前可视化的屏幕截图:

现在,我遇到的问题是我还想添加一个标签,显示每个柱的fdr-值(因为每个柱只对应一个fdr)。但我无法让它工作,因为yOffset 通道不支持它自己的轴。

关于我如何做到这一点的任何想法?

编辑:这是我想要实现的大致目标的示例:

【问题讨论】:

    标签: bar-chart axis-labels vega-lite grouped-bar-chart


    【解决方案1】:

    你的意思是这样吗?

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "data": {
        "values": [
          {"count": 87, "organism": "Thaliana", "sample": "S01", "fdr": "0.1%"},
          {"count": 231, "organism": "Spider", "sample": "S01", "fdr": "0.1%"},
          {"count": 40, "organism": "Thaliana", "sample": "S01", "fdr": "1%"},
          {"count": 560, "organism": "Spider", "sample": "S01", "fdr": "1%"},
          {"count": 11.33, "organism": "Thaliana", "sample": "S02", "fdr": "0.1%"},
          {"count": 88.67, "organism": "Spider", "sample": "S02", "fdr": "0.1%"},
          {"count": 70, "organism": "Thaliana", "sample": "S02", "fdr": "1%"},
          {"count": 30, "organism": "Spider", "sample": "S02", "fdr": "1%"}
        ]
      },
      "width": "container",
      "encoding": {
        "y": {
          "field": "sample",
          "title": "Sample name",
          "axis": {
            "labelFontSize": 14,
            "titleFontSize": 20,
            "labelLimit": 500,
            "labelExpr": "datum.label"
          }
        },
        "yOffset": {"field": "fdr", "scale": {"paddingInner": 0.1}}
      },
      "layer": [
        {
          "mark": "bar",
          "encoding": {
            "x": {
              "aggregate": "sum",
              "field": "count",
              "stack": "normalize",
              "title": "Distribution (%)",
              "sort": "descending",
              "axis": {"labelFontSize": 14, "titleFontSize": 20}
            },
            "color": {"field": "organism", "legend": {"title": "Species"}}
          }
        },
        {
          "mark": {"type": "text", "opacity": 0.9},
          "encoding": {
            "x": {
              "aggregate": "sum",
              "field": "count",
              "sort": "descending",
              "stack": "normalize",
              "bandPosition": 0.5
            },
            "text": {"field": "fdr"},
            "detail": {"field": "organism"},
            "color": {"value": "white"}
          }
        }
      ],
      "config": {
        "legend": {
          "orient": "bottom",
          "direction": "vertical",
          "columns": 4,
          "labelFontSize": 14,
          "titleFontSize": 16,
          "labelLimit": 250,
          "symbolSize": 150
        }
      }
    }
    

    【讨论】:

    • 嗨,这已经更好了,但我实际上是指帖子中的新示例(我更新了问题并添加了一个更好的示例来说明我想要实现的目标)。
    猜你喜欢
    • 1970-01-01
    • 2021-08-22
    • 2020-03-27
    • 1970-01-01
    • 2021-12-19
    • 2019-06-02
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多