Published
Edited
Dec 30, 2021
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
chart_data = signature_matrix_tagged
.map(d => ({
...d,
cluster: d.cluster.toString(),
leave:
(brexit.find(b => d.ons_code === b.ons_code) ||
{ leave: 0 })
.leave
}))
Insert cell
colour_scheme = d3.schemeCategory10
Insert cell
petitions_by_cluster =
d3.zip(
d3.transpose(
signature_matrix_tagged
.map(d => d.signatures_freq))
.map(d => d3.mean(d)),
d3.transpose(
[...d3.group(signature_matrix_tagged, d => d.cluster)]
.map(d => d3.transpose(d[1].map(d => d.signatures_freq))
.map(d => d3.mean(d)))))
.map((p, p_i) => ({
petition_id: petitions[p_i].id,
action: petitions[p_i].attributes.action,
...Object.fromEntries(
p[1].map((c, c_i) => ([
`Cluster ${c_i}`,
p[0] - c
]))
)
}))
Insert cell
constituencies =
FileAttachment("Westminster_Parliamentary_Constituencies_(December_2019)_Boundaries_UK_BUC.geojson")
.json()
.then(d => ({
...d,
features: d
.features
.map(f => ({
type: f.type,
properties: {
...f.properties,
cluster: (
signature_matrix_tagged
.find(c => f.properties.pcon19cd === c.ons_code) || { cluster: null })
.cluster
},
geometry: f.geometry
}))
.filter(f => /^E|W/.test(f.properties.pcon19cd))
}))
Insert cell
signature_matrix_tagged = d3.sort(
[...d3.group(
signature_matrix
.map((s, i) => ({
...s,
cluster: clusters.idxs[i]
})),
d => d.cluster)]
.flatMap((c, i) => c[1].flatMap(e => ({...e, cluster: i}))))
Insert cell
clusters = skmeans.default(signature_matrix.map(d => d.signatures_freq), n_clusters)
Insert cell
signature_matrix = voters
.map(v => {
const constituency_signatures = signatures_filtered.filter(d => d.ons_code === v.ons_code);
const signatures_freq =
[...d3.union(
signatures_filtered
.map(p => p.petition_id))
]
.map(d => constituency_signatures.find(s => s.petition_id === d) || { signature_count: 0 })
.map(d => d.signature_count / v.voters)
return {
ons_code: v.ons_code,
name: v.name,
signatures_freq: signatures_freq
}
})
Insert cell
signatures_filtered = signatures.filter(p => allowed_petitions.includes(p.petition_id))
Insert cell
allowed_petitions = [...d3.rollup(
signatures, v => d3.deviation(v, d => d.signature_count),
d => d.petition_id)]
.filter(d => d[1] <= max_sdev).map(d => d[0])
Insert cell
max_sdev = d3.quantile(
[...d3.rollup(
signatures, v => d3.deviation(v, d => d.signature_count),
d => d.petition_id)]
.map(d => d[1]),
.9)
Insert cell
signatures =
Promise.all(
petitions
.map(d =>
fetch(`https://petition.parliament.uk/petitions/${d.id}.json`)
.then(r => r.json())
))
.then(d => d
.flatMap(p => p
.data
.attributes
.signatures_by_constituency
.map(s => ({
petition_id: p.data.id,
ons_code: s.ons_code,
signature_count: s.signature_count
}))
))
Insert cell
petitions =
Promise.all(
Array(n_petitions / 50)
.fill()
.map((e, i) =>
fetch(`https://petition.parliament.uk/petitions.json?page=${i + 1}&state=all&topic=covid-19`)
.then(r => r.json())
.then(d => d.data)
))
.then(p => p
.flat()
.filter(p => !excluded_petitions.split(",").includes(p.id.toString()))
.filter(p => petition_years.includes((new Date(p.attributes.opened_at)).getFullYear()))
)
Insert cell
population = FileAttachment("sape23dt7mid2020parliconsyoaestimatesunformatted.xlsx")
.xlsx()
.then(w => w.sheet("Mid-2020 Persons", { range: "A6:C578" }))
.then(s => s
.map(d => ({
ons_code: d["A"],
name: d["B"],
population: d["C"]
}))
)
Insert cell
voters = FileAttachment("localauthorityandparliamentaryregistrationsandattainers.xlsx")
.xlsx()
.then(w => w.sheet("Table 2", { range: "A3:F657" }))
.then(s => s
.map(d => ({
ons_code: d.A,
name: d.B,
voters: d.F
}))
)
Insert cell
brexit = fetch("https://docs.google.com/spreadsheets/d/1wTK5dV2_YjCMsUYlwg0l48uWWf44sKgG8uFVMv5OWlA/export?format=csv")
.then(r => r.text())
.then(d => d3.csvParse(d)
.map(d => ({
ons_code: d.PCON11CD,
leave: parseFloat(d["Figure to use"])
}))
)
Insert cell
skmeans = import("https://cdn.skypack.dev/skmeans")
Insert cell
import { Plot } from "@fil/plot-regression"
Insert cell
import { Legend } from "@d3/color-legend"
Insert cell
mapboxgl = {
const gl = await require("mapbox-gl@1");
if (!gl.accessToken) {
gl.accessToken = "pk.eyJ1IjoiZ2xvYmFsd2l0bmVzczIiLCJhIjoiY2tvYTBucTRkMmt6NTJ3bHAzaW1yZzVoZiJ9.EpQeIN2d3kpEl0yEDSZXcQ";
const href = await require.resolve("mapbox-gl@1/dist/mapbox-gl.css");
document.head.appendChild(html`<link href=${href} rel=stylesheet>`);
}
return gl;
}
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