Plot.plot({
title: "Global Median Age and Fertility Rate",
x: {
label: "Year",
domain: [1960, 2020],
ticks: [1960, 1970, 1980, 1990, 2000, 2010, 2020]
},
y: {
label: "Fertility Rate",
domain: [1, 5],
grid: true
},
color: {
domain: ["Fertility Rate", "Median Age"],
range: ["steelblue", "tomato"],
legend: true
},
marks: [
Plot.axisY({ anchor: "left", label: "Fertility Rate", color: "steelblue" }),
Plot.axisY(
d3.scaleLinear(d3.extent(data, d => d.medianAge), [1, 5]).ticks(),
{
y: d3.scaleLinear(d3.extent(data, d => d.medianAge), [1, 5]),
anchor: "right",
label: "Median Age",
color: "tomato",
tickFormat: d3.scaleLinear(d3.extent(data, d => d.medianAge), [1, 5]).tickFormat()
}
),
Plot.lineY(data, {
x: "year",
y: "fertilityRate",
stroke: "steelblue",
}),
Plot.lineY(
data.map(d => ({
year: d.year,
mapped: d3.scaleLinear(d3.extent(data, d => d.medianAge), [1, 5])(d.medianAge)
})),
{
x: "year",
y: "mapped",
stroke: "tomato"
}
),
Plot.ruleY([1])
]
})