Scatter plots are a powerful way to visualize the relationship between two variables. In this section, we will learn how to create scatter plots using D3.js. We will cover the following topics:
- Understanding Scatter Plots
- Setting Up the SVG Canvas
- Binding Data to Circles
- Adding Axes
- Styling the Scatter Plot
- Practical Exercise
- Understanding Scatter Plots
A scatter plot displays points on a two-dimensional plane, where each point represents an observation in the dataset. The position of each point is determined by the values of two variables, one plotted along the x-axis and the other along the y-axis.
- Setting Up the SVG Canvas
First, we need to set up the SVG canvas where our scatter plot will be drawn.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Scatter Plot with D3.js</title> <script src="https://d3js.org/d3.v6.min.js"></script> <style> .axis path, .axis line { fill: none; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 12px; } </style> </head> <body> <svg width="600" height="400"></svg> <script> // JavaScript code will go here </script> </body> </html>
- Binding Data to Circles
Next, we will bind our data to circles in the SVG canvas. Let's assume we have the following dataset:
const data = [ { x: 30, y: 20 }, { x: 85, y: 90 }, { x: 45, y: 50 }, { x: 60, y: 30 }, { x: 20, y: 80 }, { x: 90, y: 60 } ];
We will use D3.js to create circles for each data point.
const svg = d3.select("svg"); const width = +svg.attr("width"); const height = +svg.attr("height"); const xScale = d3.scaleLinear() .domain([0, d3.max(data, d => d.x)]) .range([0, width]); const yScale = d3.scaleLinear() .domain([0, d3.max(data, d => d.y)]) .range([height, 0]); svg.selectAll("circle") .data(data) .enter() .append("circle") .attr("cx", d => xScale(d.x)) .attr("cy", d => yScale(d.y)) .attr("r", 5);
- Adding Axes
To make our scatter plot more informative, we need to add axes.
const xAxis = d3.axisBottom(xScale); const yAxis = d3.axisLeft(yScale); svg.append("g") .attr("transform", `translate(0, ${height})`) .call(xAxis); svg.append("g") .call(yAxis);
- Styling the Scatter Plot
We can enhance the appearance of our scatter plot by adding some styles.
svg.selectAll("circle") .attr("fill", "steelblue") .attr("stroke", "black") .attr("stroke-width", 1);
- Practical Exercise
Exercise: Create a Scatter Plot
Objective: Create a scatter plot using the provided dataset and customize it with different colors and sizes for the circles.
Dataset:
const data = [ { x: 10, y: 20, size: 5, color: "red" }, { x: 40, y: 90, size: 10, color: "blue" }, { x: 60, y: 50, size: 15, color: "green" }, { x: 80, y: 30, size: 20, color: "purple" }, { x: 20, y: 70, size: 25, color: "orange" }, { x: 90, y: 60, size: 30, color: "pink" } ];
Steps:
- Set up the SVG canvas.
- Bind the data to circles.
- Add axes.
- Style the circles based on the
size
andcolor
properties.
Solution:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Scatter Plot Exercise</title> <script src="https://d3js.org/d3.v6.min.js"></script> <style> .axis path, .axis line { fill: none; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 12px; } </style> </head> <body> <svg width="600" height="400"></svg> <script> const data = [ { x: 10, y: 20, size: 5, color: "red" }, { x: 40, y: 90, size: 10, color: "blue" }, { x: 60, y: 50, size: 15, color: "green" }, { x: 80, y: 30, size: 20, color: "purple" }, { x: 20, y: 70, size: 25, color: "orange" }, { x: 90, y: 60, size: 30, color: "pink" } ]; const svg = d3.select("svg"); const width = +svg.attr("width"); const height = +svg.attr("height"); const xScale = d3.scaleLinear() .domain([0, d3.max(data, d => d.x)]) .range([0, width]); const yScale = d3.scaleLinear() .domain([0, d3.max(data, d => d.y)]) .range([height, 0]); const xAxis = d3.axisBottom(xScale); const yAxis = d3.axisLeft(yScale); svg.append("g") .attr("transform", `translate(0, ${height})`) .call(xAxis); svg.append("g") .call(yAxis); svg.selectAll("circle") .data(data) .enter() .append("circle") .attr("cx", d => xScale(d.x)) .attr("cy", d => yScale(d.y)) .attr("r", d => d.size) .attr("fill", d => d.color) .attr("stroke", "black") .attr("stroke-width", 1); </script> </body> </html>
Conclusion
In this section, we learned how to create scatter plots using D3.js. We covered setting up the SVG canvas, binding data to circles, adding axes, and styling the scatter plot. We also provided a practical exercise to reinforce the concepts learned. In the next module, we will explore more advanced visualizations.
D3.js: From Beginner to Advanced
Module 1: Introduction to D3.js
Module 2: Working with Selections
Module 3: Data and Scales
Module 4: Creating Basic Visualizations
Module 5: Advanced Visualizations
- Creating Hierarchical Layouts
- Creating Force Layouts
- Creating Geo Maps
- Creating Custom Visualizations
Module 6: Interactivity and Animation
Module 7: Working with Real Data
- Fetching Data from APIs
- Data Cleaning and Transformation
- Integrating with Other Libraries
- Case Studies and Examples
Module 8: Performance and Optimization
- Optimizing D3.js Performance
- Handling Large Datasets
- Efficient Data Binding
- Debugging and Troubleshooting
Module 9: Best Practices and Advanced Techniques
- Code Organization and Modularity
- Reusable Components
- Advanced D3.js Patterns
- Contributing to D3.js Community