# Trendlines

> A scatter fits one least-squares line through its points by default. Turn it off, restyle it, draw it over a dense cloud and see how it treats color groups.

A trend line summarizes a scatter chart with a straight line, a least-squares fit through the points. It shows the direction and steepness of the relationship in one stroke, which helps when the cloud is too loose to judge by eye.

The line is a linear fit only, so it describes a curved relationship poorly. To draw a different line, combine marks with [layers](/docs/composition/layers/). The line works on any point chart, including a [bubble chart](/docs/correlation/bubble/).

## The default trend line

A `point` mark draws a dashed trend line without any option. The line spans from the smallest x to the largest x. No line is drawn when there are fewer than two points or every point has the same x.

```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"
      }
    }
  },
  "chrome": {
    "title": "A scatter draws its trend line without being asked",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
}
```

## Turning it off

Set `trendline: false` on the mark to remove the line. Do this when the chart carries its own reference line, such as a manual diagonal, which would otherwise compete with the fitted one.

```json
{
  "mark": {
    "type": "point",
    "trendline": false
  },
  "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"
      }
    }
  },
  "chrome": {
    "title": "The same points with no fitted line",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
}
```

## Styling the line

Pass an object to `trendline` to restyle it. `stroke` sets the color, in any CSS color, and `strokeWidth` sets the width in pixels, which defaults to 1.5. A heavier, colored line stands out where the default blends into the axes.

```json
{
  "mark": {
    "type": "point",
    "trendline": {
      "stroke": "#d1495b",
      "strokeWidth": 2.5
    }
  },
  "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"
      }
    }
  },
  "chrome": {
    "title": "Stroke color and width set how much the line stands out",
    "subtitle": "Cumulative per-student spending vs. PISA 2022 math performance"
  }
}
```

## Over a dense cloud

The line is drawn below the dots by default, and a thick cloud of dots can hide it. `layer: "above"` draws it on top. This chart also sets a small dot `size` and a partial `opacity` so the density shows through.

