Published
Edited
Nov 11, 2020
4 stars
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 Cartogram
Election 2020 Vote Count Speed CartogramWhat’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
import { president, presidentByState } from "1342cadc9354cc67"
Insert cell
parsed = Array.from(presidentByState.keys()).map(parseStateData)
Insert cell
parsed.find(d => d[0] === "GA")[1]
Insert cell
a = presidentByState.get("MA").candidates.find(d => d.party_id === "republican")
Insert cell
b = presidentByState.get("MA").candidates.find(d => d.party_id === "democrat")
Insert cell
parseStateData = key => {
const state = presidentByState.get(key);

const margins = state.timeseries
.filter(({ votes }) => votes)
.map(({ votes, vote_shares, timestamp }) => ({
a: Math.round(vote_shares[a.candidate_key] * votes),
b: Math.round(vote_shares[b.candidate_key] * votes),
timestamp: new Date(timestamp)
}))
.map(({ a, b, timestamp }, i, arr) =>
!i
? { da: a, db: b, timestamp }
: { da: a - arr[i - 1].a, db: b - arr[i - 1].b, timestamp }
)
.map(({ da, db, timestamp }) => ({ margin: da - db, timestamp, key }))
.filter(
({ timestamp }) =>
xUpperBoundOption === "mostRecent" ||
(timestamp >= electionNight[0] && timestamp <= electionNight[1])
);

const marginsArrays = margins.map(({ margin, timestamp }) => [
+timestamp,
margin
]);

const corr =
marginsArrays.length > 2
? stats.sampleCorrelation(...d3.transpose(marginsArrays))
: 0;

const regressionFn = stats.linearRegressionLine(
stats.linearRegression(marginsArrays)
);

const regressionLine = d3
.extent(margins, d => d.timestamp)
.map(d => ({ timestamp: d, margin: regressionFn(+d) }));

return [key, { margins, marginsArrays, corr, regressionLine }];
}
Insert cell
Insert cell
line = d3
.line()
.x(d => x(d.timestamp))
.y(d => y(d.margin))
Insert cell
callCircleRadius = 2.5
Insert cell
electionNight = [
new Date("2020-11-03T23:00:00Z"),
new Date("2020-11-04T08:00:00Z")
]
Insert cell
x = d3.scaleTime(
xUpperBoundOption === "electionNight"
? electionNight
: d3.extent(
parsed.flatMap(([, { margins }]) => margins.map(d => d.timestamp))
),
[0, cellInner]
)
Insert cell
y = d3.scaleLinear(yDomain, [cellInner, 0]).clamp(true)
Insert cell
yDomain = {
const allMargins = parsed.flatMap(([, { margins }]) =>
margins.map(d => d.margin)
);
const extent = [0.01, 0.99].map(p => Math.abs(d3.quantile(allMargins, p)));
const max = Math.max(...extent);
return [-max, max];
}
Insert cell
color = d3.scaleLinear([-maxCorr, 0, maxCorr], ["black", "#ccc", "black"])
Insert cell
maxCorr = Math.max(...parsed.map(([, { corr }]) => Math.abs(corr)))
Insert cell
red = "#dd2c35"
Insert cell
blue = "#0080c9"
Insert cell
function halo(text) {
text
.select(function() {
return this.parentNode.insertBefore(this.cloneNode(true), this);
})
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-width", 4)
.attr("stroke-linejoin", "round");
}
Insert cell
Insert cell
legend = g =>
g
.attr("transform", `translate(${cellPadding},${cellPadding})`)
.call(g =>
g
.append("rect")
.attr("width", cellInner)
.attr("height", cellInner)
.attr("fill", "#eee")
.attr("opacity", 0.5)
)
.call(g =>
g
.append("path")
.attr("stroke", "black")
.attr("fill", "none")
.attr(
"d",
`M 0 ${cellInner / 2 +
0.5} H ${cellInner} M ${cellInner} ${cellInner} V 0`
)
)
.call(xAxis)
.call(yAxis)
.call(colorLegend)
Insert cell
xAxis = g =>
g
.append("g")
.attr("transform", `translate(0, ${y(0)})`)
.call(
d3
.axisBottom(x)
.ticks(4)
.tickFormat(d =>
d[xUpperBoundOption === "mostRecent" ? "getDate" : "getHours"]()
)
)
.call(g => g.select(".domain").remove())
.call(g =>
g
.append("text")
.attr("fill", "black")
.attr("x", cellInner / 2)
.attr("dy", "2.5em")
.attr("text-anchor", "middle")
.text(
x.domain()[0].toLocaleString("default", {
month: "long",
day: xUpperBoundOption === "mostRecent" ? undefined : "numeric",
year: "numeric"
})
)
)
Insert cell
yAxis = g =>
g
.append("g")
.attr("transform", `translate(${cellInner}, 0)`)
.call(
d3
.axisRight(y)
.ticks(3)
.tickFormat(
d =>
`Net ${d3.format(",")(Math.abs(d))} ${
d > 0
? "for " + a.last_name
: d < 0
? "for " + b.last_name
: "votes"
}`
)
)
.call(g => g.select(".domain").remove())
Insert cell
colorLegend = g =>
g
.append("g")
.attr("transform", `translate(${3 * cellSize}, ${y(0)})`)
.selectAll("g.color-legend")
.data(color.domain())
.join("g")
.attr("transform", (d, i) => `translate(0, ${(i - 1) * 20})`)
.call(g =>
g
.append("path")
.attr("d", d => `M -4 ${d * 4} 4 ${-d * 4}`)
.attr("stroke", d => color(d))
.attr("stroke-width", 1.75)
)
.call(g =>
g
.append("text")
.attr("dy", "0.31em")
.attr("dx", "1em")
.text(d =>
d === 0
? "No correlation between when vote was counted and candidate favored"
: "Votes counted later tended toward " + (d > 0 ? a : b).last_name
)
)
Insert cell
Insert cell
position = grid`
, , , , , , , , , ,ME
, , , , ,WI, , , ,VT,NH
WA,ID,MT,ND,MN,IL,MI, ,NY,MA,
OR,NV,WY,SD,IA,IN,OH,PA,NJ,CT,RI
CA,UT,CO,NE,MO,KY,WV,VA,MD,DE,
,AZ,NM,KS,AR,TN,NC,SC,DC, ,
, , ,OK,LA,MS,AL,GA, , ,
HI,AK, ,TX, , , , ,FL, , `
Insert cell
function grid() {
const positionById = new Map;
d3.csvParseRows(String.raw.apply(String, arguments).replace(/^\n|\n$/g, ""), (row, j) => {
row.forEach((id, i) => {
if (id = id.trim()) {
positionById.set(id, [i, j]);
}
});
});
return positionById;
}
Insert cell
gridWidth = d3.max(position, ([, [i]]) => i) + 1
Insert cell
gridHeight = d3.max(position, ([, [, j]]) => j) + 1
Insert cell
cellSize = 80
Insert cell
cellPadding = 1
Insert cell
cellInner = cellSize - cellPadding * 2
Insert cell
svgPadding = 5
Insert cell
Insert cell
d3 = require("d3@6")
Insert cell
stats = require("simple-statistics")
Insert cell
import { radio } from "@jashkenas/inputs"
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