Published
Edited
Jan 12, 2022
1 star
Election 2020 County Vote Distribution within a State2020 Presidential Election Time Series AnomaliesMontana 2020 Legislative OutcomesDot-density election maps with WebglPennsylvania 2020 General Election Mail Ballot Requests AnalysisElection Night Results2020 Presidential Election Time SeriesUS Electoral College Results 1900 - 2020Facebook Advertising in the 2020 Presidential ElectionUS Presidential Election Results (1976 - 2020)2020 Presidential Election in ChicagoBlue waveElection 2020: How reliable are different results at different stages?Election 2020 Vote–Time Correlation CartogramElection 2020 Vote Count Speed Cartogram
What’s the connection between these two election bar charts?
Spinning counties, November 2020A Better Way to Visualize US Elections 2020 ResultsUS Elections 2020 Results - Deeper LookUS Election 2020Indian Country Today #NativeVote2020Indian Country Today #NativeVote2020As votes are countedTry to impeach this? Challenge accepted!Early Voting Wait Times in Gwinnett County, GeorgiaVOTE LogoDonor Age Distribution of 2020 Contributions by Individuals: Trump vs. Sanders2020: Where Donation $$ EmergeUnique Individual Donors contributing to the 2020 Presidential CandidatesContributions: Trump vs. Biden 2020Changes to polling placesElectoral College Unit ShuffleAs votes come in, what would it take for the trailing candidate to win?Electoral College Decision Tree2004 - 2016 Presidential Margin of Victory per County2020 State Probabilities by Election Model - Shaded Table2020 Presidential Election ForecastsElection Maps: 2016Working With Election Data: EAVS3a. Historical participation in early voting vs. Election Day votingNC Election MapSouth Carolina Early and Absentee Voting DataRace Bar Chart utilitiesNC Congressional DistrictsGrid cartogramsPaths to the White House (Inferred)US Elections 2018How well does population density predict U.S. voting outcomes?
US Election Convention MapElection Data Tutorial2020 Presidential Election Forecasts2016 U.S. presidential election (PEPSI Remix)2016 U.S. presidential election
USPS collection box changesU.S. Geographic DataU.S. County Visualization IdeasRejected Mail Ballots in North Carolina
Also listed in…
Election
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof switchDomain = Inputs.radio(
new Map([
["Show all possibilities", "all"],
["Focus on the more-probable middle", "some"]
]),
{ value: "some" }
)
Insert cell
drawOutcome = () => Math.round(cdfScale(Math.random()))
Insert cell
cdfScale = d3.scaleLinear(cdf.map(d => d[1]), cdf.map(d => d[0]))
Insert cell
bar = function(clinton, trump, total, format, label) {
const marginLeft = 60;
const h = 60;
const w = width - marginLeft;
return svg`<svg width=${width} height="${h}" style="font-family: sans-serif; cursor: default;">
<g transform="translate(${marginLeft * .4}, ${h * .6})">${getLabel(
label,
marginLeft / 3
)}</g>
<g transform="translate(${marginLeft}, 0)">
<text y="2em" fill="${blue}">
<tspan style="font-size: 2em">${format(clinton)}</tspan>
Hillary Clinton
</text>
<text x="${w}" text-anchor="end" y="2em" fill="${red}">
Donald Trump
<tspan style="font-size: 2em">${format(trump)}</tspan>
</text>
<g transform="translate(0, 40)">
<rect
width=${w * (clinton / (clinton + trump))}
height="20"
fill=${blue} />
<rect
width=${w * (trump / total)}
height="20"
fill=${red}
x="${w * (1 - trump / total)}" />
</g>
</g>
</svg>`;
}
Insert cell
getLabel = (letter, radius) => {
const fontSize = radius * 1.5;
const strokeWidth = radius / 8;
const _letter = letter.charAt(0);
const suffix = letter.substring(1);
return svg`<g>
<circle
r="${radius}"
fill="white"
stroke="black"
stroke-width="${strokeWidth}" />
<text
style="font-family: sans-serif; font-size: ${fontSize}px;"
text-anchor="middle"
dy="0.32em"
>
${_letter}
</text>
<text
style="font-family: sans-serif; font-size: ${fontSize}px;"
text-anchor="middle"
dx="0.4em"
dy="0.32em"
>
${suffix}
</text>
</g>`;
}
Insert cell
clintonProb = .714
Insert cell
trumpProb = 1 - clintonProb
Insert cell
clintonVotes = 232
Insert cell
trumpVotes = numberOfElectors - clintonVotes
Insert cell
pct = d3.format(".1%")
Insert cell
blue = "#0080c9"
Insert cell
red = "#dd2c35"
Insert cell
cdf = d3.zip(possibleOutcomes, d3.cumsum(pmf, d => d.prob))
Insert cell
cdfMap = new Map(cdf)
Insert cell
winProb = 1 - cdfMap.get(voteThreshold)
Insert cell
tieProb = pmf.find(d => d.votes === numberOfElectors / 2).prob
Insert cell
loseProb = cdfMap.get(voteThreshold - 1)
Insert cell
normalizeProbs = counts => {
const sum = d3.sum(counts.map(({ count }) => count));
const map = new Map(counts.map(({ votes, count }) => [votes, count]));
return possibleOutcomes.map(votes => ({
votes,
prob: (map.get(votes) || 0) / sum
}));
}
Insert cell
possibleOutcomes = d3.range(numberOfElectors + 1)
Insert cell
probableOutcomes = switchDomain === "all"
? possibleOutcomes
: d3.range(Math.round(randMean - 2 * randSd), randMean + 2 * randSd)
Insert cell
x = d3
.scaleBand(probableOutcomes, [margin.left, width - margin.right])
.paddingOuter(0)
.paddingInner(0)
Insert cell
xContinuous = d3.scaleLinear(
d3.extent(x.domain()),
d3.extent(x.domain()).map(x)
)
Insert cell
y = d3.scaleLinear(
[0, d3.max(Array.from(initialCounts.values()))],
[height - margin.bottom, margin.top]
)
Insert cell
y2 = y.copy().domain([0, 1])
Insert cell
xAxis = g =>
g.attr("transform", `translate(0,${height - margin.bottom})`).call(
d3
.axisBottom(xContinuous)
.ticks(width / 80)
.tickSizeOuter(0)
)
Insert cell
y2Axis = g =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(
d3
.axisLeft(y2)
.ticks(5)
.tickFormat(d3.format(".0%"))
)
.call(g => g.select(".domain").remove())
.call(g =>
g
.select(".tick:last-of-type text")
.clone()
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text("Probability of Clinton getting up to this many votes")
)
Insert cell
_rand = d3.randomNormal(randMean, randSd)
Insert cell
randMean = numberOfElectors / 2
Insert cell
randSd = 2 * Math.sqrt(numberOfElectors)
Insert cell
rand = d =>
Math.min(
Math.max(_rand(), d3.min(possibleOutcomes)),
d3.max(possibleOutcomes)
)
Insert cell
initialCounts = d3.rollup(
d3.range(2000).map(rand),
arr => arr.length,
d => Math.round(d)
)
Insert cell
numberOfElectors = 538
Insert cell
voteThreshold = numberOfElectors / 2 + 1
Insert cell
height = width / 2
Insert cell
margin = ({ top: 10, right: 60, bottom: 20, left: 40 })
Insert cell
d3 = require("d3@6")
Insert cell
import { tweet } from "@mbostock/tweet"
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