Public
Edited
Jul 19, 2024
Insert cell
Insert cell
import { Plot } from "@tututwo/rough-js-plugin-for-observable-plot"
Insert cell
Insert cell
chart = {
const legend = Plot.plot({
color: {
range: scheme,
transform: ([a, b]) => 3 * a + b,
unknown: "#ccc" // See Valdez-Cordova, Alaska
},
axis: null,
margin: 0,
inset: 18,
width: 106,
height: 106,
style: "overflow: visible;",
marks: [
Plot.dot(d3.cross([0, 1, 2], [0, 1, 2]), {
x: ([a, b]) => b - a,
y: ([a, b]) => b + a,
symbol: "square",
rotate: 45,
r: 14,
fill: (d) => d,
title: ([a, b]) => `Diabetes${label(a)}\nObesity${label(b)}`,
tip: true
}),
Plot.text(["Obesity →"], {
frameAnchor: "right",
fontWeight: "bold",
rotate: -45,
dy: 10
}),
Plot.text(["← Diabetes"], {
frameAnchor: "left",
fontWeight: "bold",
rotate: 45,
dy: 10
})
]
});

const color = legend.scale("color");
const index = new Map(data.map(({ county, ...rest }) => [county, rest]));
return Plot.plot({
width: width,
height: 800,
projection: "identity",
color,
marks: [
Plot.geo(
topojson.feature(us, us.objects.counties),
Plot.centroid({
stroke: "white",
rough: {
id: "I-am-an-id",
roughness: 1,
strokeWidth: 0.3,
fillWeight: 0.51,
bowing: 1,
fillStyle: "cross-hatch",
hachureGap: 2
},
fill: (d) => bivariateClass(index.get(d.id)),
title: (d) => {
const name = `${d.properties.name}, ${
states.get(d.id.slice(0, 2)).name
}`;
const value = index.get(d.id);
if (!value || (isNaN(value.diabetes) && isNaN(value.obesity)))
return `${name}\nno data`;
const [dc, oc] = bivariateClass(value);
return `${name}\n${
isNaN(value.diabetes) ? "No Data" : value.diabetes
}% Diabetes${label(dc)}\n${
isNaN(value.obesity) ? "No Data" : value.obesity
}% Obesity${label(oc)}`;
},
tip: true
})
)
,
() => svg`<g transform="translate(835,410)">${legend}`
],
style: "overflow: visible;"
});
}
Insert cell
Select a data source…
Type SQL, then Shift-Enter. Ctrl-space for more options.

Insert cell
labels = ["low", "", "high"]
Insert cell
label = i => labels[i] ? ` (${labels[i]})` : ""
Insert cell
data = FileAttachment("cdc_diabetes_obesity_2020.csv").csv()
.then((data) => {
data.forEach((d) => {
d.obesity = +d.obesity; // type as numeric
d.diabetes = +d.diabetes;
});
return data;
})
Insert cell
diabetes_thresholds = d3.scaleQuantile(data.map(d => d.diabetes), [0, 1, 2]).quantiles()
Insert cell
obesity_thresholds = d3.scaleQuantile(data.map(d => d.obesity), [0, 1, 2]).quantiles()
Insert cell
bivariateClass = {
const d = diabetes_thresholds;
const o = obesity_thresholds;
return (value) => {
const { diabetes: a, obesity: b } = value;
return [
isNaN(a) ? a : +(a > d[0]) + (a > d[1]),
isNaN(b) ? b : +(b > o[0]) + (b > o[1])
];
};
}
Insert cell
scheme = ["#e8e8e8","#ace4e4","#5ac8c8","#dfb0d6","#a5add3","#5698b9","#be64ac","#8c62aa","#3b4994"]
Insert cell
us = FileAttachment("counties-albers-10m.json").json()
Insert cell
states = new Map(us.objects.states.geometries.map(d => [d.id, d.properties]))
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more