Public
Edited
May 6
Insert cell
Insert cell
Insert cell
Insert cell
// http://www.joshuastevens.net/cartography/make-a-bivariate-choropleth-map/
// This code creates and defines the different color schemes able to be used for bivariate maps.
schemes = [
{
name: "RdBu",
colors: [
"#e8e8e8", "#e4acac", "#c85a5a",
"#b0d5df", "#ad9ea5", "#985356",
"#64acbe", "#627f8c", "#574249"
]
},
{
name: "BuPu",
colors: [
"#e8e8e8", "#ace4e4", "#5ac8c8",
"#dfb0d6", "#a5add3", "#5698b9",
"#be64ac", "#8c62aa", "#3b4994"
]
},
{
name: "GnBu",
colors: [
"#e8e8e8", "#b5c0da", "#6c83b5",
"#b8d6be", "#90b2b3", "#567994",
"#73ae80", "#5a9178", "#2a5a5b"
]
},
{
name: "PuOr",
colors: [
"#e8e8e8", "#e4d9ac", "#c8b35a",
"#cbb8d7", "#c8ada0", "#af8e53",
"#9972af", "#976b82", "#804d36"
]
}
]
Insert cell
labels = ["low", "middle", "high"]
Insert cell
n = Math.floor(Math.sqrt(colors.length))
Insert cell
x = d3.scaleQuantile(Array.from(data.values(), d => d[0]), d3.range(n))
Insert cell
y = d3.scaleQuantile(Array.from(data.values(), d => d[1]), d3.range(n))
Insert cell
path = d3.geoPath().projection(projection)
Insert cell
projection = d3.geoAlbers().fitExtent([[0, 0], [width, height]], polygon_features)
Insert cell
polygon_features = topojson.feature(polygons, polygons.objects.California_tract_2023WGS84)
Insert cell
data = Object.assign(new Map(
d3.csvParse(await FileAttachment("nhgis0006_ds267_20235_tract.csv").text(),
({ GISJOIN, ASUBE001, ASVNE001 }) => [
GISJOIN,
[+ASUBE001 < 0 ? NaN : +ASUBE001, +ASVNE001 < 0 ? NaN : +ASVNE001]
])),
{ title: ["Avg Number of Rooms", "Median Value"] });
Insert cell
idAttribute = "GISJOIN"
Insert cell
polygons = FileAttachment("California_tract_2023WGS84.json").json()
Insert cell
height = 610
Insert cell
width = 875
Insert cell
legend = () => { //Defines a function legend that returns an SVG visual element
const k = 24; //Constant defined
const arrow = DOM.uid(); //A unique ID is generated for an arrow
// Adding a rectangle for NaN below the grid
const nanRectY = n * k + 30; // Position it below the grid with some spacing
return svg`<g font-family=sans-serif font-size=10>
<g transform="translate(-${k * n / 2},-${k * n / 2})
rotate(-45 ${k * n / 2},${k * n / 2})">
//Positions the group by translating it to the center at the midpoint, calculated using n and k, and rotates the group -45 degrees around the center
<marker id="${arrow.id}" markerHeight=10 markerWidth=10 refX=6 refY=3 orient=auto>
<path d="M0,0L9,3L0,6Z" />
</marker>
//Creates a marker element (arrow) used to create arrowheads for the legend and sets width and height. Sets refrence point for marker to position on lines. Defines shape of arrowhead.
${d3.cross(d3.range(n), d3.range(n)).map(([i, j]) => svg`<rect width=${k} height=${k} x=${i * k} y=${(n - 1 - j) * k} fill=${colors[j * n + i]}>
<title>${data.title[0]}${labels[j] && ` (${labels[j]})`}
${data.title[1]}${labels[i] && ` (${labels[i]})`}</title>
</rect>`)}
//Creates pairwise combinations of both variables, creating a grid. Iterates through the grid and creates a rectangle for each pair, sets the dimensions, position, and assigns a color. Also adds tooltip which displays info for each rectangle

<line marker-end="${arrow}" x1=0 x2=${n * k} y1=${n * k} y2=${n * k} stroke=black stroke-width=1.5 />
<line marker-end="${arrow}" y2=0 y1=${n * k} stroke=black stroke-width=1.5 />
//Adds two drawn arrow lines horizontal and vertical, and sets color and width

<text font-weight="bold" dy="1.5em" transform="rotate(90) translate(${n / 2 * k},6)" text-anchor="middle">${data.title[0]}</text>
<text font-weight="bold" dy="1.5em" transform="translate(${n / 2 * k},${n * k + 6})" text-anchor="middle">${data.title[1]}</text>
//adds text for labels of axes

<rect width=${k} height=${k} x=0 y=${nanRectY} fill="black">
<title>NaN</title>
</rect>
<text x=${k + 5} y=${nanRectY + k / 2} font-weight="bold" dy="0.35em" font-size="10">No Data</text>
</g>
</g>`;
}

Insert cell
color = {
return value => {
// Define a function that takes a single argument, `value`

if (!value) return "#ccc";
// If `value` is null, undefined, or falsy, return the color "#ccc" (a gray color for missing or invalid values)

let [a, b] = value;
// Destructure the `value` into two variables, `a` and `b`.
// Assumes `value` is an array or similar structure with two elements.

return colors[y(b) + x(a) * n];
// Calculate an index into the `colors` array:
// - `y(b)` computes a value based on `b`
// - `x(a)` computes a value based on `a`
// - Multiply `x(a)` by `n` (likely the width of a grid or the size of a dataset) and add `y(b)`
// Use the calculated index to retrieve the corresponding color from `colors`
};
};

Insert cell
formatNum = d3.format(".1f")
Insert cell
format = (value) => {
// Define a function `format` that takes a single argument, `value`

if (!value) return "N/A";
// If `value` is null, undefined, or falsy, return "N/A" (for missing or invalid data)

let [a, b] = value;
// Destructure the `value` into two variables, `a` and `b`.
// Assumes `value` is an array or structure containing two elements.

return `${formatNum(a)} ${data.title[0]}${labels[x(a)] && ` (${labels[x(a)]})`}
${formatNum(b)} ${data.title[1]}${labels[y(b)] && ` (${labels[y(b)]})`}`;
// Return a formatted string:
// - `formatNum(a)` converts `a` into a formatted number
// - Append `data.title[0]` (a title from the dataset) with optional label for `a`:
// - Check if `labels[x(a)]` exists; if so, add it in parentheses (e.g., " (LabelX)")
// - Similarly for `b`: format its value, append `data.title[1]`, and check for its label `labels[y(b)]`
};

Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@5")
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