Published
Edited
Jul 5, 2021
14 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof tb1 = aq.from(data)
.derive({ Class_c:
d => op.recode(d.Class, {'First Class': '1', 'Second Class': '2', 'Third Class': '3', 'Crew' :'C' })})
// recode to sort in a more natural way
.groupby('Sex', 'Age', 'Class')
.derive({p_fate: d => d.value / op.sum(d.value)})
.derive({key3: d => d.Sex + ' / ' + d.Age + ' / ' + d.Class })
.orderby(aq.desc('Survived'))
.view()
Insert cell
// tell apart females and males
viewof tb2 = tb1.params({gutter1})
.groupby('Sex')
.rollup({value: op.sum('value')})
.orderby('Sex')
.derive({p_sex: (d,$) => (1-$.gutter1) * d.value / op.sum(d.value) })
.derive({x2: aq.rolling((d,$) => op.sum(d.p_sex) + (op.row_number() == 1 ? 0 : $.gutter1)) })
.derive({x1: (d,$) => op.lag(d.x2, 1, 0) + (op.row_number() == 1 ? 0 : $.gutter1) })
.select(aq.not(['value'])).relocate('x1',{before:'x2'})
.view()
Insert cell
viewof tb3 = {
let gutter2 = gutter1 * chart_width / chart_height
return tb1.params({gutter2})
.groupby('Sex', 'Age')
.rollup({value: op.sum('value')})
.orderby('Age')
.groupby('Sex')
.derive({p: (d,$) => (1 - $.gutter2) * d.value/op.sum(d.value)})
.derive({y2: aq.rolling((d,$) => op.sum(d.p) + (op.row_number() == 1 ? 0 : $.gutter2))})
.derive({y1: (d,$) => op.lag(d.y2, 1, 0) + (op.row_number() == 1 ? 0 : $.gutter2) })
.join(tb2, 'Sex')
.orderby('Sex', 'Age')
.select(aq.not(['value','p'])).relocate('y1',{before:'y2'}).relocate(['y1','y2'],{after:'x2'})
.view()
}
Insert cell
// introducing Class dimension
viewof tb4 = tb1.groupby('Sex', 'Age', 'Class_c', 'Class')
.rollup({value: op.sum('value')})
.groupby('Sex', 'Age')
.derive({p: d => d.value / op.sum(d.value)})
.orderby('Sex', 'Age', 'Class_c')
.derive({_x2: aq.rolling(d => op.sum(d.p))})
.derive({_x1: d => op.lag(d._x2, 1, 0) })
.select(aq.not(['p'])).relocate('_x1',{before:'_x2'})
.view()
Insert cell
viewof tb5 = tb3.join(tb4)
.derive({X1: d => d.x1 + d._x1*(d.x2 - d.x1)})
.derive({X2: d => d.x1 + d._x2*(d.x2 - d.x1)})
.view()
Insert cell
viewof tb6 = tb1.groupby('Sex', 'Age', 'Class_c')
.orderby('Sex', 'Age', 'Class_c')
.filter(d => d.Survived == 'Survived')
.rename({value:'survived'})
.select(aq.not(['key3', 'Survived']))
.join(tb5)
.derive({Y1: d => d.y1 })
.derive({Y2: d => d.y1 + d.p_fate*(d.y2 - d.y1)})
.view()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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