You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Vega/Vega-Lite柱状图添加显著性比较星号标记?

在Vega/Vega-Lite图表中添加显著性(p值)标记

完全可以在Vega(尤其是Vega-Lite)中添加用于表示显著性比较的标记,比如连接组间的横线、代表p值等级的星号,实现类似ggplot中的显著性标注效果。

目标效果示例

带误差棒和显著性比较的柱状图

可实现该效果的Vega-Lite配置示例

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "带显著性标记的柱状图",
  "data": {
    "values": [
      {"group": "DMSO", "value": 1.0},
      {"group": "Compound 1", "value": 0.4},
      {"group": "Compound 2", "value": 0.55}
    ]
  },
  "width": 300,
  "height": 200,
  "layer": [
    {
      "mark": "bar",
      "encoding": {
        "x": {"field": "group", "type": "nominal", "axis": {"labelAngle": 0}},
        "y": {"field": "value", "type": "quantitative"},
        "color": {"field": "group", "type": "nominal"}
      }
    },
    {
      "mark": {"type": "text", "fontSize": 14, "dy": -5},
      "encoding": {
        "x": {"field": "group", "type": "nominal"},
        "y": {"field": "value", "type": "quantitative"},
        "text": {"field": "value", "type": "quantitative", "format": ".2f"}
      }
    },
    {
      "data": {
        "values": [
          {"x": "DMSO", "x2": "Compound 1", "y": 1.3, "label": "**"},
          {"x": "DMSO", "x2": "Compound 2", "y": 1.4, "label": "**"}
        ]
      },
      "mark": {"type": "rule"},
      "encoding": {
        "x": {"field": "x", "type": "nominal"},
        "x2": {"field": "x2"},
        "y": {"field": "y", "type": "quantitative"}
      }
    },
    {
      "data": {
        "values": [
          {"x": "Compound 1", "y": 1.32, "label": "**"},
          {"x": "Compound 2", "y": 1.42, "label": "**"}
        ]
      },
      "mark": {"type": "text", "fontSize": 16, "fontWeight": "bold"},
      "encoding": {
        "x": {"field": "x", "type": "nominal"},
        "y": {"field": "y", "type": "quantitative"},
        "text": {"field": "label"}
      }
    }
  ]
}

配置说明

  • 第一层:绘制基础柱状图,展示各组数据的数值大小
  • 第二层:在柱状顶部添加数值标签,方便读取具体数值
  • 第三层:通过rule标记绘制组间的显著性连线,明确标注比较的两组
  • 第四层:使用加粗文本标记添加代表显著性等级的星号,放置在连线上方

实际生成效果

添加了显著性标记的Vega-Lite柱状图

内容的提问来源于stack exchange,提问作者nlarusstone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 11:37:29