{
const conditions = Array.from(new Set(birds.map(d => d.condition)));
const colorScale = d3.scaleOrdinal(d3.schemeTableau10).domain(conditions);
const svg = d3.create("svg")
.attr("width", 800)
.attr("height", 600)
.style("background", "#f8f9fa");
const margin = { top: 100, right: 100, bottom: -40, left: 70 };
const width = 800 - margin.left - margin.right;
const height = 500 - margin.top - margin.bottom;
const x = d3.scaleLinear()
.domain([d3.min(birds, d => d.bill_length) * 0.95, d3.max(birds, d => d.bill_length) * 1.05])
.range([0, width]);
const y = d3.scaleLinear()
.domain([0, 0.06])
.range([height, 0]);
const g = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const histogram = d3.bin()
.value(d => d.bill_length)
.domain(x.domain())
.thresholds(15);
conditions.forEach(cond => {
const data = birds.filter(d => d.condition === cond);
const bins = histogram(data);
// Calculate max density for scaling
const maxDensity = d3.max(bins, d => d.length / data.length / (d.x1 - d.x0));
// Scale histogram to match KDE range
const scaleFactor = 0.06 / maxDensity * 0.8; // 80% of plot height
g.selectAll(`.histogram-${cond}`)
.data(bins)
.join("rect")
.attr("x", d => x(d.x0) + 1)
.attr("y", d => y(scaleFactor * (d.length / data.length / (d.x1 - d.x0))))
.attr("width", d => Math.max(0, x(d.x1) - x(d.x0) - 1))
.attr("height", d => height - y(scaleFactor * (d.length / data.length / (d.x1 - d.x0))))
.attr("fill", colorScale(cond))
.attr("opacity", 0.3);
});
// Raw Data Points
conditions.forEach(cond => {
g.selectAll(`.point-${cond}`)
.data(birds.filter(d => d.condition === cond))
.join("circle")
.attr("cx", d => x(d.bill_length) + (Math.random() - 0.5) * 5) // Jitter
.attr("cy", d => y(0.01) + Math.random() * 20) // Offset from baseline
.attr("r", 2)
.attr("fill", colorScale(cond))
.attr("opacity", 0.6);
});
// X-axis
g.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x))
.append("text")
.attr("class", "axis-label")
.attr("x", width / 2)
.attr("y", 45)
.attr("fill", "currentColor")
.style("font-size", "14px")
.style("text-anchor", "middle")
.text("Beak Length (mm)");
// Y-axis
g.append("g")
.call(d3.axisLeft(y).tickFormat(d3.format(".0%")))
.append("text")
.attr("class", "axis-label")
.attr("transform", "rotate(-90)")
.attr("y", -50)
.attr("x", -height / 2)
.attr("fill", "currentColor")
.style("font-size", "14px")
.style("text-anchor", "middle")
.text("Density");
// Legend
const legend = g.append("g")
.attr("transform", `translate(${width - 100}, 20)`);
conditions.forEach((cond, i) => {
legend.append("rect")
.attr("x", 0)
.attr("y", i * 20)
.attr("width", 15)
.attr("height", 15)
.attr("fill", colorScale(cond))
.attr("opacity", 0.6);
legend.append("text")
.attr("x", 20)
.attr("y", i * 20 + 12)
.text(cond)
.style("font-size", "12px");
});
// Title
g.append("text")
.attr("x", width / 2)
.attr("y", -10)
.attr("text-anchor", "middle")
.style("font-weight", "bold")
.text("Beak Length Distribution by Condition");
return svg.node();
}