# Scatter

> Plot two numeric fields against each other to find correlation, clusters and outliers, then add color, direct labels, a reference line and a log axis.

A scatter chart places one dot per row at the position of two numbers, so a reader can see whether they move together, which rows cluster and which sit apart. Reach for it when the question is how one measure relates to another.

Add a third number and it becomes a [bubble chart](/docs/correlation/bubble/). When both fields are categories, a [heatmap](/docs/correlation/heatmap/) fits better, and when the x field is time, a [line chart](/docs/change-over-time/line/) shows the trend directly.

## A basic scatter

Set `mark` to `"point"` and put one quantitative field on each of `x` and `y`. A `tooltip` channel lists the fields shown on hover. A scatter also draws a fitted trend line by default, which the [trendlines](/docs/correlation/trendlines/) page covers.

```json
{
  "mark": "point",
  "data": [
    {
      "country": "Singapore",
      "spending": 14.5,
      "math": 575
    },
    {
      "country": "Japan",
      "spending": 10.1,
      "math": 536
    },
    {
      "country": "South Korea",
      "spending": 12.2,
      "math": 527
    },
    {
      "country": "Estonia",
      "spending": 8.4,
      "math": 510
    },
    {
      "country": "Switzerland",
      "spending": 17.8,
      "math": 508
    },
    {
      "country": "Canada",
      "spending": 12.4,
      "math": 497
    },
    {
      "country": "Netherlands",
      "spending": 13.2,
      "math": 493
    },
    {
      "country": "Ireland",
      "spending": 11.3,
      "math": 492
    },
    {
      "country": "Poland",
      "spending": 7.8,
      "math": 489
    },
    {
      "country": "Denmark",
      "spending": 14.1,
      "math": 489
    },
    {
      "country": "United Kingdom",
      "spending": 12.6,
      "math": 489
    },
    {
      "country": "Australia",
      "spending": 12.8,
      "math": 487
    },
    {
      "country": "Finland",
      "spending": 12,
      "math": 484
    },
    {
      "country": "Germany",
      "spending": 13.7,
      "math": 475
    },
    {
      "country": "France",
      "spending": 11.4,
      "math": 474
    },
    {
      "country": "Italy",
      "spending": 10.2,
      "math": 471
    },
    {
      "country": "Norway",
      "spending": 16.2,
      "math": 468
    },
    {
      "country": "United States",
      "spending": 14.3,
      "math": 465
    },
    {
      "country": "Israel",
      "spending": 10.6,
      "math": 458
    },
    {
      "country": "Chile",
      "spending": 6.1,
      "math": 412
    },
    {
      "country": "Mexico",
      "spending": 3.3,
      "math": 395
    },
    {
      "country": "Colombia",
      "spending": 3.8,
      "math": 383
    }
  ],
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    },
    "tooltip": [
      {
        "field": "country",
        "type": "nominal",
        "title": "Country"
      },
      {
        "field": "spending",
        "type": "quantitative",
        "title": "Spending ($K)"
      },
      {
        "field": "math",
        "type": "quantitative",
        "title": "Math score"
      }
    ]
  },
  "chrome": {
    "title": "Switzerland spends the most per student; Singapore scores highest",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance, OECD countries"
  }
}
```

## Direct labels

Text annotations name the notable points on the chart, so a reader does not need to hover to find them. A `refline` marks a benchmark, here the OECD average. Anchor each label to the side of its point that has room.

```json
{
  "mark": "point",
  "data": [
    {
      "country": "Singapore",
      "spending": 14.5,
      "math": 575
    },
    {
      "country": "Japan",
      "spending": 10.1,
      "math": 536
    },
    {
      "country": "South Korea",
      "spending": 12.2,
      "math": 527
    },
    {
      "country": "Estonia",
      "spending": 8.4,
      "math": 510
    },
    {
      "country": "Switzerland",
      "spending": 17.8,
      "math": 508
    },
    {
      "country": "Canada",
      "spending": 12.4,
      "math": 497
    },
    {
      "country": "Netherlands",
      "spending": 13.2,
      "math": 493
    },
    {
      "country": "Ireland",
      "spending": 11.3,
      "math": 492
    },
    {
      "country": "Poland",
      "spending": 7.8,
      "math": 489
    },
    {
      "country": "Denmark",
      "spending": 14.1,
      "math": 489
    },
    {
      "country": "United Kingdom",
      "spending": 12.6,
      "math": 489
    },
    {
      "country": "Australia",
      "spending": 12.8,
      "math": 487
    },
    {
      "country": "Finland",
      "spending": 12,
      "math": 484
    },
    {
      "country": "Germany",
      "spending": 13.7,
      "math": 475
    },
    {
      "country": "France",
      "spending": 11.4,
      "math": 474
    },
    {
      "country": "Italy",
      "spending": 10.2,
      "math": 471
    },
    {
      "country": "Norway",
      "spending": 16.2,
      "math": 468
    },
    {
      "country": "United States",
      "spending": 14.3,
      "math": 465
    },
    {
      "country": "Israel",
      "spending": 10.6,
      "math": 458
    },
    {
      "country": "Chile",
      "spending": 6.1,
      "math": 412
    },
    {
      "country": "Mexico",
      "spending": 3.3,
      "math": 395
    },
    {
      "country": "Colombia",
      "spending": 3.8,
      "math": 383
    }
  ],
  "encoding": {
    "x": {
      "field": "spending",
      "type": "quantitative",
      "axis": {
        "title": "Spending per student ($K, PPP)"
      }
    },
    "y": {
      "field": "math",
      "type": "quantitative",
      "axis": {
        "title": "PISA math score"
      }
    }
  },
  "annotations": [
    {
      "type": "text",
      "x": 14.5,
      "y": 575,
      "text": "Singapore",
      "anchor": "left",
      "fontSize": 10,
      "dot": false
    },
    {
      "type": "text",
      "x": 14.3,
      "y": 465,
      "text": "United States",
      "anchor": "right",
      "fontSize": 10,
      "dot": false
    },
    {
      "type": "text",
      "x": 8.4,
      "y": 510,
      "text": "Estonia",
      "anchor": "left",
      "fontSize": 10,
      "dot": false
    },
    {
      "type": "refline",
      "y": 472,
      "label": "OECD avg",
      "style": "dashed",
      "stroke": "#64748b"
    }
  ],
  "chrome": {
    "title": "The US spends about as much as Singapore and scores 110 points lower",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance, OECD countries"
  }
}
```

