How to Make a Scatter Plot with a Trendline, Equation and R-squared

By Dr. Zubair Khalid, DVM, MS, PhD ·

How to Make a Scatter Plot with a Trendline, Equation and R-squared

A scatter plot shows paired observations (x, y) as points in a plane. The NIST/SEMATECH e-Handbook treats it as the basic tool for revealing the relationship between two variables, including linear and nonlinear patterns and outliers that summary statistics hide [1]. When you add a trendline, the equation, and R-squared, the figure stops being decoration and becomes a quantitative statement about your data.

That statement has to be earned. A line fitted to points that curve, or to points dominated by one observation, will report a slope and an R-squared that describe the fit, not the system. This guide covers the axis convention, the least-squares calculation, a worked standard curve, and the checks that tell you whether the line is worth reporting.

Quick Answer

  • Plot the explanatory or controlled variable on the x axis and the measured response on the y axis. Ordinary least-squares regression of y on x minimizes vertical distances, so swapping the axes gives a different line.
  • The least-squares line is $y = a + bx$, with slope $b = r \times (s_y / s_x)$, and it passes through the point (mean x, mean y) [2].
  • R-squared is the fraction of variance in y explained by the line. For a simple linear fit it equals r squared [2].
  • Always plot the points before you trust the equation. Anscombe's four data sets share a mean of x of 9, a mean of y of 7.50, a correlation of 0.816, and the regression line y = 3.00 + 0.500x, yet their scatter plots look completely different, including a curve and a single influential outlier [3].
  • Report the equation, R-squared, n, and the fitting method. A high R-squared does not prove the relationship is linear, and it says nothing about accuracy outside the fitted range.

Step 1: Decide Which Variable Goes on Which Axis

The convention is not arbitrary. Put the variable you controlled or used to predict on the x axis, and the variable you measured in response on the y axis. In a Bradford assay, protein concentration is set by the experimenter and absorbance is read from the instrument, so concentration goes on x and A595 goes on y.

This matters because least-squares regression minimizes the squared vertical distances from the points to the line. The method assumes the x values are known and the error lives in y. If you swap the axes, you minimize horizontal distances instead, and you get a different slope and intercept. The two lines are not inverses of each other except in the special case where all points fall exactly on a line.

If both variables carry comparable measurement error, ordinary least-squares is still the common choice, but say so in your methods. Do not present the fitted line as if the x axis were error-free when it is not.

Step 2: Plot the Points Before You Fit Anything

Plot first. Look for curvature, clusters, gaps, and points that sit far from the rest. The NIST handbook describes the scatter plot as the tool for revealing these features [1]. A fit computed on unplotted data can hide a curve behind a respectable R-squared.

Overplotting is the main practical problem once you have many points. Use transparency, smaller markers, or a small amount of jitter so that dense regions do not collapse into a solid blob. When you have groups, use distinct colors and shapes and include a legend. Weissgerber and colleagues argue that showing individual data points lets readers evaluate the distribution directly instead of inferring it from a summary bar [6].

For a standard curve, plot the calibrator points as markers and the fitted line as a separate line. Readers need to see both.

Step 3: Fit the Least-Squares Line

The fitted model is:

$$y = a + bx$$

where $a$ is the intercept (the predicted y when x is 0) and $b$ is the slope (the change in predicted y per unit change in x). The slope is related to the correlation by:

$$b = r \times \frac{s_y}{s_x}$$

with $s_x$ and $s_y$ the sample standard deviations of x and y [2]. The line always passes through (mean x, mean y), which is a useful check when you plot it by hand or verify software output.

R-squared is the fraction of the variance in y that the line explains. For a simple linear fit, it equals the square of the correlation coefficient r [2]. That equivalence holds only for the straight-line case. Once you fit a curve or add terms, R-squared and r squared are no longer the same quantity.

The estimates $a$ and $b$ describe the fitted sample. They are not the true population parameters, and they carry uncertainty that a standard error or confidence interval would express.

Worked Example

A Bradford standard curve is a good test case because the relationship is expected to be close to linear over a narrow range. The data below are illustrative. BSA standards at 0, 0.25, 0.50, 0.75, and 1.00 mg/mL gave A595 readings of 0.002, 0.131, 0.258, 0.372, and 0.487.

BSA (mg/mL)A595
00.002
0.250.131
0.500.258
0.750.372
1.000.487

The least-squares fit is:

$$A = 0.4844 \times \text{concentration} + 0.0078, \quad R^2 = 0.9991$$

An unknown sample with A = 0.300 reads:

$$\text{concentration} = \frac{0.300 - 0.0078}{0.4844} = 0.603 \text{ mg/mL}$$

Two rules follow from this. First, do not report unknowns outside the standard range. An absorbance above the top standard or below the blank falls outside the range you actually validated, and the linear fit gives no guarantee there. Second, the intercept of 0.0078 is small but not zero, which is normal for a blank-corrected assay. Report it, and do not force the line through the origin without a stated reason.

If you want to reproduce this fit without writing code, the Scatter Plot Maker on this site plots the points and returns the trendline, equation, and R-squared. A Linear Regression Calculator gives the same coefficients with more detail on the fit.