```json
{
  "mark": {
    "type": "point",
    "size": 3,
    "opacity": 0.45,
    "trendline": {
      "layer": "above",
      "strokeWidth": 2.5
    }
  },
  "data": [
    {
      "lowIncome": 42.5,
      "reading": 80.5
    },
    {
      "lowIncome": 20.6,
      "reading": 79.1
    },
    {
      "lowIncome": 68.1,
      "reading": 75.5
    },
    {
      "lowIncome": 8.7,
      "reading": 98
    },
    {
      "lowIncome": 57.3,
      "reading": 61.8
    },
    {
      "lowIncome": 24.1,
      "reading": 96.2
    },
    {
      "lowIncome": 58,
      "reading": 54.4
    },
    {
      "lowIncome": 84.9,
      "reading": 55.1
    },
    {
      "lowIncome": 93.6,
      "reading": 38
    },
    {
      "lowIncome": 34.8,
      "reading": 70.1
    },
    {
      "lowIncome": 53.4,
      "reading": 65.1
    },
    {
      "lowIncome": 26.5,
      "reading": 82.5
    },
    {
      "lowIncome": 73,
      "reading": 42.4
    },
    {
      "lowIncome": 28.8,
      "reading": 86
    },
    {
      "lowIncome": 0.7,
      "reading": 98
    },
    {
      "lowIncome": 0.9,
      "reading": 86.6
    },
    {
      "lowIncome": 78.9,
      "reading": 40.7
    },
    {
      "lowIncome": 54,
      "reading": 70.2
    },
    {
      "lowIncome": 54.4,
      "reading": 64.3
    },
    {
      "lowIncome": 76.5,
      "reading": 41.4
    },
    {
      "lowIncome": 89.3,
      "reading": 47.7
    },
    {
      "lowIncome": 60.6,
      "reading": 80.4
    },
    {
      "lowIncome": 66.3,
      "reading": 58
    },
    {
      "lowIncome": 11.1,
      "reading": 98
    },
    {
      "lowIncome": 73.5,
      "reading": 41.4
    },
    {
      "lowIncome": 48.2,
      "reading": 87.6
    },
    {
      "lowIncome": 53.7,
      "reading": 57
    },
    {
      "lowIncome": 14.6,
      "reading": 84.5
    },
    {
      "lowIncome": 93.1,
      "reading": 48.6
    },
    {
      "lowIncome": 76.7,
      "reading": 57.6
    },
    {
      "lowIncome": 45.5,
      "reading": 81.5
    },
    {
      "lowIncome": 15.3,
      "reading": 98
    },
    {
      "lowIncome": 17.1,
      "reading": 98
    },
    {
      "lowIncome": 34.7,
      "reading": 76.3
    },
    {
      "lowIncome": 98.3,
      "reading": 51.8
    },
    {
      "lowIncome": 62.4,
      "reading": 77
    },
    {
      "lowIncome": 74.7,
      "reading": 68.1
    },
    {
      "lowIncome": 96.7,
      "reading": 43.3
    },
    {
      "lowIncome": 2.9,
      "reading": 86
    },
    {
      "lowIncome": 35.5,
      "reading": 76.4
    },
    {
      "lowIncome": 17.6,
      "reading": 98
    },
    {
      "lowIncome": 22.6,
      "reading": 75.2
    },
    {
      "lowIncome": 13.7,
      "reading": 87.3
    },
    {
      "lowIncome": 89.2,
      "reading": 38.2
    },
    {
      "lowIncome": 11.4,
      "reading": 98
    },
    {
      "lowIncome": 72.5,
      "reading": 68.1
    },
    {
      "lowIncome": 36.9,
      "reading": 86
    },
    {
      "lowIncome": 68.4,
      "reading": 43.9
    },
    {
      "lowIncome": 59.9,
      "reading": 82.8
    },
    {
      "lowIncome": 17.5,
      "reading": 86.3
    },
    {
      "lowIncome": 38.7,
      "reading": 93.2
    },
    {
      "lowIncome": 33.5,
      "reading": 92.8
    },
    {
      "lowIncome": 52,
      "reading": 83.3
    },
    {
      "lowIncome": 89.2,
      "reading": 35.3
    },
    {
      "lowIncome": 51.4,
      "reading": 85.7
    },
    {
      "lowIncome": 22.7,
      "reading": 85
    },
    {
      "lowIncome": 12.4,
      "reading": 96.8
    },
    {
      "lowIncome": 53.1,
      "reading": 61.2
    },
    {
      "lowIncome": 56.2,
      "reading": 77.1
    },
    {
      "lowIncome": 70.9,
      "reading": 43
    }
  ],
  "encoding": {
    "x": {
      "field": "lowIncome",
      "type": "quantitative",
      "scale": {
        "domain": [
          0,
          100
        ]
      },
      "axis": {
        "title": "Students from low-income households (%)"
      }
    },
    "y": {
      "field": "reading",
      "type": "quantitative",
      "scale": {
        "domain": [
          0,
          100
        ]
      },
      "axis": {
        "title": "Reading at grade level (%)"
      }
    }
  },
  "chrome": {
    "title": "Reading scores fall as the low-income share rises",
    "subtitle": "Share of students reading at grade level vs. low-income share, by campus"
  }
}
```

## Color groups

The fit uses every point on the chart, so a chart with a `color` channel still gets one line, not one per group. Read it as the overall trend, and look at the colors for groups that sit off it.

```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"
    }
  },
  "chrome": {
    "title": "The fitted line covers every region at once",
    "subtitle": "Cost of living vs. quality of living in global cities, 2024"
  }
}
```

## Fields that matter

| Field | What it does |
| --- | --- |
| `mark.trendline` | `true` by default on a point mark. `false` removes the line. |
| `mark.trendline.layer` | `"below"` (default) draws the line behind the dots, `"above"` draws it over them. |
| `mark.trendline.stroke` | The line color, any CSS color. The theme sets the default. |
| `mark.trendline.strokeWidth` | The line width in pixels. Defaults to 1.5. |
| `mark.size`, `mark.opacity` | The dot radius and opacity, used to keep a dense cloud readable. |
| `encoding.x`, `encoding.y` | The two quantitative fields the line is fitted through. |

## Related

- [Scatter](/docs/correlation/scatter/): the chart the line belongs to.
- [Bubble](/docs/correlation/bubble/): the same line over sized dots.
- [Layers](/docs/composition/layers/): combine marks on shared scales.
- [Line](/docs/change-over-time/line/): a line drawn from the data itself.
- [Error bars](/docs/distribution/error-bars/): show an interval around an estimate.
- [LayerSpec reference](/docs/spec/layer-spec/): the layer fields and what a layer inherits.
