Density
Smooth one numeric field into a density curve to compare the shape of groups, then change the bandwidth or switch to a cumulative curve.
A density chart shows the shape of one number as a smooth curve. OpenChart runs a Gaussian kernel density estimate over the raw values and draws it as a translucent filled area, so several groups can overlap on one axis and still be compared.
The smoothing is a choice you make through the bandwidth. A histogram shows the same data with bin edges instead, and a beeswarm keeps every observation visible. For a count per category, use a bar chart.
A basic density curve
Set mark to { type: "density" } and put the raw field on x. The y axis is hidden by default, because the absolute height of a density is not a quantity readers interpret.
{
"mark": {
"type": "density"
},
"data": [
{
"amount": 353,
"candidate": "Small donors"
},
{
"amount": 650,
"candidate": "Small donors"
},
{
"amount": 56,
"candidate": "Small donors"
},
{
"amount": 51,
"candidate": "Small donors"
},
{
"amount": 20,
"candidate": "Small donors"
},
{
"amount": 267,
"candidate": "Small donors"
},
// … 30 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "Small gifts cluster under $100",
"subtitle": "Kernel density estimate of contribution size"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 36 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density"
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "Small gifts cluster under $100",
"subtitle": "Kernel density estimate of contribution size"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 36 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density"
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "Small gifts cluster under $100",
"subtitle": "Kernel density estimate of contribution size"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 36 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density"
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "Small gifts cluster under $100",
"subtitle": "Kernel density estimate of contribution size"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 36 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density"
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
}
},
"chrome": {
"title": "Small gifts cluster under $100",
"subtitle": "Kernel density estimate of contribution size"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Grouped curves
Add a color channel for one curve per group. All the curves are evaluated over a shared extent, so they are comparable, and the fill defaults to 0.4 opacity when the chart is grouped.
{
"mark": {
"type": "density"
},
"data": [
{
"amount": 353,
"candidate": "Small donors"
},
{
"amount": 650,
"candidate": "Small donors"
},
{
"amount": 56,
"candidate": "Small donors"
},
{
"amount": 51,
"candidate": "Small donors"
},
{
"amount": 20,
"candidate": "Small donors"
},
{
"amount": 267,
"candidate": "Small donors"
},
// … 54 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "The same two distributions, smoothed",
"subtitle": "Kernel density estimate by group"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density"
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "The same two distributions, smoothed",
"subtitle": "Kernel density estimate by group"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density"
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "The same two distributions, smoothed",
"subtitle": "Kernel density estimate by group"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density"
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "The same two distributions, smoothed",
"subtitle": "Kernel density estimate by group"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density"
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "The same two distributions, smoothed",
"subtitle": "Kernel density estimate by group"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Bandwidth
bandwidth sets the width of the Gaussian kernel. When you omit it, OpenChart uses Silverman’s rule of thumb. A narrow value, such as 15 here, follows every bump in the sample.
{
"mark": {
"type": "density",
"bandwidth": 15
},
"data": [
{
"amount": 353,
"candidate": "Small donors"
},
{
"amount": 650,
"candidate": "Small donors"
},
{
"amount": 56,
"candidate": "Small donors"
},
{
"amount": 51,
"candidate": "Small donors"
},
{
"amount": 20,
"candidate": "Small donors"
},
{
"amount": 267,
"candidate": "Small donors"
},
// … 54 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "A narrow bandwidth follows every bump",
"subtitle": "Kernel density estimate by group, bandwidth 15"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density",
"bandwidth": 15
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "A narrow bandwidth follows every bump",
"subtitle": "Kernel density estimate by group, bandwidth 15"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density",
"bandwidth": 15
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "A narrow bandwidth follows every bump",
"subtitle": "Kernel density estimate by group, bandwidth 15"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density",
"bandwidth": 15
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "A narrow bandwidth follows every bump",
"subtitle": "Kernel density estimate by group, bandwidth 15"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density",
"bandwidth": 15
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "A narrow bandwidth follows every bump",
"subtitle": "Kernel density estimate by group, bandwidth 15"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Cumulative curves
cumulative: true draws the cumulative distribution instead of a density. Each curve climbs to its group’s total, so where one curve sits above another, that group has more of its values below that x.
{
"mark": {
"type": "density",
"cumulative": true
},
"data": [
{
"amount": 353,
"candidate": "Small donors"
},
{
"amount": 650,
"candidate": "Small donors"
},
{
"amount": 56,
"candidate": "Small donors"
},
{
"amount": 51,
"candidate": "Small donors"
},
{
"amount": 20,
"candidate": "Small donors"
},
{
"amount": 267,
"candidate": "Small donors"
},
// … 54 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Where each group's gifts accumulate",
"subtitle": "Cumulative distribution of contribution size"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density",
"cumulative": true
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Where each group's gifts accumulate",
"subtitle": "Cumulative distribution of contribution size"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density",
"cumulative": true
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Where each group's gifts accumulate",
"subtitle": "Cumulative distribution of contribution size"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density",
"cumulative": true
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Where each group's gifts accumulate",
"subtitle": "Cumulative distribution of contribution size"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 60 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "density",
"cumulative": true
},
"encoding": {
"x": {
"field": "amount",
"type": "quantitative",
"axis": {
"title": "Contribution ($)"
}
},
"color": {
"field": "candidate",
"type": "nominal"
}
},
"chrome": {
"title": "Where each group's gifts accumulate",
"subtitle": "Cumulative distribution of contribution size"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Fields that matter
| Field | What it does |
|---|---|
mark.type |
"density" runs a kernel density estimate over the x values. |
mark.bandwidth |
The Gaussian kernel bandwidth. Defaults to Silverman’s rule of thumb. |
mark.cumulative |
Draws a cumulative distribution instead of a density. Defaults to false. |
mark.steps |
The number of points the curve is evaluated at. Defaults to 200. |
mark.fillOpacity |
The fill opacity. Defaults to 0.4 when grouped. |
encoding.x |
The raw quantitative field to estimate over. Required. |
encoding.color |
Optional. One curve per group. |
Related
- Histogram: bin and count the same values.
- Beeswarm: one dot per observation.
- Error bars: show an interval around an estimate.
- Bar: compare one total per category.
- Scatter: relate two numbers.
- Transforms reference: every transform and its fields.