Public
Edited
Jan 14, 2024
Insert cell
Insert cell
Insert cell
---
## Summary
Insert cell
toc({
title: null,
headers: 'h1',
headers: 'h2',
headers: 'h3',
})
Insert cell
Insert cell
Insert cell
Plot.plot({
title: "CO2 emissions in Europe per country",
subtitle: "Data from 2022",
width: 500,
projection: { type: "conic-conformal", domain: eu_data_map },
color: {
scheme: "YlOrRd",
label: selected_plot.legend,
legend: true
},
marks: [
Plot.geo(eu_countries, { fill: (d) => d.properties[selected_plot.variable], opacity: 1 }),
Plot.geo(other_eu, { fill: "grey" , stroke: "white", opacity: 0.15 }),
Plot.geo(not_esp_ita, {stroke: "black",strokeWidth: 0.5 }),
Plot.geo(esp_ita, {stroke: "black",strokeWidth: 2 }),
Plot.dot(eu_countries, Plot.centroid({ fill: "white", stroke: "black" })),
Plot.tip(eu_countries, Plot.pointer(
Plot.centroid({
stroke: "white",
strokeWidth: 0.125,
tip: true,
channels: {
"Country": d => d.properties.country,
" - CO2": d => d.properties.co2,
" - CO2 per capita": d => d.properties.co2_per_capita
}
})
))
]
});
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const svg = d3
.create("svg")
.attr("width", 1000)
.attr("height", 400)
.attr("viewBox", [-100000, -50000, 200000, 100000])
svg.append('circle').attr('r', bubblePlot[0].energy_per_capita).attr('cx', -bubblePlot[0].energy_per_capita).attr('cy', 0).attr("fill", "green");
svg.append('circle').attr('r', bubblePlot[1].energy_per_capita).attr('cx', bubblePlot[1].energy_per_capita).attr('cy', 0).attr("fill", "red");
return svg.node();
}
Insert cell
Plot.plot({
width: widthBubble,
height: heightBubble,
marginLeft: 80,
marginBottom: 80,
title: "Comparing energy consumption between Spain and Italy",
subtitle: "Difference in per capita yearly energy consumption",
marks: [
Plot.circle(bubblePlot, {
x: -bubblePlot[0].energy_per_capita,
y: 0 ,
r: bubblePlot[0].energy_per_capita,
fill: "Green"},
),
Plot.circle(bubblePlot, {
x: bubblePlot[1].energy_per_capita,
y: 0 ,
r: bubblePlot[1].energy_per_capita,
fill: "Red"},
),
Plot.circle(bubblePlot, {
x: 0,
y: 0 ,
r: 50000,
stroke: "Black"},
)
]
})


Insert cell
d3 = require('d3')
Insert cell
{
const svg = d3
.create("svg")
.attr("width", 500)
.attr("height", 200)
.attr("viewBox", [-100000, -50000, 200000, 100000])
svg.append('circle').attr('r', bubblePlot[0].energy_per_capita).attr('cx', -bubblePlot[0].energy_per_capita).attr('cy', 0).attr("fill", "rgb(156 163 175)");
svg.append('circle').attr('r', bubblePlot[1].energy_per_capita).attr('cx', bubblePlot[1].energy_per_capita).attr('cy', 0).attr("fill", "red");
return svg.node();
}
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
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
europeanData
SELECT
CAST (ed1.year AS Float) AS year,
CAST(ed1.energy_per_capita AS Float) AS epc_italy,
CAST(ed2.energy_per_capita AS Float) AS epc_spain,
CAST(ed2.energy_per_capita AS Float) - CAST(ed1.energy_per_capita AS Float) AS diff
FROM
europeanData ed1
JOIN
europeanData ed2 ON ed1.year = ed2.year
AND ed1.country = 'Italy'
AND ed2.country = 'Spain'
WHERE
ed1.year >= 1965

ORDER BY ed1.year


Insert cell
energy_diff
INSERT INTO energy_diff (year, epc_italy, epc_spain, diff)
VALUES (1999.5, 5428, 5644, 0);
Insert cell
Insert cell
europeanData
SELECT
ed1.year AS year,
CAST(ed1.gdp AS Float) / CAST(ed1.co2 AS Float) / 1000000000 AS ratio_italy,
CAST(ed2.gdp AS Float) / CAST(ed2.co2 AS Float) / 1000000000 AS ratio_spain,
CAST(ed1.gdp AS Float) / 1000000000 AS gdp_italy,
CAST(ed2.gdp AS Float) / 1000000000 AS gdp_spain,
CAST(ed1.co2 AS Float) AS co2_italy,
CAST(ed2.co2 AS Float) AS co2_spain,

FROM
europeanData ed1
JOIN
europeanData ed2 ON ed1.year = ed2.year
AND ed1.country = 'Italy'
AND ed2.country = 'Spain'
WHERE
ed1.year >= 1965 AND ed1.year < 2019


ORDER BY ed1.year

Insert cell
europeanData
SELECT
year AS year,
country AS country,
CAST(gdp AS Float) / 1000000000 AS gdp,
CAST(co2 AS Float) AS co2

FROM
europeanData
WHERE
year >= 1965 AND year < 2019
AND (country = 'Italy' OR country = 'Spain')

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function filterObject(obj, keysToKeep) {
return Object.fromEntries(Object.entries(obj).filter(([key]) => keysToKeep.includes(key)));
}
Insert cell
Insert cell
Insert cell
greens = d3.scaleLinear([1965, 2020], ["#ebf5ec", "#008001"])
Insert cell
reds = d3.scaleLinear([1965, 2020], ["#ffebec", "#fe0000"]);
Insert cell
Insert cell
import {Scrubber} from "@mbostock/scrubber"
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