chart = Plot.plot({
width: 900,
facet: {
data: loadingsAndScores,
x: "year"
},
x: { domain: [-4, 4] },
y: { domain: [-4, 4] },
color: { legend: true },
marks: [
Plot.density(loadingsAndScores, {
x: "PC1",
y: "PC2",
fill: "watershed",
stroke: "watershed",
fillOpacity: (d) => (d.watershed ? 0.02 : 0),
strokeOpacity: (d) => (d.watershed ? 0.8 : 0),
tip: true
}),
Plot.ruleX([0], { stroke: "lightgrey" }),
Plot.ruleY([0], { stroke: "lightgrey" }),
Plot.arrow(loadingsAndScores, {
x1: 0,
x2: (d) => d.PC1 * scalingFactor,
y1: 0,
y2: (d) => d.PC2 * scalingFactor,
opacity: (d) => (d.Variable ? 1 : 0)
}),
Plot.text(loadingsAndScores, {
x: (d) => d.PC1 * scalingFactor * 1.3,
y: (d) => d.PC2 * scalingFactor * 1.1,
text: "Variable",
opacity: (d) => (d.Variable ? 1 : 0)
})
]
})