Published
Edited
Mar 14, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
variablesSummed = {
/* This sums up the values across each variable for each country
The output data looks something like:
[{CO2:"AFG", Variable1:"sum", ... VariableN:"sum"}, {CO2:"ALB", Variable1:"sum", ... VariableN:"sum"}, ...]
*/
let df =[]
let CO2Grouped = Object.values(z.groupBy(r => r.CO2, migration)) //Group migration data by Country of Origin
for (let c in CO2Grouped){ // loop through each CO2
let c_data = {} // create new row for current CO2
c_data.CO2 = CO2Grouped[c][0].CO2 // assign property "CO2"
let c_sums = z.gbSum("Value", z.groupBy(r => r.Variable, CO2Grouped[c])) // gbSum according to Variable

for (let i=0;i<c_sums.length;i++){ // assign each Variable as a property with value=sum
c_data[`${c_sums[i].group}`] = c_sums[i].sum
}
df.push(c_data) // append row to df
}
return df
}
Insert cell
Insert cell
joined = leftjoin_col(variablesSummed, meta, "CO2", "Country Code", "IncomeGroup")
Insert cell
Insert cell
// Your code here
income_group = z.groupBy(r => r.IncomeGroup, joined)
Insert cell
Insert cell
viewof groups = embed({
vconcat: [{
title: "Countries of Origin with Low Income",
repeat: {column: vars},
spec: {
data: {values: income_group['Low income']},
mark: "bar",
encoding: {
x: {
field: {"repeat":"column"},
bin: {maxbins: 12}
},
y: {
aggregate: "count",
type: "quantitative"
}
}
}
},
{
title: "Countries of Origin with Lower Middle Income",
repeat: {column: vars},
spec: {
data: {values: income_group['Lower middle income']},
mark: "bar",
encoding: {
x: {
field: {"repeat":"column"},
bin: {maxbins: 12}
},
y: {
aggregate: "count",
type: "quantitative"
}
}
}
},
{
title: "Countries of Origin with Upper Middle Income",
repeat: {column: vars},
spec: {
data: {values: income_group['Upper middle income']},
mark: "bar",
encoding: {
x: {
field: {"repeat":"column"},
bin: {maxbins: 12}
},
y: {
aggregate: "count",
type: "quantitative"
}
}
}
},
{
title: "Countries of Origin with High Income",
repeat: {column: vars},
spec: {
data: {values: income_group['High income']},
mark: "bar",
encoding: {
x: {
field: {"repeat":"column"},
bin: {maxbins: 12}
},
y: {
aggregate: "count",
type: "quantitative"
}
}
}
}]
})
Insert cell
Insert cell
Insert cell
// Your code here
viewof problem4 = embed({
title: "Normalized Stacked Income Group Bar Charts",
repeat: {column: vars},
spec: {
data: {values: clean_joined},
mark: "bar",
encoding: {
x: {
field: {"repeat":"column"},
bin: {maxbins: 12}
},
y: {
aggregate: "count",
type: "quantitative",
stack: "normalize"
},
color: {
field: "IncomeGroup",
type: "nominal",
}
}
}
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Your code here
viewof problem5 = embed({
width: 500,
height: 500,
title: "Scatter plot of immigration: Country of Birth vs. GDP per capita 2017",
data: {values: data_1},
mark: "point",
encoding: {
x: {
field: "Inflows of foreign population by nationality",
type: "quantitative",
scale: {"domain": [0,10000000]}
},
y: {
field: "2017",
type: "quantitative",
},
color: {
field: "Region",
type: "nominal",
}
}
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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