Public
Edited
Nov 27, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Inputs.table(happiness)
Insert cell
Insert cell
World Happiness Report 2022.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
Insert cell
happinessMap = worldHappinessReport2022.map(d => {
var x = d;

x.Happiness_score = +d.Happiness_score;
x. Explained_by_GDP = +d. Explained_by_GDP;
x. Explained_by_Social_support = +d. Explained_by_Social_support;
x. Explained_by_Healthy_life_expectancy = +d. Explained_by_Healthy_life_expectancy;
x. Explained_by_Freedom = +d. Explained_by_Freedom;
x. Explained_by_Generosity = +d. Explained_by_Generosity;
x. Explained_by_trust = +d. Explained_by_trust;

return x;
})
Insert cell
Insert cell
Insert cell
// finding the highest happiness score using d3
{
let Happiness_score = happinessMap.map(function(d){
return +d.Happiness_score;
})
return "The highest happiness score is "+d3.max(Happiness_score)+".";
}
Insert cell
Insert cell
{
let Happiness_score = happinessMap.map(function(d){
return +d.Happiness_score;
})
return "The lowest happiness score is "+d3.min(Happiness_score)+".";
}
Insert cell
Insert cell
Insert cell
{
let Happiness_score = happinessMap.map(function(d){
return +d.Happiness_score;
})
return "The total happiness score is "+d3.sum(Happiness_score)+".";
}
Insert cell
Insert cell
"The median of the happiness score (column) is " + d3.median(happinessMap, d => d.Happiness_score)+".";
Insert cell
Insert cell
Insert cell
Insert cell
aboveOne = happinessMap.filter(function(d){
return d.Explained_by_GDP >= 1
});
Insert cell
"The number of countries that have an extent to which GDP contributes to the calculation of the"
Insert cell
"Happiness Score that is above 1 is "+aboveOne.length+"."
Insert cell
Insert cell
Insert cell
Plot.plot({
y:{grid:true},
marks: [
Plot.rectY(happinessMap, Plot.binX({y: "count"}, {x: "Happiness_score", fill:"steelblue"})),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.rectY(happinessMap, Plot.binX({y: "count"}, {x: "Explained_by_GDP", fill:"steelblue"})),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.rectY(happinessMap, Plot.binX({y: "count"}, {x: "Explained_by_Social_support", fill:"steelblue"})),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.rectY(happinessMap, Plot.binX({y: "count"}, {x: "Explained_by_Healthy_life_expectancy", fill:"steelblue"})),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.rectY(happinessMap, Plot.binX({y: "count"}, {x: "Explained_by_Freedom", fill:"steelblue"})),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.rectY(happinessMap, Plot.binX({y: "count"}, {x: "Explained_by_Generosity", fill:"steelblue"})),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.rectY(happinessMap, Plot.binX({y: "count"}, {x: "Explained_by_trust", fill:"steelblue"})),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Insert cell
Insert cell
import {vl} from '@vega/vega-lite-api'
Insert cell
d3 = require("d3@7")
Insert cell
happiness = d3.csv('https://gist.githubusercontent.com/WinnieS49/e3efb7190508716cd17ec763119fd718/raw/e73da6b36270e0aa9c4771dd33a80a68e2abd091/gistfile1.txt')
Insert cell
Insert cell
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