箱形图
上一篇
面积图
下一篇
双向柱状图
Loading...
箱形图(Box Plot),又称盒须图、盒式图或箱线图,是一种用于展示一组数据分布情况的统计图表。它能够显示数据的五个重要统计量:最小值、下四分位数(Q1)、中位数(Q2)、上四分位数(Q3)和最大值,同时可以清楚地识别出数据中的异常值。
箱形图的设计简洁明了,通过箱体和须线的组合,可以快速了解数据的集中趋势、离散程度和偏态分布,是统计分析和数据探索中的重要工具。
英文名:Box Plot, Box-and-Whisker Plot
其他名称:盒须图、盒式图、箱线图
图表类型 | 箱形图 |
---|---|
适合的数据 | 一个分类数据字段、一个连续数据字段 |
功能 | 展示数据分布情况,识别异常值,比较不同分组的数据分布 |
数据与图形的映射 | 分类数据字段映射到横轴位置 连续数据字段自动计算统计值映射到箱体各部分 异常值显示为散点 |
适合的数据条数 | 每个分组建议至少有 5-10 个数据点 |
箱形图的主要组成部分包括:
import { Chart } from '@antv/g2';const chart = new Chart({container: 'container',theme: 'classic',});chart.options({type: 'boxplot',autoFit: true,data: {type: 'fetch',value: 'https://assets.antv.antgroup.com/g2/morley.json',},encode: {x: 'Expt',y: 'Speed',},style: {boxFill: '#1890ff',boxFillOpacity: 0.3,pointStroke: '#f5222d',pointR: 3,},});chart.render();
场景 1:数据分布分析
箱形图是分析数据分布的理想工具,能够快速识别数据的集中趋势、离散程度和偏态分布。
import { Chart } from '@antv/g2';const chart = new Chart({container: 'container',theme: 'classic',});chart.options({type: 'boxplot',autoFit: true,data: {type: 'fetch',value: 'https://assets.antv.antgroup.com/g2/penguins.json',},encode: {x: 'species',y: 'flipper_length_mm',color: 'species',},axis: {y: {title: '鳍长 (mm)',},x: {title: '企鹅种类',},},});chart.render();
场景 2:异常值检测
箱形图能够直观地显示数据中的异常值,帮助识别需要进一步调查的数据点。
场景 3:多组数据比较
通过并排显示多个箱形图,可以有效地比较不同组别间的数据分布差异。
import { Chart } from '@antv/g2';const chart = new Chart({container: 'container',theme: 'classic',});chart.options({type: 'boxplot',autoFit: true,data: {type: 'fetch',value: 'https://assets.antv.antgroup.com/g2/penguins.json',},encode: {x: 'species',y: 'flipper_length_mm',color: 'sex',series: 'sex',},axis: {y: {title: '鳍长 (mm)',},x: {title: '企鹅种类',},},});chart.render();
场景 1:数据量过少
当每个分组的数据点少于 5 个时,箱形图的统计意义不大,建议使用散点图或条形图。
场景 2:展示精确数值
箱形图侧重于显示数据分布的整体特征,不适合需要精确数值的场景,此时应使用表格或条形图。
场景 3:时间序列分析
通过设置不同的颜色和系列,可以在同一图表中比较多个维度的数据分布。
import { Chart } from '@antv/g2';const chart = new Chart({container: 'container',theme: 'classic',});chart.options({type: 'boxplot',autoFit: true,data: {type: 'fetch',value: 'https://assets.antv.antgroup.com/g2/penguins.json',},encode: {x: 'species',y: 'body_mass_g',color: 'sex',series: 'sex',},axis: {y: {title: '体重 (g)',},x: {title: '企鹅种类',},},});chart.render();
当分类标签过长时,可以使用横向箱形图来提高可读性。
import { Chart } from '@antv/g2';const chart = new Chart({container: 'container',theme: 'classic',});chart.options({type: 'boxplot',coordinate: { transform: [{ type: 'transpose' }] },autoFit: true,data: {type: 'fetch',value: 'https://assets.antv.antgroup.com/g2/morley.json',transform: [{ type: 'filter', callback: (d) => d.Expt <= 3 }],},encode: {x: 'Expt',y: 'Speed',color: 'Expt',},axis: {x: {title: '光速测量值',},y: {title: '实验编号',},},});chart.render();
在某些场景下,可以隐藏异常点,只关注数据的整体分布。
import { Chart } from '@antv/g2';const chart = new Chart({container: 'container',theme: 'classic',});chart.options({type: 'boxplot',autoFit: true,data: {type: 'fetch',value: 'https://assets.antv.antgroup.com/g2/morley.json',},encode: {x: 'Expt',y: 'Speed',},style: {point: false,boxFill: '#52c41a',boxFillOpacity: 0.4,},axis: {y: {title: '光速测量值',},x: {title: '实验编号',},},});chart.render();