Public
Edited
Jan 19, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = emissions
Insert cell
data
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
viewof country = Inputs.select(sortedEmissionsByCountry, {label: "Country:"})
Insert cell
Plot.plot({
subtitle: "Plot auxiliar",
width,
y: {
grid: true
},
marks: [
Plot.barY(country.filter(d => d.year > 1970), {y: (d) => d.total_ghg == '' ? NaN : Number(d.total_ghg), x: "year", tip: true}),
]
})

Insert cell
entriesArray = Array.from(sortedEmissionsByCountry.entries())
Insert cell
f1 = entriesArray
.flatMap(([key, values]) => values.filter(entry => entry.year === Years))
Insert cell
transformedData = f1.map(entry => {
return {
Country: entry.country,
ghg: entry.total_ghg
};
});
Insert cell
function transformData(sortedEmissionsByCountry) {

const entriesArray = Array.from(sortedEmissionsByCountry.entries());
const filteredData = entriesArray.flatMap(([key, values]) => values.filter(entry => entry.year === Years));

const transformedData = filteredData.map(entry => {
return {
Country: entry.country,
ghg: entry.total_ghg
};
});
return transformedData;
}
Insert cell
Insert cell
Insert cell
chart1 = embed({
"$schema": "https://vega.github.io/schema/vega/v4.json",
"width": 1500,
"height": 100,
"data": [
{
"name": "places",
"values": toyData
}
],
"scales": [
{
"name": "xscale",
"type": "point",
"domain": {"data": "places", "field": "Country"},
"range": "width"
},
{
"name": "sizescale",
"type": "linear",
"domain": {"data": "places", "field": "ghg"},
"range": [0, 3000]
}
],

"axes": [
{ "orient": "bottom", "scale": "xscale" }
],

"marks": [
{
"type": "symbol",
"from": {"data": "places"},
"encode": {
"enter": {
"fill": {"value": "seagreen"},
"size": {"scale": "sizescale", "field": "ghg"},
"x": {"scale": "xscale", "field": "Country"},
"y": {"value": 50}
},
"update": {
"tooltip": {
"signal": "{Country: datum.Country, GHG: datum.ghg}"
}
}
}
}
]
})

Insert cell
embed = require('vega-embed@3')
Insert cell
anos= entriesArray.flatMap(([key, values]) => values.filter(entry => entry.year === 1990 || (entry.year >= 2008 && entry.year <= 2012)))
Insert cell
anos1= entriesArray.flatMap(([key, values]) => values.filter(entry => entry.year === 1990 || (entry.year >= 2008 && entry.year <= 2020)))
Insert cell
transformedData_all = anos.map(entry => {
return {
Country: entry.country,
year: entry.year,
ghg: entry.total_ghg,
}
})
Insert cell
transformedData_all1 = anos1.map(entry => {
return {
Country: entry.country,
year: entry.year,
ghg: entry.total_ghg,
}
})
Insert cell
function calcularPromedioGHG(data) {
const promedioPorPais = {};

data.forEach(entry => {
if (!promedioPorPais[entry.Country]) {
promedioPorPais[entry.Country] = {
total: 0,
count: 0,
};
}
if (entry.year >= 2008 && entry.year <= 2012) {
promedioPorPais[entry.Country].total += entry.ghg;
promedioPorPais[entry.Country].count += 1;
}
});
const resultados = Object.keys(promedioPorPais).map(pais => {
const { total, count } = promedioPorPais[pais];
const ghgPromedio = count > 0 ? total / count : 0;
return {
Country: pais,
ghg1990: data.find(entry => entry.Country === pais && entry.year === 1990)?.ghg || 0,
ghgPromedio: ghgPromedio.toFixed(2),
};
});
return resultados;
}
Insert cell
function calcularPromedioGHG1(data) {
const promedioPorPais = {};

data.forEach(entry => {
if (!promedioPorPais[entry.Country]) {
promedioPorPais[entry.Country] = {
total_08_12: 0,
count_08_12: 0,
total_13_20: 0,
count_13_20: 0,
ghg1990: 0,
};
}
if (entry.year === 1990) {
promedioPorPais[entry.Country].ghg1990 = entry.ghg;
} else if (entry.year >= 2008 && entry.year <= 2012) {
promedioPorPais[entry.Country].total_08_12 += entry.ghg;
promedioPorPais[entry.Country].count_08_12 += 1;
} else if (entry.year >= 2013 && entry.year <= 2020) {
promedioPorPais[entry.Country].total_13_20 += entry.ghg;
promedioPorPais[entry.Country].count_13_20 += 1;
}
});

const resultados = Object.keys(promedioPorPais).map(pais => {
const { total_08_12, count_08_12, total_13_20, count_13_20, ghg1990 } = promedioPorPais[pais];
const ghgPromedio_08_12 = count_08_12 > 0 ? total_08_12 / count_08_12 : 0;
const ghgPromedio_13_20 = count_13_20 > 0 ? total_13_20 / count_13_20 : 0;

return {
Country: pais,
ghg1990: ghg1990,
ghgPromedio_08_12: ghgPromedio_08_12.toFixed(2),
ghgPromedio_13_20: ghgPromedio_13_20.toFixed(2),
};
});
return resultados;
}

