Brushable Scatterplot Matrix
The repeat matrix of four penguin measurements: a grid of cells, each nesting a scatterplot of its own pair of fields, so each penguin is a row of its own in each cell, matched across them by its key. Brushing in a cell selects the penguins there: they keep their colors in every cell and grow, while the others turn gray. Like Vega-Lite's, the rows are in the reverse order of the columns, so the diagonal runs from the bottom left to the top right. Drag in any cell to brush. After D3.
js
const penguins = await d3.csv("/data/penguins.csv", d3.autoType);
const fields = ["culmen_length_mm", "culmen_depth_mm", "flipper_length_mm", "body_mass_g"];
const brush = cm.selection();
const renderer = cm.svg({width: 800, height: 800});
const root = cm.flow().renderer(renderer);
root
.data(d3.cross(fields, fields))
.transform(cm.encode, {x: ([fx]) => fx, y: ([, fy]) => fy})
.transform(cm.spreadX, {domain: fields})
.transform(cm.spreadY, {domain: fields.slice().reverse()})
.transform(cm.plane, {
x: {label: null, tickSize: 0},
y: {label: null, tickSize: 0, tickRotate: -90},
})
.append(cm.g)
.data(([fx, fy]) =>
penguins
.map((d, id) => ({id, x: d[fx], y: d[fy], species: d.species}))
.filter((d) => Number.isFinite(d.x) && Number.isFinite(d.y)),
)
.transform(cm.encode, {x: "x", y: "y", fill: "species", key: "id"})
.transform(cm.plane, {
resolve: {x: "independent", y: "independent"},
x: {axis: "bottom", grid: true},
y: {axis: "left", grid: true},
})
.add(cm.annotate(cm.rect), {fill: "none", stroke: "currentColor"})
.add(cm.circle, {r: brush.match(3, 1.5, 1.5), fill: brush.match((d) => d.fill, "#ccc")})
.add(cm.brush, {as: brush});
root.render();
document.body.append(renderer.node());