Skip to content

Titanic Mosaic ​

The Titanic's passengers by class, then sex, then survival, one dot each. Every level is a chart nested in the bands of the one before, its bands proportional to their passengers with flex, and every cell packs its passengers, so the cells' areas read as the counts. After GoFish.

js
const passengers = await d3.csv("/data/titanic3.csv", d3.autoType);
const n = ([, rows]) => rows.length;

const renderer = cm.svg({width: 640, height: 480});
const root = cm.flow().renderer(renderer);

root
  .data(d3.groups(passengers, (d) => d.pclass))
  .transform(cm.encode, {y: ([pclass]) => pclass, n})
  .transform(cm.spreadY, {padding: 0.05, flex: "n", domain: [3, 2, 1]})
  .transform(cm.plane, {margin: 10, y: {axis: null}})
  .append(cm.g)
  .data(([, rows]) => d3.groups(rows, (d) => d.sex))
  .transform(cm.encode, {y: ([sex]) => sex, n})
  .transform(cm.spreadY, {padding: 0.06, flex: "n", domain: ["male", "female"]})
  .transform(cm.plane)
  .append(cm.g)
  .data(([, rows]) => d3.groups(rows, (d) => d.survived))
  .transform(cm.encode, {x: ([s]) => s, n})
  .transform(cm.spreadX, {padding: 0.03, flex: "n", domain: [1, 0]})
  .transform(cm.plane)
  .append(cm.g)
  .data(([, rows]) => rows)
  .transform(cm.encode, {fill: "survived"})
  .transform(cm.plane, {fill: {type: "ordinal", domain: [1, 0], range: ["#2b8cbe", "#ff8408"]}})
  .transform(cm.pack)
  .add(cm.annotate(cm.rect), {fill: "none", stroke: "currentColor"})
  .add(cm.circle, cm.inset("7.5%"));

root.render();
document.body.append(renderer.node());