Insert cell
Country_90_promedio = calcularPromedioGHG(transformedData_all)
Insert cell
Country_90_promedio1 = calcularPromedioGHG1(transformedData_all1)
Insert cell
Informacion_plot= Country_90_promedio.map(entry => {
const { Country, ghg1990, ghgPromedio } = entry;
const porcentajeReduccion = ghg1990 !== 0
? ((ghgPromedio - ghg1990) / ghg1990) * 100
: 0;
const matchingEntry = filteredResults_A.find(filteredEntry => filteredEntry.country === Country);

const burdenSharingPercentage = matchingEntry ? matchingEntry['Burden_Sharing(%)'] : null;

return {
Country: Country,
ghg_1990: ghg1990,
ghgAverage_2008_2012: ghgPromedio,
percentage: porcentajeReduccion.toFixed(2),
'Burden_Sharing(%)': burdenSharingPercentage,
};
});
Insert cell
Plot.plot({
height: 1000,
width: 500,
marginLeft: 100,
grid: true,
y: {
label: null,
},
marks: [
Plot.dot(Informacion_plot1,{
y: "Country",
r: d => d.ghg_1990,
fill: "currentColor",
color: "black",
}
)
]
})
Insert cell
Informacion_plot1= Country_90_promedio1.map(entry => {
const { Country, ghg1990, ghgPromedio_08_12, ghgPromedio_13_20 } = entry;
const porcentajeReduccion_08_12 = ghg1990 !== 0
? ((ghgPromedio_08_12 - ghg1990) / ghg1990) * 100
: 0;
const porcentajeReduccion_13_20 = ghg1990 !== 0
? ((ghgPromedio_13_20 - ghg1990) / ghg1990) * 100
: 0;
const matchingEntry = filteredResults_A.find(filteredEntry => filteredEntry.country === Country);

const burdenSharingPercentage = matchingEntry ? matchingEntry['Burden_Sharing(%)'] : null;
const burdenSharingPercentage_1 = matchingEntry ? matchingEntry['Goal_2020(%)'] : null;

return {
Country: Country,
ghg_1990: ghg1990,
ghgAverage_2008_2012: ghgPromedio_08_12,
percentage_08_12: porcentajeReduccion_08_12.toFixed(2),
ghgAverage_2013_2020: ghgPromedio_13_20,
percentage_13_20: porcentajeReduccion_13_20.toFixed(2),
'Burden_Sharing(%)': burdenSharingPercentage,
'Goal_2020(%)': burdenSharingPercentage_1,
};
});
Insert cell
Plot.plot({
height: 500,
width: 1500,
y: {
grid: true,
type: "linear",
},
marks: [
Plot.barY(Informacion_plot, {
x: "Country",
y: "percentage",
sort: { x: "-y" },
fill: "#4CAF50",
}),
Plot.ruleY([0]),
Plot.dot(Informacion_plot, {
x: "Country",
y: "Burden_Sharing(%)",
fill: "currentColor",
color: "black",
size: 6,
}),
],
});
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
height: 780,
width: 1000,
marginLeft: 100,
grid: true,
title: `Performance of EU countries against the GHG reduction of the Kyoto Protocol (Period ${option})`,
subtitle: "In blue, the countries that achieved the reduction goal; in red, that did not meet it.",
x: {
axis: "top",
round: true,
label: `Avg GHG dec (%) ${option}`,
labelAnchor: "center",
type: "linear",
},
y: {
label: null,
},
marks: [
Plot.barX(Informacion_plot1, {
y: "Country",
x: x_b,
sort: { y: "-x" },
fill: d => {
if (d.Country === "European Union (27)")
{return "#a2a2a2"}
else if (d[x_b] >= 0) {
return (d[x_b]) >= (d[x_d]) ? "#e15759" : "#4e79a7";
} else {
return Math.abs(d[x_b]) < Math.abs(d[x_d]) ? "#e15759" : "#4e79a7";
}
},
channels: {'GHG 1990': "ghg_1990", 'GHG Avg over time period': Selec, 'Goal (%)': x_d},
tip: {
format: {
y: true,
'Goal (%)': true,
x: true,
'GHG 1990': true,
'GHG Avg over time period': true,
fill: false,
},
}
}),
Plot.dot(Informacion_plot1, {
y: "Country",
x: x_d,
fill: "currentColor",
color: "black",
size: 6,
}),
Plot.ruleX([0]),
],
});
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
//viewof summary_raw_data = SummaryTable(emissions_raw, {label: "Emissions Data"})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3.max(heatMapValuesStruct[1], (d) => d.ghg);
Insert cell
d3.min(heatMapValuesStruct, (d, i) => d3.min(heatMapValuesStruct[i], (d) => d.ghg));
Insert cell
d3.max(heatMapValuesStruct[0], (d) => d[heatmapChosenData])
Insert cell
Insert cell
function accessHeatMapValue(index) {
debugger
const countryIdx = parseInt(index.slice(0, 2));
const yearIdx = parseInt(index.slice(-2));

return heatMapValuesStruct[countryIdx][yearIdx][heatmapChosenData];
}
Insert cell
Insert cell
accessHeatMapValue("0501")
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
//filteredResults_A = data1
// .filter(entry => entry.UE === 'Yes' && entry.country !== 'Romania')

