Public
Edited
May 17, 2023
Insert cell
Insert cell
csubs = (await FileAttachment("csub-thefts-by-state-and-loss-type.csv").csv()).map(d=>({
...d,
period:d3.utcParse('%Y')(d.period),
count:+d.count
}))
Insert cell
csubs
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
count_by_state = d3.rollup(csubs, v => d3.sum(v, d => d.count), d => d.state)
Insert cell
count_by_state.get('FL')
Insert cell
Plot.plot({
color:{legend:true},
marks:[
Plot.lineY(csubs, Plot.groupX({y: "sum"}, {x: "period", y: "count", stroke:'loss_type'})
)
]
})
Insert cell
transit = csubs.filter(d=>d.loss_type=='LOSS IN TRANSIT')
Insert cell
transit.map(d=>({
...d,
pop: state_pop.filter(v=> v.STATE == +statetofips[d.state])[0]
}))
Insert cell
viewof overlap = Inputs.range([0, 9], {step: 0.1, label: "Overlap"})
Insert cell
Plot.plot({
// height: 700,
// width,
x: {axis: "top"},
y: {domain: [0, d3.max(transit, d => d.count) / (overlap + 1)]},

axis:null,
marks:[
Plot.areaY(transit, {x:'period', y:'count', fy:'state', curve: "basis", fill: "#ddd"}),
Plot.lineY(transit, {x:'period', y:'count', fy:'state', curve: "basis", stroke: "#000"}),
Plot.text(transit, Plot.selectFirst({x: d3.min(transit, d => d.period), fy: "state", text: "state", dx: -3, frameAnchor: "right"}))


]
})
Insert cell
NST-EST2022-ALLDATA.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
state_pop = nstEst2022Alldata.map(d=>({
STATE:d.STATE,
POP:d.POPESTIMATE2022
})).filter(d=>d.STATE!=0)
Insert cell
csubs.state.get('FL')
Insert cell
Plot.plot({
projection: "albers-usa",
width,
color:{legend:true},
marks: [
Plot.geo(nation),
Plot.geo(states, { strokeOpacity: 0.25 , fill: d=>count_by_state.get(fipstostate[d.id])}),
]
})
Insert cell
fipstostate[10]
Insert cell
+statetofips['AL']
Insert cell
statetofips = FileAttachment("stateCodeToFips@1.json").json()
Insert cell
fipstostate = Object.fromEntries(
Object.entries(
(await FileAttachment("stateCodeToFips@1.json").json())
).map(a => a.reverse())
)
Insert cell
states
Insert cell
fipstostate1 = FileAttachment("fipsToState.json").json()
Insert cell
import {us} from "@observablehq/us-geographic-data"
Insert cell
nation = topojson.feature(us, us.objects.nation)
Insert cell
counties = topojson.feature(us, us.objects.counties)
Insert cell
states = topojson.feature(us, us.objects.states)
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