Published
Edited
Nov 19, 2020
3 stars
Insert cell
Insert cell
Insert cell
chart = vl
.data(data)
.encode(
vl
.x()
.fieldQ("x")
.axis({ title: "Votes counted", format: "%" }),
vl
.y()
.fieldQ("y")
.axis({
title: `Share of votes for ${republican.last_name} in ${state_id}`,
format: "%"
})
)
.layer(
vl
.markArea({ clip: true })
.data(endzoneR)
.encode(
vl.x().fieldQ("x"),
vl
.y()
.fieldQ("y")
.scale({ domain }),
vl.y2().fieldQ("y2"),
vl.opacity().value(.5),
vl.color().value("#dd2c35")
),
vl
.markArea({ clip: true })
.data(endzoneD)
.encode(
vl.x().fieldQ("x"),
vl
.y()
.fieldQ("y")
.scale({ domain }),
vl.y2().fieldQ("y2"),
vl.opacity().value(.5),
vl.color().value("#0080c9")
),
vl.markLine().encode(vl.color().value("black"))
)
.render()
Insert cell
race = president.data.races.find(d => d.state_id === state_id)
Insert cell
republican = race.candidates.find(d => d.party_id === "republican")
Insert cell
democrat = race.candidates.find(d => d.party_id === "democrat")
Insert cell
data = race.timeseries
.slice(1) // nyt data has spurious first point
.map(d => ({
y:
d.vote_shares[republican.candidate_key] /
(d.vote_shares[republican.candidate_key] +
d.vote_shares[democrat.candidate_key]),
x: d.eevp / 100
}))
Insert cell
domain = [0, 1]
Insert cell
contour = z => y => (y === 0 ? null : (0.5 + (y - 1) * z) / y)
Insert cell
endzoneR = range(domain, 20).map(x => ({ x, y: contour(0)(x), y2: 1 }))
Insert cell
endzoneD = range(domain, 20).map(x => ({ x, y: contour(1)(x), y2: 0 }))
Insert cell
range = ([x0, x1], n = 20) => [
...Array.from({ length: n }, (_, i) => x0 + ((x1 - x0) * i) / n),
x1
]
Insert cell
import {president} from "@tophtucker/election-2020-data"
Insert cell
import {select} from "@jashkenas/inputs"
Insert cell
import { vl } from "@vega/vega-lite-api"
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