filteredResults_A = data1
.filter(entry => entry.UE === 'Yes')
Insert cell
data1
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
data_UE_World = {
const countriesInData4 = data2.map(item => item.country);

const filteredData4 = emissionsDataset .filter(item => countriesInData4.includes(item.country));

return filteredData4;
}

Insert cell
data_UE_World
select country,year,sum(co2) from data_UE_World
group by country,year
Insert cell
data_UE = {
const countriesInData2 = data2.map(item => item.country);

const filteredData = emissions.filter(item => countriesInData2.includes(item.country));
console.log(filteredData);

return filteredData;
}
Insert cell
data_UE
SELECT
YEAR,
COUNTRY,
CO2,
CO2_1990::DOUBLE AS CO2_1990,
CO2_1990*0.95 AS REDUCTION_CO2_1990
FROM
(SELECT
year,
country,
co2::DOUBLE AS CO2,
(SELECT co2::double FROM data_UE t2 WHERE t2.country = t1.country AND t2.year = 1990) AS co2_1990
FROM
data_UE t1
where co2 <> '')

--WHERE country = 'Austria'
Insert cell
kioto_UE
Insert cell
CO2_1990 = data_UE.filter(d => d.year === "1990")
.map((d) => {
return {
country: d.country,
co2_1990: d.co2,
reduction_co2_1990: d.co2 * 0.95
}
});

Insert cell
kioto_UE_2 = {
const dataZ = [];
kioto_UE
.forEach((yearData) => {
const year = parseInt(yearData.year);
const quinquennium = year + (5 - year % 5);
dataZ.push({
country: yearData.country,
year: year,
quinquennium: quinquennium,
co2: yearData.CO2,
co2_1990: yearData.CO2_1990,
red_co2_1990: yearData.REDUCTION_CO2_1990
});
});
return dataZ;
}
Insert cell
countries_data = d3.group(kioto_UE_2, d => d.country)
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
Insert cell
Insert cell
Insert cell
Insert cell
import {select, slider} from "@jashkenas/inputs"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more