## Color groups

A categorical `color` channel splits the cloud into series. Setting `scale.domain` on each axis fixes the plot window to a range you choose.

```json
{
  "mark": "point",
  "data": [
    {
      "city": "Zurich",
      "cost": 131,
      "quality": 98,
      "region": "Europe"
    },
    {
      "city": "Vienna",
      "cost": 79,
      "quality": 97,
      "region": "Europe"
    },
    {
      "city": "Geneva",
      "cost": 124,
      "quality": 96,
      "region": "Europe"
    },
    {
      "city": "Copenhagen",
      "cost": 89,
      "quality": 95,
      "region": "Europe"
    },
    {
      "city": "Singapore",
      "cost": 107,
      "quality": 93,
      "region": "Asia"
    },
    {
      "city": "Sydney",
      "cost": 83,
      "quality": 92,
      "region": "Asia-Pacific"
    },
    {
      "city": "Montreal",
      "cost": 64,
      "quality": 91,
      "region": "Americas"
    },
    {
      "city": "Tokyo",
      "cost": 78,
      "quality": 90,
      "region": "Asia"
    },
    {
      "city": "London",
      "cost": 101,
      "quality": 89,
      "region": "Europe"
    },
    {
      "city": "Budapest",
      "cost": 52,
      "quality": 87,
      "region": "Europe"
    },
    {
      "city": "Seoul",
      "cost": 82,
      "quality": 86,
      "region": "Asia"
    },
    {
      "city": "New York",
      "cost": 100,
      "quality": 85,
      "region": "Americas"
    },
    {
      "city": "Kuala Lumpur",
      "cost": 40,
      "quality": 83,
      "region": "Asia"
    },
    {
      "city": "Warsaw",
      "cost": 51,
      "quality": 88,
      "region": "Europe"
    },
    {
      "city": "Santiago",
      "cost": 47,
      "quality": 79,
      "region": "Americas"
    },
    {
      "city": "Hong Kong",
      "cost": 120,
      "quality": 78,
      "region": "Asia"
    },
    {
      "city": "Bangkok",
      "cost": 44,
      "quality": 70,
      "region": "Asia"
    },
    {
      "city": "Buenos Aires",
      "cost": 38,
      "quality": 72,
      "region": "Americas"
    }
  ],
  "encoding": {
    "x": {
      "field": "cost",
      "type": "quantitative",
      "axis": {
        "title": "Cost of living index"
      },
      "scale": {
        "domain": [
          35,
          140
        ]
      }
    },
    "y": {
      "field": "quality",
      "type": "quantitative",
      "axis": {
        "title": "Quality of living index"
      },
      "scale": {
        "domain": [
          68,
          100
        ]
      }
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "tooltip": [
      {
        "field": "city",
        "type": "nominal",
        "title": "City"
      },
      {
        "field": "cost",
        "type": "quantitative",
        "title": "Cost index"
      },
      {
        "field": "quality",
        "type": "quantitative",
        "title": "Quality index"
      },
      {
        "field": "region",
        "type": "nominal",
        "title": "Region"
      }
    ]
  },
  "chrome": {
    "title": "Vienna and Montreal rate higher than New York and cost less",
    "subtitle": "Cost of living vs. quality of living in global cities, 2024"
  }
}
```

## A log axis

`scale: { type: "log" }` spaces the axis by orders of magnitude. It suits a field like GDP per capita that spans a wide range.

