Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Find the Line of Best Fit in Excel

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To find the line of best fit in Excel, put your x-values in one column and y-values in the next, select both columns, then insert a Scatter chart. Right-click any data point, choose Add Trendline, pick Linear, and tick Display Equation on chart and Display R-squared value on chart. The equation shown is your line of best fit in the form y = mx + b. That is the whole job on a modern Excel for Windows or Mac; the rest of this guide covers doing it cleanly, reading the output, and fixing the cases where the trendline refuses to appear or gives nonsense.

What You'll Need

  • Excel for Windows, Excel for Mac, or Excel for the web. The chart and trendline features described here exist in all three; exact menu labels can differ slightly by version, device and region.
  • Two columns of numeric data: one independent variable (x) and one dependent variable (y). Each row is one observation, and the two columns must be the same length with no blank rows in the middle.
  • At least three or four data pairs for a meaningful line. Two points define a line exactly but tell you nothing about fit.

Step-by-Step

  1. Lay the data out in two adjacent columns. Put x in column A and y in column B, with a header in row 1 such as "Hours" and "Score". Do not leave blank rows between observations, and do not put the two series side by side with a gap column between them.
  2. Select the data range, including headers. Click the top-left cell and drag to the bottom-right, or click A1 and press Ctrl+Shift+End (Cmd+Shift+End on Mac). Headers help Excel label the axes automatically.
  3. Insert a scatter chart. Go to Insert > Charts > Scatter and choose the plain "Scatter" option (markers only, no connecting lines). A line chart is not the same thing: it spaces categories evenly along the x-axis and will distort a numeric relationship. If you only see recommended charts, open the full chart gallery and pick Scatter from there.
  4. Right-click any data point and choose Add Trendline. A formatting pane opens on the right. If the right-click menu does not show it, click the chart once, then use the plus sign (+) that appears at the top-right corner of the chart and tick Trendline.
  5. Choose Linear in the trendline options. Linear is the default and is what most people mean by a line of best fit. Leave the other types (Exponential, Logarithmic, Polynomial, Power, Moving Average) alone unless your data clearly curves.
  6. Tick "Display Equation on chart" and "Display R-squared value on chart". Both options sit at the bottom of the trendline pane. The equation appears on the chart in the form y = mx + b, where m is the slope and b the intercept. R-squared tells you how much of the variation in y is explained by x; closer to 1 is a tighter fit.
  7. Read the equation. If Excel shows y = 2.5x + 10, then each one-unit increase in x is associated with a 2.5-unit increase in y, and the predicted y when x is zero is 10. To predict a value, type the x into a cell and rebuild the formula, for example =2.5*A2+10, rather than eyeballing the chart.
  8. Optionally forecast beyond your data. In the same trendline pane, under Forecast, set Forward or Backward to a number of periods. Only do this a short distance past your last point; a straight line extended far beyond the data is a guess, not a prediction.
  9. Get the exact coefficients without reading the chart. If you want slope and intercept as live numbers, use =SLOPE(known_ys, known_xs) and =INTERCEPT(known_ys, known_xs), and =RSQ(known_ys, known_xs) for R-squared. Note the order: y range first, x range second. These update automatically if the data changes, unlike a chart label.
  10. Save and label. Add axis titles via the chart's plus button (Axis Titles), and rename the chart so anyone opening the file knows what it shows. A trendline without units on the axes is easy to misread later.

Common Mistakes

  • Using a line chart instead of a scatter chart. This is the single most common error. A line chart treats the x-values as evenly spaced labels, so the trendline and the visual slope are both wrong.
  • Including text, blanks or totals in the range. A "Total" row at the bottom of the data gets plotted as a real observation and drags the line toward it. Select only the actual observations.
  • Swapping x and y in the SLOPE and INTERCEPT functions. Both take the y range first. Reversing them gives a different, wrong line.
  • Reading R-squared as proof of cause. A high R-squared means the straight line describes the points well; it says nothing about whether x causes y.
  • Forcing a linear fit on curved data. If the points form a clear arc, a straight line through them is misleading. Try a different trendline type or transform the data first.
  • Leaving the equation floating over the plot area where it covers data points. Drag the label to an empty corner.

Tips

  • Format the trendline to stand out: right-click it, choose Format Trendline, and set a distinct line colour and a dash style so it is not confused with a data series.
  • Add data labels to the points if you need to identify outliers, then remove them again before sharing the chart.
  • Keep the raw data and the chart on separate sheets once the workbook grows; it makes the chart easier to resize and the data easier to audit.
  • If you need the full regression output (standard errors, p-values, confidence intervals), enable the Analysis ToolPak via File > Options > Add-ins, then use Data > Data Analysis > Regression. The chart trendline gives you the equation and R-squared only.
  • Round the displayed equation to a sensible number of decimals by formatting the label, but keep the underlying SLOPE and INTERCEPT values unrounded for calculations.

Troubleshooting

  • The trendline option is greyed out. You are almost certainly on a chart type that does not support trendlines, such as a pie, doughnut or stacked chart. Switch to a scatter chart.
  • No line appears after you add the trendline. Check that the selected range contains numbers, not numbers stored as text. Text-formatted values are ignored. Select the column, use Data > Text to Columns and finish, or multiply by 1 in a helper column, to convert them.
  • The equation is missing or blank. The Display Equation checkbox was not ticked, or the label is hidden behind the plot. Reopen the trendline pane and tick it again, then drag the label clear.
  • The line looks wrong or flat. Confirm you selected both columns and that x is the left-hand column. A flat line usually means the x range was not included, so Excel plotted the row number instead.
  • R-squared is very low. That is information, not a bug: the straight-line relationship is weak. Check for outliers, consider a curved trendline type, or accept that the variables are only loosely related.
  • The chart shows dates on the x-axis oddly. Excel may treat dates as a date axis. Right-click the axis, choose Format Axis, and set it to a text or numeric axis as appropriate, or convert dates to serial numbers.

Frequently Asked Questions

What does the equation on the chart actually mean?

It is the formula of the straight line that minimises the total squared vertical distance from the points to the line. In y = mx + b, m is the slope (how much y changes per unit of x) and b is the intercept (predicted y when x is zero).

Can I get the slope and intercept as cell values instead of a chart label?

Yes. Use =SLOPE(y_range, x_range) and =INTERCEPT(y_range, x_range). They return live numbers you can reference elsewhere, and =RSQ(y_range, x_range) returns R-squared.

Does a line of best fit prove that one thing causes another?

No. It describes the strength and direction of a straight-line association in your data. Causation requires a proper study design, not a trendline.

Why is my trendline different from what I calculated by hand?

Usually because the selected range includes an extra row, a header treated as data, or a text-formatted number. Recheck the range and the number format of both columns.

Can I add a trendline to a chart in Excel for the web?

Yes, scatter charts in Excel for the web support trendlines, including the equation and R-squared display. Labels and menu wording may differ slightly from the desktop version.

How many data points do I need?

Technically two, but a line through two points tells you nothing about fit. Use at least several pairs, and treat any R-squared from a handful of points as indicative only.