Step 4: Check the Residuals

A trendline and an R-squared are not evidence that a line is the right model. Plot the residuals, meaning the vertical distance from each point to the fitted line, against x. The NIST handbook explains that residual plots show whether a model fits, because patterned residuals indicate the wrong model [5].

A random band of residuals around zero supports the linear fit. A U-shaped pattern suggests curvature. A funnel shape, where residuals spread out as x increases, suggests that the variance is not constant. A single point with a large residual may be driving the slope.

Anscombe's quartet makes the point concrete. Four data sets share a mean of x of 9, a mean of y of 7.50, a correlation of 0.816, and the regression line y = 3.00 + 0.500x, but their scatter plots differ completely, including a curve and a single influential outlier [3]. Identical summary statistics, different data. The residual plot is where that difference becomes visible.

Step 5: Label the Figure and Report the Fit

A finished figure carries the axis labels with units, the fitted line, and the equation and R-squared in the panel or caption. State n and the fitting method. If you used ordinary least-squares regression of y on x, say so.

Correlation measures association, not causation. Altman and Krzywinski explain how pairwise dependence is characterized with correlation and why association does not imply causation [4]. A tight line between two variables does not establish that one causes the other, and the figure should not imply that it does.

Common Mistakes

  • Swapping the axes after fitting. The slope and intercept change because least-squares minimizes vertical distances. Fix: decide the axis assignment from the study design before you fit, and keep it.
  • Reporting R-squared as proof of linearity. A high R-squared means the line explains a large share of the variance in this sample, not that the underlying relationship is linear. Fix: inspect the scatter plot and the residual plot.
  • Extrapolating beyond the standard range. A calibration curve is validated only across the calibrators. Fix: dilute and re-measure samples that fall outside the range.
  • Fitting a line through a curve. A log scale or a power law may be the right description. Fix: check whether a straight line on log-log axes corresponds to a power law, and remember that zero or negative values cannot be plotted on a log axis.
  • Letting one point set the slope. A single influential outlier can pull the line and inflate or deflate R-squared. Fix: identify it, check whether it is a data entry or assay error, and report the fit with and without it if the decision is not clear.
  • Plotting only the mean of replicates. This hides the spread. Fix: show individual points, using transparency, smaller markers, or jitter when they overlap [6].
  • Omitting n and the fitting method. Readers cannot judge the fit without them. Fix: include both in the caption.

Limitations

Least-squares regression assumes that the relationship is linear in the fitted range, that the residuals are independent, and that their variance is roughly constant. When those assumptions fail, the equation and R-squared still compute, but they describe the fit, not the data-generating process.

R-squared also depends on the range of x you sampled. With the same scatter around the line, a narrow range of x gives a lower R-squared and a wide range gives a higher one, so R-squared values from studies with different ranges are not directly comparable. The statistic is also sensitive to outliers and to the choice of model.

Correlation and regression describe association. They do not establish causation, and they do not control for confounding variables that you did not measure [4]. For prediction outside the fitted range, the line gives an estimate with no empirical support.

Frequently Asked Questions

What is the difference between a trendline and a line of best fit?

They are the same object in a simple scatter plot. The line of best fit is the least-squares line that minimizes the sum of squared vertical distances from the points. Software often calls it a trendline, and the equation it reports is the fitted model.

Does R-squared tell me whether the relationship is linear?

No. R-squared is the fraction of variance in y explained by the fitted line [2]. A curved relationship can still produce a high R-squared over a limited range, and Anscombe's quartet shows four data sets with identical R-squared and correlation but very different shapes [3]. Plot the points and the residuals.

Can I use a scatter plot trendline to predict values outside my data range?

You can compute the value, but you should not report it as a measurement. A standard curve is validated only across the calibrators, so unknowns outside that range need dilution and re-measurement. Extrapolation assumes the linear relationship continues, which the data do not show.

Why does my slope change when I swap the x and y axes?

Least-squares regression minimizes vertical distances, so it treats y as the variable with error and x as known. Swapping the axes changes which distances are minimized, which changes the slope and intercept. The two fitted lines are not simple inverses of each other.

When should I use a log scale on a scatter plot?

Use a log scale when the data span several orders of magnitude, because it spreads the points and makes the pattern readable. Remember that a straight line on log-log axes corresponds to a power law, not a linear relationship, and that zero or negative values cannot be plotted on a log axis.

References

  1. NIST/SEMATECH e-Handbook of Statistical Methods: Scatter Plot
  2. Altman N, Krzywinski M. Simple linear regression. Nature Methods, 2015
  3. Anscombe FJ. Graphs in statistical analysis. The American Statistician, 1973
  4. Altman N, Krzywinski M. Association, correlation and causation. Nature Methods, 2015
  5. NIST/SEMATECH e-Handbook: How can I tell if a model fits my data?
  6. Weissgerber TL, Milic NM, Winham SJ, Garovic VD. Beyond bar and line graphs: time for a new data presentation paradigm. PLOS Biology, 2015

Related Articles