Public
Edited
May 12, 2023
Insert cell
Insert cell
dataFile = await FileAttachment("AllControversies_WeighteddDisa_Beeswarm.csv").csv()
Insert cell
data = dataFile.map((d) => {
return {
...d,
position: +d.AvgDW,
type: d.Ranking,
group: d.Controversy,
value: +d.SumT / 250,
name: d.Topics
};
})
Insert cell
viewof table = Inputs.table(data)
Insert cell
maxDisa = d3.max(data, (d)=>d.position)
Insert cell
colorScale = d3.scaleOrdinal(
[...new Set(data.map((d) => d.Ranking))],
d3.schemeTableau10
)
Insert cell
maxTw = d3.max(dataFile, (d)=>+d.SumT)
Insert cell
chart = beeswarm(data, "position", "group", "type", "value", "name")
Insert cell
beeswarm = (data, position, group, type, value, name) => {
const svg = d3.create("svg").attr("viewBox", [0, 0, 900, 700]);

let categories = [...new Set(data.map((d) => d[group]))];

const padding = 1;
const size = { w: 900, h: 600 };
const margin = { top: 20, bottom: 30, left: 130, right: 100 };
const radius = d3
.scaleSqrt()
.domain(d3.extent(data, (d) => d[value]))
.range([2, 40]);
const labelSize = d3
.scaleSqrt()
.domain(d3.extent(data, (d) => d[value]))
.range([2, 20]);
const textRadius = d3
.scaleSqrt()
.domain(d3.extent(data, (d) => d[value]))
.range([2, 10]);

const xScale = d3.scaleLinear().domain([0, maxDisa]).range([0, 700]);
const xAxis = d3.axisBottom().scale(xScale);

// y scale
const yScale = d3
.scaleBand()
.domain(categories)
.range([0, size.h - margin.top - margin.bottom]);
const yAxis = d3.axisLeft().scale(yScale);

data.forEach((d) => {
d.x = xScale(d[position]);
d.y = yScale(d[group]);
});

const box = svg
.append("g")
.attr("class", "box")
.attr("transform", "translate(100, 80)");

const nodes = box
.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("class", "node")
.attr("cy", (d) => d.y)
.attr("cx", (d) => d.x)
.attr("r", (d) => radius(d[value]))
.style("fill", (d) => colorScale(d[type]));

const labels = box
.selectAll("text")
.data(data)
.enter()
.append("text")
.attr("text-anchor", "middle")
.attr("x", (d) => d.x)
.attr("y", (d) => d.y)
.attr("font-size", (d) => textRadius(d[value]))
.text((d) => d.Topics);

let simulation = d3
.forceSimulation()
.force(
"collide",
d3.forceCollide((d) => radius(d[value]) + padding)
)
.force(
"x",
d3.forceX((d) => d.x)
)
.force("y", d3.forceY((d) => d.y).strength(1))
.alphaMin(0.00001);

if (categories) {
simulation.force("y", d3.forceY((d) => yScale(d[group])).strength(1));
}

simulation.nodes(data).on("tick", () => {
nodes.attr("cx", (d) => d.x).attr("cy", (d) => d.y);
labels.attr("x", (d) => d.x).attr("y", (d) => d.y);
});

// Add y-axis

const yAxisG = box.append("g").call(yAxis);

// Adjust y-axis label position and text
yAxisG
.selectAll(".tick text")
.attr("x", -2)
.attr("y", (d) => yScale(d[group]));

// Add y-axis label
box.append("text").attr("class", "axis-label").attr("x", 0).attr("y", 0);

box
.append("g")
.attr("transform", `translate(0, ${size.h - margin.top})`)
.style("font-size", "12px")
.style("font-family", "Spectral")
.call(xAxis)
.select(".domain")
.remove();

return svg.node();
}
Insert cell
Insert cell
d3 = require("d3@v6")
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