Abline in R – A Quick Tutorial

Ever needed to add straight lines to an R plot? You can use abline in R to add straight lines to a plot. What is ab line? It helps you plot a line in R, and with it making lines in R has never been easier. This R function is great for adding cutoffs or similar limits to an existing R plot.

# abline in r / r plot add line

abline(a = NULL, b = NULL, h = NULL, v = NULL, reg = NULL,
       coef = NULL, col = NULL, lty = NULL, lwd= NULL)

The abline function can accept several arguments, with defaults of NULL:

  • a – The intercept of the line, as a single value
  • b – The slope of a line, as a single value
  • h – the y-value(s) for horizontal line(s)
  • v – the x-value(s) for vertical line(s).
  • coef – a vector of length two giving the intercept and slope
  • reg – a regression object generated by the coef method; intended to simplifying passing trend-lines from a regression calculation
  • col – optional color parameter for the line
  • lty – optional line type parameter
  • lwd – optional line width parameter

Initial Setup – Examples

We’re going to use the airmiles dataset for this tutorial on how to plot a line in r. This is one of the default datasets available in R.

> airmiles

# default plot for abline in r tutorial
> plot (airmiles)
airmiles data frame

As you can see, airmiles is a nice basic timeseries. It shows the growth of airline travel within the US.

Abline in R – Add Horizontal Lines

How do you draw a horizontal line in R? First, lets start by drawing a horizontal line on the graph. Maybe we are very interested in seeing when the total miles traveled is above 20,000.

# abline in R example - horizontal line

This draws a lovely horizontal line at the y = 20000 level.

abline in r - horizontal

Abline in R – Add Vertical Lines

Now for some tips on vertical line drawing. We can also easily add a vertical line to the graph, similar to a dropline in Excel. Perhaps our client would like to see a line at the year 1955.

# abline in R example - horizontal line
abline(v = 1955)

This draws a lovely horizontal line at the x = 1955 level.

abline r limit length of abline in r abline in r not working label abline in r lty in r abline in r regression abline in r ggplot2
abline in R – Vertical Line

Abline in R – Color and Line Types

Moving past basic black and white, we can easily adapt the abline function to draw lines in color and in different formats. We can edit our R plot line thickness, do a dotted abline in R, and add color to abline in R.

abline(v=1955, col='blue', lty=2, lwd=5)
dashed line in r abline r dotted abline in r ab line draw vertical line in r abline color r add liner add line to plot make ab liner plot line thickness vertical line drawing yx line line r plot regression line r r 45 degree line vertical line graph html code for vertical line what is a plot line

Abline in R – Multiple Lines

In this next example, we’re going to draw a range of two lines on the chart. There’s syntax that can simplify this for horizontal lines.

abline(c(5000, 25000), col='blue', lty=2, lwd=4)
abline(5000, 0, col='blue', lty=2, lwd=4)
abline(25000, 0, col='blue', lty=2, lwd=4)
abline in r not working label abline in r lty in r abline in r regression abline in r ggplot2 abline(lm in r) lines in r abline function in rr horizontal line abline r studio abline r example what is a b line

Abline in R – Drawing a slope

For our next example, we will demonstrate how to draw a sloped line. The abline function in R can help us plot a sloped line, which can help us plot a linear regression line in R. How do you add a trendline in R? The ab line tool can help us with that too.

A slope of 1 is a 45 degree line.

with(airquality, plot(Ozone, Temp))
abline(60, .5, col='blue', lty=2, lwd=4)
abline(60, 0, col='blue', lty=2, lwd=4)
abline(60, 1, col='blue', lty=2, lwd=4)

Abline in R can help us with a great many things. Whether you want to do abline on a histogram in R, change your R plot line thickness, or even learn how to draw in R, abline in R is a great place to start. R lines will become your best friend when plotting data, and learning simple tricks like how to limit abline in R, how to do a linear regression in R, or how to make an R 45 degree line will all make you a much better data scientist!

We hope our tutorial on abline in R was helpful, and encourage you to check out some of our other tips and tricks for everything R-related!