@@ -2,26 +2,11 @@ import * as Plot from "@observablehq/plot";
2
2
import * as d3 from "d3" ;
3
3
4
4
export async function youngAdults ( ) {
5
- const ilc_lvps08 = await d3 . csv < any > ( "data/ilc_lvps08.csv" , d3 . autoType ) ;
6
5
const ages = [ "Y16-19" , "Y20-24" , "Y25-29" ] ;
7
6
const geos = [ "SE" , "FR" , "DE" , "TR" , "IT" ] ;
8
- const wider = d3
9
- . flatRollup (
10
- ilc_lvps08 . filter ( ( { age, geo} ) => ages . includes ( age ) && geos . includes ( geo ) ) ,
11
- ( v ) =>
12
- Object . fromEntries (
13
- d3
14
- . index (
15
- v . map ( ( { sex, OBS_VALUE } ) => [ sex , OBS_VALUE ] ) ,
16
- ( d ) => d [ 0 ]
17
- )
18
- . values ( )
19
- ) ,
20
- ( d ) => d . geo ,
21
- ( d ) => d . age ,
22
- ( d ) => d . TIME_PERIOD
23
- )
24
- . map ( ( [ geo , age , year , d ] ) => ( { geo, age, year, ...d } ) ) ;
7
+ const ilc_lvps08 = await d3 . csv < any > ( "data/ilc_lvps08.csv" , ( d ) =>
8
+ ages . includes ( d . age ) && geos . includes ( d . geo ) ? d3 . autoType ( d ) : null
9
+ ) ;
25
10
return Plot . plot ( {
26
11
title : "Share of young adults living with their parents (%)" ,
27
12
subtitle : "…by age and sex. Data: Eurostat" ,
@@ -32,20 +17,28 @@ export async function youngAdults() {
32
17
y : { grid : true , nice : true } ,
33
18
marks : [
34
19
Plot . frame ( ) ,
35
- Plot . differenceY ( wider , {
36
- x : "year" ,
37
- y1 : "F" ,
38
- y2 : "M" ,
39
- sort : { fx : { value : "y" , reduce : "mean" } } ,
40
- fillOpacity : 0.5 ,
41
- negativeFill : "grey" ,
42
- positiveFill : "age" ,
43
- fx : "geo" ,
44
- curve : "basis" ,
45
- z : ( d ) => `${ d . age } , ${ d . geo } ` ,
46
- channels : { M : "M" , F : "F" } ,
47
- tip : true
48
- } )
20
+ Plot . differenceY (
21
+ ilc_lvps08 ,
22
+ Plot . groupX (
23
+ {
24
+ y1 : Plot . find ( ( d ) => d . sex === "F" ) ,
25
+ y2 : Plot . find ( ( d ) => d . sex === "M" ) ,
26
+ positiveFill : "first"
27
+ } ,
28
+ {
29
+ x : "TIME_PERIOD" ,
30
+ y : "OBS_VALUE" ,
31
+ negativeFill : "grey" ,
32
+ positiveFill : "age" ,
33
+ z : "age" ,
34
+ sort : { fx : { value : "y" , reduce : "mean" } } ,
35
+ fillOpacity : 0.5 ,
36
+ fx : "geo" ,
37
+ curve : "basis" ,
38
+ tip : true
39
+ }
40
+ )
41
+ )
49
42
]
50
43
} ) ;
51
44
}
0 commit comments