Plot.plot({
inset: 10,
width: 1200,
marks: [
Plot.frame({ stroke: "#eee" }),
Plot.axisX({
label: xLabel,
ticks: 5,
tickFormat: "$.0s",
grid: true
}),
Plot.gridX({ ticks: 5 }),
Plot.axisY({
label: "Healthcare expenditure as fraction of GDP",
tickFormat: "%"
}),
Plot.gridY(),
Plot.axisFx({ label: "Year" }),
Plot.linearRegressionY(data, {
...channels,
filter: (d) =>
d.population >= 1e6 &&
["Saudi Arabia", "Switzerland", "Ireland", "United States"].indexOf(
d.country
) < 0,
stroke: "#ff7f0e"
}),
Plot.dot(data, { fx: "year", x: "gpc", y: "hpg" }),
Plot.text(data, {
filter: (d, i) => d.year === "1990" && d.country === "United States",
fx: "year",
x: { value: 2.32e4 },
y: { value: 0.085 },
text: (d) =>
"Regression performed on countries with population at least 1M, excluding U.S., Saudi Arabia, Luxembourg, and Ireland.",
...tooltipStyle,
textAnchor: "start",
lineAnchor: "bottom",
lineWidth: 24,
fontStyle: "italic",
fill: "black",
stroke: "white"
}),
Plot.tip(data, {
filter: (d) => d.country === "United States" && d.year === "2020",
...channels,
anchor: "top-left",
title: tooltipTitle,
...tooltipStyle
}),
Plot.tip(data, {
filter: (d) => d.country === "United States" && d.year !== "2020",
...channels,
anchor: "bottom-left",
title: tooltipTitle,
...tooltipStyle
}),
Plot.tip(
data,
Plot.pointer({
filter: (d) => d.country !== "United States",
...channels,
title: tooltipTitle,
...tooltipStyle
})
)
]
})