```json
{
  "mark": "point",
  "data": [
    {
      "country": "United States",
      "gdpPerCapita": 63544,
      "lifeExpectancy": 77.3,
      "region": "Americas",
      "pop": 331
    },
    {
      "country": "Canada",
      "gdpPerCapita": 43242,
      "lifeExpectancy": 82.2,
      "region": "Americas",
      "pop": 38
    },
    {
      "country": "Brazil",
      "gdpPerCapita": 6797,
      "lifeExpectancy": 75.9,
      "region": "Americas",
      "pop": 213
    },
    {
      "country": "Mexico",
      "gdpPerCapita": 8347,
      "lifeExpectancy": 75.1,
      "region": "Americas",
      "pop": 130
    },
    {
      "country": "Argentina",
      "gdpPerCapita": 8442,
      "lifeExpectancy": 76.5,
      "region": "Americas",
      "pop": 45
    },
    {
      "country": "Germany",
      "gdpPerCapita": 45724,
      "lifeExpectancy": 81,
      "region": "Europe",
      "pop": 83
    },
    {
      "country": "France",
      "gdpPerCapita": 38625,
      "lifeExpectancy": 82.5,
      "region": "Europe",
      "pop": 67
    },
    {
      "country": "United Kingdom",
      "gdpPerCapita": 40285,
      "lifeExpectancy": 81.3,
      "region": "Europe",
      "pop": 67
    },
    {
      "country": "Italy",
      "gdpPerCapita": 31676,
      "lifeExpectancy": 83.5,
      "region": "Europe",
      "pop": 59
    },
    {
      "country": "Spain",
      "gdpPerCapita": 27057,
      "lifeExpectancy": 83.6,
      "region": "Europe",
      "pop": 47
    },
    {
      "country": "Japan",
      "gdpPerCapita": 39313,
      "lifeExpectancy": 84.6,
      "region": "Asia",
      "pop": 125
    },
    {
      "country": "South Korea",
      "gdpPerCapita": 31489,
      "lifeExpectancy": 83.5,
      "region": "Asia",
      "pop": 52
    },
    {
      "country": "China",
      "gdpPerCapita": 10500,
      "lifeExpectancy": 78.2,
      "region": "Asia",
      "pop": 1412
    },
    {
      "country": "India",
      "gdpPerCapita": 1901,
      "lifeExpectancy": 70.2,
      "region": "Asia",
      "pop": 1408
    },
    {
      "country": "Indonesia",
      "gdpPerCapita": 3870,
      "lifeExpectancy": 71.9,
      "region": "Asia",
      "pop": 273
    },
    {
      "country": "South Africa",
      "gdpPerCapita": 5091,
      "lifeExpectancy": 64.1,
      "region": "Africa",
      "pop": 60
    },
    {
      "country": "Nigeria",
      "gdpPerCapita": 2066,
      "lifeExpectancy": 54.7,
      "region": "Africa",
      "pop": 211
    },
    {
      "country": "Egypt",
      "gdpPerCapita": 3019,
      "lifeExpectancy": 72,
      "region": "Africa",
      "pop": 104
    },
    {
      "country": "Ethiopia",
      "gdpPerCapita": 926,
      "lifeExpectancy": 66.6,
      "region": "Africa",
      "pop": 118
    },
    {
      "country": "Kenya",
      "gdpPerCapita": 1838,
      "lifeExpectancy": 66.7,
      "region": "Africa",
      "pop": 54
    }
  ],
  "encoding": {
    "x": {
      "field": "gdpPerCapita",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (USD, log scale)",
        "format": "$.2~s"
      }
    },
    "y": {
      "field": "lifeExpectancy",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "color": {
      "field": "region",
      "type": "nominal"
    }
  },
  "chrome": {
    "title": "A log axis spreads out the poorest countries",
    "subtitle": "GDP per capita vs. life expectancy, 2022"
  }
}
```

## Fields that matter

| Field | What it does |
| --- | --- |
| `mark` | `"point"` draws one dot per row. |
| `encoding.x`, `encoding.y` | The two quantitative fields the dot is placed by. |
| `encoding.color` | Optional. A categorical field that splits the dots into colored series. |
| `encoding.tooltip` | Optional. The fields listed when a dot is hovered. |
| `scale.domain` | Fixes the axis range to `[min, max]`. |
| `scale.type` | Sets the scale type. `"log"` gives a logarithmic axis. |
| `scale.zero` | Whether the axis must include zero. Point marks default to false, so the axis fits the data; set true to anchor at zero. |
| `annotations` | Text labels and `refline` entries drawn in data units. |

## Related

- [Bubble](/docs/correlation/bubble/): add a third number as dot size.
- [Trendlines](/docs/correlation/trendlines/): control the fitted line.
- [Heatmap](/docs/correlation/heatmap/): relate two categories through a number.
- [Beeswarm](/docs/distribution/beeswarm/): one number per dot, dodged along a lane.
- [Line](/docs/change-over-time/line/): a trend over time.
- [Encoding reference](/docs/spec/encoding/): every channel with its scale and axis settings.
