p2 = {
const coLData = await FileAttachment("MBM_diffs_by_province-2.csv").csv();
const populationData = await FileAttachment("pop_stdevs_by_province.csv").csv();
const combinedData = populationData.map(populationObj => {
const provinceCode = populationObj.province;
const years = Object.keys(populationObj).filter(key => key === "2017" || key === "2022");
const combinedDataPerProvince = years.map(year => {
const stdDev = parseFloat(populationObj[year]);
const costDifferenceObj = coLData.find(costObj => costObj.province === provinceCode);
const costDifference = costDifferenceObj ? costDifferenceObj[year] : null;
return {
province: provinceCode,
year: year,
stdDev: parseInt(stdDev),
costDifference: costDifference !== null ? parseInt(costDifference) : null,
};
});
return combinedDataPerProvince;
}).flat();
return Plot.plot({
marginTop: 50,
marginBottom: 40,
marginLeft: 60,
style: {backgroundColor: "#fff5cb", fontFamily: "IBM Plex Mono", fontSize: "14px"},
color: {
domain: ["NL", "PE", "NS", "NB", "QC", "ON", "MB", "SK", "AB", "BC"],
range: ["#7fc97f", "#beaed4", "#fdc086", "#ffff99", "#a4d5e4", "#f0027f", "#bf5b17", "#666666", "#1f78b4", "#33a02c"],
legend: true
},
symbol: {
domain: ["2017", "2022"],
range: ["circle", "star"],
legend: true
},
marks: [
Plot.ruleY([0]),
Plot.ruleX([0]),
Plot.arrow(
combinedData.filter(d => d.year === "2017"),
{
x1: d => d.stdDev,
y1: d => d.costDifference,
x2: d => {
const correspondingPoint = combinedData.find(
point => point.province === d.province && point.year === "2022"
);
return correspondingPoint ? correspondingPoint.stdDev : d.stdDev;
},
y2: d => {
const correspondingPoint = combinedData.find(
point => point.province === d.province && point.year === "2022"
);
return correspondingPoint ? correspondingPoint.costDifference : d.costDifference;
},
stroke: "black",
strokeWidth: 1,
headLength: 0
}
),
Plot.dot(combinedData, {
x: "stdDev",
y: "costDifference",
r: 4,
fill: "province",
symbol: "year",
stroke: "black"
}),
],
x: {
label: "Population spread",
},
y: {
label: "Rural v. urban cost of living difference ($ CAD / year)",
nice: true
},
});
}