Skip to main content
v2026.11,858 entries · CC-BY 4.0

abline in R: Add Horizontal, Vertical and Regression Lines to a Plot

How abline() adds horizontal, vertical and regression lines to an R plot, with every argument and the ggplot2 equivalents.

Written and maintained by CASRAI Editorial Board

Last updated

abline() is the base R graphics function that adds one or more straight lines to a plot that already exists: a line from an intercept and slope, a horizontal line at a y-value, a vertical line at an x-value, or the line from a fitted regression object. This guide is for researchers who need to mark a threshold, a mean or a fitted trend on a figure and get the call right the first time. It also covers the ggplot2 equivalents, because geom_abline(), geom_hline() and geom_vline() take different argument names.

What abline in R is used for in research

In practice abline() is used to overlay a reference on a figure that has already been drawn:

  • a fitted least-squares line on a scatterplot (abline(fit)), see the linear regression scatter plot guide;
  • a horizontal line at a cut-off, mean or limit (h =);
  • a vertical line at a time point, dose or threshold (v =), which is the usual answer to a “vertical line plot” question in base R;
  • a line of identity or any other line with a known intercept and slope (a =, b =).

abline() draws on the current plot, so a plot must be created first, for example with plot(). It does not create a figure by itself. If you want to draw a line from your own data points rather than from a formula, see plotting a line in R.

Install or access

abline() belongs to the graphics package, which is part of base R, so nothing needs to be installed beyond R itself (available from the R Project). The ggplot2 equivalents need the ggplot2 package, installed with install.packages("ggplot2") and loaded with library(ggplot2).

abline() arguments

The documented usage in the R reference manual (graphics package) is:

abline(a = NULL, b = NULL, h = NULL, v = NULL, reg = NULL, coef = NULL, untf = FALSE, ...)
Argument What it does ggplot2 equivalent
a, b Intercept and slope, each a single value geom_abline(intercept =, slope =)
h y-value(s) at which to draw horizontal line(s) geom_hline(yintercept =)
v x-value(s) at which to draw vertical line(s) geom_vline(xintercept =)
coef A vector of length two: intercept, then slope geom_abline() with the two values
reg An object with a coef method, such as an lm fit geom_abline() with the values from coef(fit)
untf Only relevant when an axis is log-transformed (see below) Not applicable
col, lty, lwd, xpd, lend, ljoin, lmitre Graphical parameters passed through ... colour, linetype, linewidth, alpha

Step-by-step: add a line with abline()

1. Draw the plot first

plot(mtcars$wt, mtcars$mpg)

2. Add a line from an intercept and slope

abline(a = 1, b = 2, col = 2)

This is the example form given in the reference manual: intercept 1, slope 2, drawn in colour 2.

3. Add a regression line

fit <- lm(mpg ~ wt, data = mtcars)abline(fit)

Passing a fitted model is the same as abline(coef = coef(fit)), as the manual’s example notes for an lm fit. For an intercept-only model the manual draws the line with abline(a = coef(model), b = 0, col = "blue").

4. Add horizontal and vertical lines

abline(h = 20)abline(v = 3.5, lty = 2)

Both h and v accept vectors, so abline(h = c(15, 20, 25)) draws three lines. col, lty and lwd can also be vectors for h and v, and they are recycled across the lines. Line width and type are explained in lwd in R and ggplot line types.

The same lines in ggplot2

p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()p + geom_vline(xintercept = 5)p + geom_hline(yintercept = 20)p + geom_abline(intercept = 37, slope = -5)

These are the examples in the ggplot2 reference. The three geoms do not inherit the plot’s x and y aesthetics and do not affect the scales. They are drawn using the same aesthetics as geom_line(): alpha, colour, linetype and linewidth.

Output and how to read it

abline() adds a line to the open graphics device; it does not return a new plot. Read the line against the axes of the existing plot: an h line crosses the y-axis at the value you gave, a v line crosses the x-axis at its value, and an a/b line rises by b y-units for each 1 x-unit. A fitted line from abline(fit) is a visual summary of the model, not a test of it: check the residuals and model assumptions separately, and see the scatterplots in R guide for the wider plotting workflow.

Log-transformed axes and untf

With untf = TRUE and a log-transformed axis, the line is drawn in the original coordinates, so it appears as a curve. Lines drawn with h and v always use the original coordinates.

Troubleshooting

abline() fails because no plot is open

abline() adds to the current plot, so with no plot open there is nothing to draw on. Call plot() (or another high-level plot function) first.

The ggplot2 line does not show

Lines outside the data range are invisible, and geom_abline() called with no arguments can look empty. Check that the intercept or slope values put the line inside the plotted range.

The line repeats in every facet

Arguments given directly to the geom draw the same line in every facet. To draw a different line per facet, put the values in a data frame and map them with aes().

abline(reg = fit) draws a line through the origin

This happens when the model’s coef method returns a single value (a slope-only fit). Fit the model with an intercept, or pass both values through coef, to get a line that does not pass through the origin.

Free alternatives and related functions

Task Base R ggplot2
Line from intercept and slope abline(a, b) geom_abline(intercept, slope)
Horizontal reference line abline(h = ) geom_hline(yintercept = )
Vertical reference line abline(v = ) geom_vline(xintercept = )
Line from a fitted model abline(fit) geom_abline() with values from coef(fit)

FAQ

What does abline do in R?

abline() adds one or more straight lines to the current plot: from an intercept and slope, at given y-values (h), at given x-values (v), or from a regression object (reg or coef).

How do I add a vertical line to a plot in R?

Draw the plot, then call abline(v = x) with the x-value, for example abline(v = 3.5). In ggplot2 use geom_vline(xintercept = 3.5).

How do I add a regression line with abline in R?

Fit the model with lm() and pass it to abline(), for example abline(lm(mpg ~ wt, data = mtcars)). It is equivalent to abline(coef = coef(fit)).

Can I use abline with ggplot2?

Not the function itself. ggplot2 provides geom_abline(), geom_hline() and geom_vline(), which are added to a plot with +.

Sources (official documentation)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about abline in R: Add Horizontal, Vertical and Regression Lines to a Plot

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →