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

如何修改Vega-Lite代码生成含自定义分位数的分布图表?

修改Vega-Lite代码生成含5%/25%/中位数/75%/95%分位数的分布图表

原代码通过聚合最小值、25%分位数、中位数、75%分位数和最大值,生成分层条形的分布图表。如需替换为5%、25%、中位数、75%、95%分位数,需按以下两处修改代码:

修改步骤

  • 替换聚合操作:在transform的aggregate数组中,移除min和max操作,新增计算5%分位数(p05)和95%分位数(p95)的操作,保留原有的q1、median、q3操作
  • 调整分层条形区间:更新layer中每个条形图层的y和y2字段,对应新的分位数区间(5%-25%、25%-中位数、中位数-75%、75%-95%)

修改后的完整代码

{
  "data": {"name": "dataset"},
  "transform": [
    {
      "aggregate": [
        {
          "op": "p05",
          "field": "Total Sales",
          "as": "P05"
        },
        {
          "op": "p95",
          "field": "Total Sales",
          "as": "P95"
        },
        {
          "op": "median",
          "field": "Total Sales",
          "as": "Median"
        },
        {
          "op": "q1",
          "field": "Total Sales",
          "as": "Q1"
        },
        {
          "op": "q3",
          "field": "Total Sales",
          "as": "Q3"
        }
      ],
      "groupby": ["Subcategory"]
    }
  ],
  "layer": [
    {
      "mark": {
        "type": "bar",
        "color": "red"
      },
      "encoding": {
        "y": {
          "field": "P05",
          "type": "quantitative"
        },
        "y2": {"field": "Q1"}
      }
    },
    {
      "mark": {
        "type": "bar",
        "color": "blue"
      },
      "encoding": {
        "y": {
          "field": "Q1",
          "type": "quantitative"
        },
        "y2": {"field": "Median"}
      }
    },
    {
      "mark": {
        "type": "bar",
        "color": "green"
      },
      "encoding": {
        "y": {
          "field": "Median",
          "type": "quantitative"
        },
        "y2": {"field": "Q3"}
      }
    },
    {
      "mark": {
        "type": "bar",
        "color": "yellow"
      },
      "encoding": {
        "y": {
          "field": "Q3",
          "type": "quantitative"
        },
        "y2": {"field": "P95"}
      }
    }
  ],
  "encoding": {
    "x": {"field": "Subcategory"}
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 04:57:45