Public
Edited
May 28, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.tsvParse(raw)
Insert cell
[...new Set(data.map(d => d["Question #2 Answer"]))]
Insert cell
[...new Set(data.map(d => d["Question #1 Answer"]))]
Insert cell
domain = ["18-29", "30s", "40s", "50s", "60s", "70s and up"]
Insert cell
Plot.barX(
data,
Plot.groupY(
{ x: "count" },
{ y: "Question #2 Answer", fill: "Question #1 Answer" }
)
).plot({ x: { percent: true }, y: { domain, label: "Age" }, marginLeft: 70 })
Insert cell
data1 = d3.cross(domain, ["Yes", "No"]).map(([age, feel]) => {
const segment = data.filter(d => d["Question #2 Answer"] === age);
const frac = segment.filter(d => d["Question #1 Answer"] === feel).length / segment.length;
return {age, feel, frac}
})
Insert cell
Plot.plot({
subtitle: "Do you feel old?",
marginLeft: 70,
x: { label: null, tickFormat: ".0%" },
y: { label: "Age" },
marks: [
Plot.barX(data1, { x: "frac", y: "age", fill: (d) => d.feel === "Yes" }),
Plot.textX(
data1,
Plot.selectFirst(
Plot.stackX({
x: "frac",
y: "age",
z: (d) => d.feel === "Yes",
text: "feel",
inset: 0.5,
fill: "black",
stroke: "white"
})
)
)
]
})
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