Published
Edited
Nov 21, 2020
1 fork
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
svg.selectAll("rect")
.data(bins)
.join("rect")
.attr("class", d => d.believe ? "rect-believers" : "rect-deniers")
//.attr("transform", "translate(50,0)")
.attr("width", d => x(d.x1) - x(d.x0))
.attr("y", halfBandwidth + margin.top)
.attr("x", d => x(d.x0))
.style("fill", "#f2cc37")
.style("fill-opacity", d => d.believe ? 1 : 0.5)
.transition()
.duration(500)
.delay((d, i) => i % binThresholds.length * 100)
.attr("height", d => d.believe ?
halfBandwidth + margin.top - believers.scale(d.length):
deniers.scale(-d.length) - (halfBandwidth + margin.top))
.attr("y", d => d.believe ? believers.scale(d.length) : halfBandwidth + margin.top);
svg.append("g")
.call(xAxis);
return svg.node();
}
Insert cell
bins.map (d => d.believe ?
halfBandwidth + margin.top - believers.scale(d.length):
deniers.scale(-d.length) - (halfBandwidth + margin.top))
Insert cell
bins.map(d => d.believe ? believers.scale(d.length) : halfBandwidth + margin.top)
Insert cell
bins = d3.merge([
believers.bins.map(d => ({x0: d.x0, x1: d.x1, length: d.length, believe: true})),
deniers.bins.map(d => ({x0: d.x0, x1: d.x1, length: d.length, believe: false}))
])
Insert cell
believers = createHistogram(data.filter(d => d.existence == 'Yes' || d.existence == 'Y').map(d => d['existence.confidence']), binThresholds, x, y);
Insert cell
deniers = createHistogram(data.filter(d => d.existence == 'No' || d.existence == 'N').map(d => d['existence.confidence']), binThresholds, x, y);
Insert cell
Insert cell
data = Object.assign(d3.csvParse(await FileAttachment("1377884570_tweet_global_warming.csv").text(), d3.autoType))
Insert cell
Insert cell
function createHistogram(data, binThresholds, xScale, yScale) {
// general histogram
const histogram = d3.histogram()
.domain(xScale.domain())
.thresholds(binThresholds);
const bins = histogram(data);

// create scale
const maxBinLength = d3.max(bins.map(d => d.length));
const violinScale = d3.scaleLinear()
.range([height - margin.bottom, margin.top])
.domain([-1448, 1448]); //max lenght for the highest bar

return {
bins: bins,
scale: violinScale
}
}
Insert cell
binThresholds = x.ticks(50)
Insert cell
halfBandwidth = y.bandwidth() / 2
Insert cell
Insert cell
d3.extent(data, d => d['existence.confidence'])
Insert cell
x = d3.scaleLinear()
.domain([0.3, 1.01]).nice() // d3.extent(data, d => d['existence.confidence'])
.range([margin.left, width - margin.right])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80 ).tickSizeOuter(0))
Insert cell
y = d3.scaleBand()
.range([height - margin.bottom, margin.top])
.domain(["pseudo"])
//.paddingOuter(0.1)
Insert cell
y.bandwidth()
Insert cell
Insert cell
width
Insert cell
height = 500
Insert cell
margin = ({top: 20, right: 20, bottom: 30, left: 40})
Insert cell
radius = 3
Insert cell
d3 = require("d3@^6.2.0")
Insert cell
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