Public
Edited
Jan 5, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function color(d) {
const neutrals = ['lightgrey', 'grey', 'black']
const primary = [ '#C2E778', '#A8D555', '#6C9715']
const fillColor = (type, arr) => type === 'wages' ? arr[0] : type === 'retirement' ? arr[1] : arr[2]
return d.decile === selectedSegment ? fillColor(d.type, primary) : fillColor(d.type, neutrals)
}
Insert cell
Insert cell
Select a data source…
Type Chart, then Shift-Enter. Ctrl-space for more options.

Insert cell
fig3_data@3.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
chartData = incomeData.map(d => ({
income: format(d.wages + d.investments + d.retirement ),
CO2: format(d.CO2_wages + d.CO2_investments + d.CO2_retirement),
investments: format(d.investments),
retirement: format(d.retirement),
CO2_investments: format(d.CO2_investments),
CO2_retirement: format(d.CO2_retirement),
wages: format(d.wages),
CO2_wages: format(d.CO2_wages)
}) )
.sort((a, b) => a.income - b.income)
.filter(d => d.income > 0)
Insert cell
format = (n) => +(n).toFixed(5)
Insert cell
thresholds = d3.range(thresholdIncrement, 1 + thresholdIncrement, thresholdIncrement).map(d => +(d).toFixed(2))
Insert cell
indeces = [0, ...thresholds.map(x => d3.quantileIndex(chartData.map(d => d.income), x))]
Insert cell
sumData = {
const results = []
for (let i = 1; i < indeces.length; i++) {
const slice = chartData.slice(indeces[i - 1], indeces[i])
const sumInvest = d3.sum(slice, d => d.CO2_investments)
const sumRetirement = d3.sum(slice, d => d.CO2_retirement)
const sumWages = d3.sum(slice, d => d.CO2_wages)
results.push({decile: +(i * thresholdIncrement).toFixed(2), investments: sumInvest, retirement: sumRetirement, wages: sumWages })
}

return results
}
Insert cell
mekkoData = {
const results = []
sumData.forEach(d => {
results.push([{decile: d.decile, type: 'investments', CO2: +d.investments.toFixed(5) },
{decile: d.decile, type: 'retirement', CO2: +d.retirement.toFixed(5) },
{decile: d.decile, type: 'wages', CO2: +d.wages.toFixed(5) } ])
})

return results.flat(2)
}
Insert cell
function floor(n) {
const pct = Math.round(n * 100)
return pct % 2 === 0 ? n : +(n + .01).toFixed(2)
}
Insert cell
floor(.17)
Insert cell
percentile = (arr, val) => {
let count = 0;
arr.forEach(v => {
if (v < val) {
count++;
} else if (v == val) {
count += 0.5;
}
});
return 100 * count / arr.length;
}

Insert cell
import {marimekko} from '@observablehq/plot-marimekko'
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more