Published
Edited
May 16, 2022
4 stars
Insert cell
Insert cell
Insert cell
// curve distribution function suggested by @mootari, thanks!
population = {
const random = d3.randomLcg(42);
const curve = (t) => (1 - t ** 0.4) ** 0.75;
return Array.from({ length: 40 }, () => ({
age: (curve(random()) * 100) | 0,
hand: ["Y", "L", "R", "Z"][(0.9 + random() * 2.2) | 0]
}));
}
Insert cell
Insert cell
Insert cell
Plot.plot({
height: 100,
marks: [
Plot.barY(population, Plot.groupX({ y: "count" }, { x: "age" })),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Plot.plot({
height: 100,
marks: [
Plot.barY(
population,
Plot.groupX(
{ y: "count" },
{
x: (d) =>
`${5 * Math.floor(d.age / 5)}-${5 + 5 * Math.floor(d.age / 5)}`
}
)
),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Plot.plot({
height: 300,
marks: [
Plot.rectY(
population,
Plot.binX(
{ y: "count" },
{ x: "age", thresholds: [5, 10, 18, 25, 35, 50, 65] }
)
),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Plot.plot({
height: 100,
marks: [
Plot.rect(
population,
Plot.stackY(
Plot.binX(
{ y: "count" },
{
x: "age",
thresholds: 90
}
)
)
),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Plot.plot({
height: 250,
marks: [
Plot.rect(
population,
Plot.stackY(
Plot.binX(
{ y: "count" },
{
x: "age",
interval: 5
}
)
)
),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Plot.plot({
height: 250,
marks: [
Plot.rectY(
population,
Plot.binX({ y: "count" }, { x: "age", fill: "hand", interval: 5 })
),
Plot.ruleY([0])
],
color: { legend: true }
})
Insert cell
Insert cell
Plot.plot({
height: 250,
marks: [
Plot.rectY(
population,
Plot.binX(
{ y: "count" },
{
x: "age",
fill: (d) => (["R", "L"].includes(d.hand) ? d.hand : "other"),
interval: 5
}
)
),
Plot.ruleY([0])
],
color: { legend: true }
})
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
height: 250,
marks: [
Plot.rectY(
population,
Plot.binX(
{ y: "count" },
{
x: "age",
filter: (d) => d.age >= ageMin && d.age < ageMax,
fill: (d) => (["R", "L"].includes(d.hand) ? d.hand : "other"),
interval: 5
}
)
),
Plot.ruleY([0])
],
color: { legend: true }
})
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
height: 250,
marks: [
Plot.rectY(
population,
Plot.binX(
{ y: "count" },
{
x: "age",
fill: (d) => (["R", "L"].includes(d.hand) ? d.hand : "other"),
interval: 5,
opacity: 0 // 💡 invisible marks!
}
)
),

Plot.rectY(
population,
Plot.binX(
{ y: "count" },
{
x: "age",
filter: (d) => d.age >= ageMin && d.age < ageMax,
fill: (d) => (["R", "L"].includes(d.hand) ? d.hand : "other"),
interval: 5
}
)
),
Plot.ruleY([0])
],
color: { legend: true }
})
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
height: 250,
marks: [
Plot.rectY(
population,
Plot.filter(
([d]) => d.age >= ageMin && d.age < ageMax,
Plot.stackY(
Plot.binX(
{ y: "count" },
{
x: "age",
fill: (d) => (["R", "L"].includes(d.hand) ? d.hand : "other"),
interval: 5
}
)
)
)
),
Plot.ruleY([0])
],
color: { legend: true }
})
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
height: 250,
marks: [
Plot.rectY(
population,
Plot.binX(
{ y: "count" },
{
x: "age",
fill: (d) => (["R", "L"].includes(d.hand) ? d.hand : "other"),
interval: 5
}
)
),
Plot.rectY(
population,
Plot.binX(
{ y: "count" },
{
x: "age",
filter: (d) => !(d.age >= ageMin && d.age < ageMax),
fill: "lightgray",
interval: 5
}
)
),
Plot.ruleY([0])
],
color: { legend: true }
})
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more