如何在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标记绘制组间的显著性连线,明确标注比较的两组 - 第四层:使用加粗文本标记添加代表显著性等级的星号,放置在连线上方
实际生成效果

内容的提问来源于stack exchange,提问作者nlarusstone
相关产品推荐
相关产品推荐

