Scatter Plot

A scatter plot is a type of data visualization that uses dots to represent the values obtained for two different variables. It is particularly useful for identifying relationships between these variables. In the context of this discussion, scatter plots are used to illustrate the relationship between rainfall and umbrella sales, demonstrating a positive correlation where both variables increase together.

Example of a Scatter Plot

The chapter provides an example of a scatter plot in Figure 9.16, which shows the relationship between rainfall and umbrella sales. The scatter plot illustrates a positive correlation, with data points forming a band from the bottom left to the top right.

Figure 9.16 Scatter plot of rainfall vs umbrella sales Figure 9.16 Scatter plot of rainfall vs umbrella sales

Creating a Scatter Plot with C3

Scatter plots can be easily created using the C3.js library. To create a scatter plot, you need to set the type field to scatter, with one variable on the X axis and another on the Y axis. This is demonstrated in Figure 10.22, which shows a scatter plot chart of NYC rainfall versus umbrella sales.

Figure 10.22 Scatter plot chart of NYC rainfall vs. umbrella sales Figure 10.22 Scatter plot chart of NYC rainfall vs. umbrella sales

Code Example

Below is a simple chart definition required to create a scatter plot using C3.js. In this example, the Precipitation (rainfall) column is used as the X axis, and the UmbrellaSales column is used as the Y axis. The key difference from other chart types is setting the type field to scatter.

Listing 10.12 Creating a scatter plot chart comparing rainfall to umbrella sales in NYC (extract from listing-10.12/public/app.js)

var chart = c3.generate({
    bindto: "#chart",
    data: {
        json: data,
        keys: {
            x: "Precipitation",    // Uses rainfall as the X axis in the scatter plot
            value: [ "UmbrellaSales" ]    // Uses umbrella sales as the Y axis in the scatter plot
        },
        type: "scatter"    // Sets our chart type to scatter
    }
});

This code snippet demonstrates how straightforward it is to create a scatter plot using C3.js, making it an accessible tool for visualizing relationships between data variables.

FAQ (Frequently asked questions)

What does Figure 10.22 in the book 'Data Wrangling with JavaScript' show?

What variables are used on the X and Y axes in the scatter plot example from Listing 10.12?

What does Figure 9.16 in the book 'Data Wrangling with JavaScript' depict?