Marginal Histogram (Jointplot)
A scatter plot of penguin beak length against beak depth framed by marginal histograms of each variable along the top and right edges.
ts
import { Binning, Constraint, chart, circle, field, layer, partition, rect, scatter } from "gofish-graphics";
import { penguins } from "./dataset";
const container = document.getElementById("app");
// Our penguins export uses "Beak ..." rather than seaborn's "bill ..." field names.
const data = penguins
.filter(
(d) =>
(d as any)["Beak Length (mm)"] != null &&
(d as any)["Beak Depth (mm)"] != null
)
.map((d, i) => ({ ...d, id: i }));
(async () => {
const sc = await chart(data)
.flow(
scatter({ by: "id", x: "Beak Length (mm)", y: "Beak Depth (mm)" } as any)
)
.mark(circle({ r: 3, fill: "steelblue", fillOpacity: 0.6 }))
.resolve();
sc.name("scatter");
const topHist = await chart(data, { h: 80 })
.flow(
partition({
by: field("Beak Length (mm)").bin(Binning.step(2)),
dir: "x",
})
)
.mark(rect({ h: field("Beak Length (mm)").count(), fill: "steelblue" }))
.resolve();
topHist.name("topHist");
const rightHist = await chart(data, { w: 80 })
.flow(
partition({
by: field("Beak Depth (mm)").bin(Binning.step(1)),
dir: "y",
})
)
.mark(rect({ w: field("Beak Depth (mm)").count(), fill: "steelblue" }))
.resolve();
rightHist.name("rightHist");
const GAP = 10;
await layer([sc, topHist, rightHist])
.relate(({ scatter, topHist, rightHist }: any) => [
Constraint.position({ x: 0, y: 0, anchor: "baseline" }, [scatter]),
Constraint.align({ x: "baseline" } as any, [scatter, topHist]),
Constraint.align({ y: "baseline" } as any, [scatter, rightHist]),
Constraint.distribute({ dir: "y", spacing: GAP }, [scatter, topHist]),
Constraint.distribute({ dir: "x", spacing: GAP }, [scatter, rightHist]),
])
.render(container, {
w: 400,
h: 400,
axes: { x: { title: "Beak Length (mm)" }, y: { title: "Beak Depth (mm)" } },
} as any);
})();dataset.ts
ts
export type Penguin = {
Species: string;
Island: string;
"Beak Length (mm)": number | null;
"Beak Depth (mm)": number | null;
"Flipper Length (mm)": number | null;
"Body Mass (g)": number | null;
Sex: string | null;
};
export const penguins = [
{
Species: "Adelie",
Island: "Torgersen",
"Beak Length (mm)": 39.1,
"Beak Depth (mm)": 18.7,
"Flipper Length (mm)": 181,
"Body Mass (g)": 3750,
Sex: "MALE",
},
{
Species: "Adelie",
Island: "Torgersen",
"Beak Length (mm)": 39.5,
"Beak Depth (mm)": 17.4,
"Flipper Length (mm)": 186,
"Body Mass (g)": 3800,
Sex: "FEMALE",
},
{
Species: "Adelie",
Island: "Torgersen",
"Beak Length (mm)": 40.3,
"Beak Depth (mm)": 18,
"Flipper Length (mm)": 195,
"Body Mass (g)": 3250,
Sex: "FEMALE",
},
{
Species: "Adelie",
Island: "Torgersen",
"Beak Length (mm)": null,
"Beak Depth (mm)": null,
"Flipper Length (mm)": null,
"Body Mass (g)": null,
Sex: null,
},
{
Species: "Adelie",
Island: "Torgersen",
"Beak Length (mm)": 36.7,
"Beak Depth (mm)": 19.3,
"Flipper Length (mm)": 193,
"Body Mass (g)": 3450,
Sex: "FEMALE",
},
// … 339 more rows
];