Scatterplot with D3
A scatterplot is a type of visualization using Cartesian Coordinates to display two variables for a set of data. The data are displayed as a collection of dots. The position of each dot on the horizontal and vertical axis indicates the values for an individual data point.
More about: Scatterplot
Scatterplot
<!DOCTYPE html>
<meta charset="utf-8">
<!-- Include d3 library -->
<script src="https://d3js.org/d3.v7.min.js"></script>
<!-- Create a container to host the chart -->
<div id="viz_container"></div>
// set the dimensions and margins of the graph
const margin = {top: 60, right: 50, bottom: 50, left: 50};
const width = 450 - margin.left - margin.right;
const height = 350 - margin.top - margin.bottom;
// append the svg object to the body of the page
const svg = d3.select("#viz_container")
.append("svg")
.attr("width", "100%")
.attr("height", "100%")
.attr("viewBox", "0 0 450 350")
.attr("preserveAspectRatio", "xMinYMin")
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
// parse the Data
d3.csv("https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/correlation/scatterplot.csv")
.then(function(data){
// list of value keys
const typeKeys = data.columns.slice(1);
// X scale and Axis
const f = d3.format("~s")
const xScale = d3.scaleLinear()
.domain(d3.extent(data, d => +d.refugee_number)).nice()
.range([0, width])
svg
.append('g')
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(xScale).tickSize(0).ticks(5).tickPadding(8).tickFormat(f))
.call(d => d.select(".domain").remove());
// Y scale and Axis
const yScale = d3.scaleLinear()
.domain(d3.extent(data, d => +d.idp_number)).nice()
.range([height, 0]);
svg
.append('g')
.call(d3.axisLeft(yScale).tickSize(0).ticks(6).tickPadding(4).tickFormat(f))
.call(d => d.select(".domain").remove());
// color palette
const color = d3.scaleOrdinal()
.domain(typeKeys)
.range(["#EF4A60", "#8EBEFF", "#00B398", "#E1CC0D", "#589BE5", "#18375F", "#0072BC"])
// set horizontal grid line
const GridLineH = function() {return d3.axisLeft().scale(yScale)};
svg
.append("g")
.attr("class", "grid")
.call(GridLineH()
.tickSize(-width,0,0)
.tickFormat("")
.ticks(6)
);
// set vertical grid line
const GridLineV = function() {return d3.axisBottom().scale(xScale)};
svg
.append("g")
.attr("class", "grid")
.call(GridLineV()
.tickSize(height,0,0)
.tickFormat("")
.ticks(5)
);
// create a tooltip
const tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip");
// tooltip events
const mouseover = function(d) {
tooltip
.style("opacity", 1)
d3.select(this)
.style("stroke", "#EF4A60")
.style("opacity", .5)
};
const mousemove = function(event,d) {
const f = d3.format(",");
tooltip
.html("<div><b>Refugees:</b> " +f(d.refugee_number)+ "</div><div><b>IDPs:</b> "+ f(d.idp_number) +"</div>")
.style("top", event.pageY - 10 + "px")
.style("left", event.pageX + 10 + "px")
};
const mouseleave = function(d) {
tooltip
.style("opacity", 0)
d3.select(this)
.style("stroke", "none")
.style("opacity", 1)
};
// create graph
svg.append("g")
.selectAll("g")
.data(data)
.join("circle")
.attr("cx", d => xScale(+d.refugee_number))
.attr("cy", d => yScale(+d.idp_number))
.attr("r", 4)
.style("fill", d => color(d.region))
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
// set data labels
svg
.append("g")
.attr("font-size", 10)
.selectAll("text")
.data(data)
.join("text")
.attr("dy", "0.35em")
.attr("x", d => xScale(+d.refugee_number) + 7)
.attr("y", d => yScale(+d.idp_number))
.text(d => d.region);
// set title
svg
.append("text")
.attr("class", "chart-title")
.attr("x", -(margin.left)*0.5)
.attr("y", -(margin.top)/1.5)
.attr("text-anchor", "start")
.text("Comparison of refugee and IDP population by region | 2021")
// set X axis label
svg
.append("text")
.attr("class", "chart-label")
.attr("x", width/1.7)
.attr("y", height+margin.bottom*0.6)
.attr("text-anchor", "middle")
.text("Number of refugees (millions)");
// set Y axis label
svg
.append("text")
.attr("class", "chart-label")
.attr("x", -(margin.left)*0.5)
.attr("y", -(margin.top/8))
.attr("text-anchor", "start")
.text("Number of IDPs (millions)")
// set source
svg
.append("text")
.attr("class", "chart-source")
.attr("x", -(margin.left)*0.5)
.attr("y", height + margin.bottom*0.8)
.attr("text-anchor", "start")
.text("Source: UNHCR")
// set copyright
svg
.append("text")
.attr("class", "copyright")
.attr("x", -(margin.left)*0.5)
.attr("y", height + margin.bottom*0.95)
.attr("text-anchor", "start")
.text("©UNHCR, The UN Refugee Agency")
})