legend = g => {
const xScale = d3.scaleLinear()
.range([600, 860])
.domain(d3.extent(color.domain()));
g.classed("legend", true)
.attr("transform", "translate(0, 40)")
.selectAll("rect")
.data(color.range().map(d => color.invertExtent(d)))
.enter()
.append("rect")
.attr("height", 8)
.attr("x", d => xScale(d[0]))
.attr("width", d => xScale(d[1]) - xScale(d[0]))
.attr("fill", d => color(d[0]));
g.append("text")
.attr("class", "caption")
.attr("x", xScale.range()[0])
.attr("y", -6)
.attr("fill", "#000")
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(`Ratio of households ${label}`);
g.call(d3.axisBottom(xScale)
.tickSize(13)
.tickFormat(format)
.tickValues(color.range().slice(1).map(d => color.invertExtent(d)[0])))
.select(".domain")
.remove();
}