Public
Edited
Mar 5, 2024
2 forks
Insert cell
Insert cell
Insert cell
chart = beeswarm(data, "position", "position2", "group", "type", "value", "name", "border")
Insert cell
dataFile = await FileAttachment("230516_ManualTopicAnalysis_FinalVisualisation@1.csv").csv()
Insert cell
data = dataFile.map((d) => {
return {
...d,
position: +d.AvgDW,
position2: +d.SumEng,
type: d.Themes,
group: d.Controversy,
value: +d.FrqTwpc,
name: d.Topics,
border: d.Cooccurrence
};
})
Insert cell
viewof table = Inputs.table(data)
Insert cell
maxDisa = d3.max(data, (d)=>d.position)
Insert cell
colorScale2 = d3.scaleOrdinal()
.domain([...new Set(data.map((d) => d.Themes))])
.range(["#291a21", "#FF6426", "#7AC8B6","#d9bad6", "#CBD854", "#88DCFF", "#ed9116", "#e5e5de", "#ccbdb2", "#ebad40", "#e5e5de", "#ccbdb2", "#408fa3", "#194F6B", "#874a85", "#f5d499", "#9ec4d1", "#bfa6bf", "#91a8b2", "#faedd6", "#c9d1d9", "#ded1db" ]);
Insert cell
colorScale = d3.scaleOrdinal(
[...new Set(data.map((d) => d.Themes))],
d3.schemeTableau10
)
Insert cell
maxTw = d3.max(dataFile, (d)=>+d.SumT)
Insert cell
themesTopics = [...new Set(data.map((d) => d.Themes))];
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
beeswarm = (data, position, position2, group, type, value, name, border) => {
const svg = d3.create("svg").attr("viewBox", [0, 0, 1000, 768]);

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

const padding = 1;
const size = { w: 700, h: 600 };
const margin = { top: 20, bottom: 30, left: 30, right: 10 };
const radius = d3
.scaleSqrt()
.domain(d3.extent(data, (d) => d[value]))
.range([4, 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) => colorScale2(d[type]))
.style("stroke", d => { if (d.border == "Y") return "black" })
.attr("stroke-width", 2)

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)
.style("font-family", selectedFont);

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", -10)
.attr("y", (d) => yScale(d[group]));

// Add y-axis label
box.append("text")
.attr("class", "axis-label")
.style("font-family", selectedFont)
.attr("x", -10)
.attr("y", 0);

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

return svg.node();
}
Insert cell
Insert cell
import {select} from "@jashkenas/inputs"
Insert cell
import {parseFont} from "caebc9c04c1cc12c"
Insert cell
viewof selectedFont = Inputs.select(
[
"Epilogue"
],
{
label: "Desired font",
// options:,
value: "Source Serif Pro"
}
)
Insert cell
Insert cell
import {Legend, Swatches} from "@d3/color-legend"
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