Unlisted
Edited
Mar 28, 2023
Insert cell
Insert cell
Plot.auto(penguins, {x: "culmen_length_mm", y: "species"}).plot()
Insert cell
Insert cell
Plot.auto(alphabet, {x: "frequency", y: "letter"}).plot()
Insert cell
Insert cell
function maybeBar(data, { x, y }) {
const X = Plot.valueof(data, x);
const Y = Plot.valueof(data, y);
if (
(isOrdinal(X) && !isOrdinal(Y) && isBijective(X)) ||
(isOrdinal(Y) && !isOrdinal(X) && isBijective(Y))
) {
return Plot.auto(data, { x, y, mark: "bar" });
}
return Plot.auto(data, { x, y });
}
Insert cell
maybeBar(penguins, {x: "culmen_length_mm", y: "species"}).plot()
Insert cell
maybeBar(alphabet, {x: "frequency", y: "letter"}).plot()
Insert cell
maybeBar(alphabet, {x: "letter", y: "frequency"}).plot()
Insert cell
Insert cell
function isBijective(values) {
return d3.rollups(values, (arr) => arr.length, (d) => d)
.every(([, len]) => len === 1);
}
Insert cell
function isOrdinal(values) {
for (const value of values) {
if (value == null) continue;
const type = typeof value;
return type === "string" || type === "boolean";
}
}
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