# Add Diagonal Line to Plot in R (2 Examples)

In this tutorial, Iâ€™ll illustrate how to **draw a diagonal line to a plot** in R.

Table of contents:

Letâ€™s do this:

## Example Data

Consider the following example data:

set.seed(3256733) # Create example data frame data <- data.frame(x = rnorm(100), y = rnorm(100)) head(data) # Print head of example data frame

Table 1 shows the first six data points of our example data â€“ as you can see, our data consists of two numerical columns.

## Example 1: Draw Diagonal Line to Base R Plot

Example 1 illustrates how to overlay a diagonal line on top of a Base R plot.

Letâ€™s first create a graphic without any lines:

plot(data) # Draw data without line

The output of the previous R programming syntax is shown in Figure 1 â€“ A Base R scatterplot.

Next, we can add a diagonal line to this plot using the abline function and the coef argument:

plot(data) # Draw data with line abline(coef = c(0, 1))

By executing the previous R programming syntax, we have plotted Figure 2, i.e. a Base R scatterplot with a thin black diagonal line.

In the next step, we may specify some further arguments within the abline function to adjust the design of our line. The following code displays our line in red and thicker than before:

plot(data) # Change color & thickness of line abline(coef = c(0, 1), col = "red", lwd = 5)

Looks good!

## Example 2: Draw Diagonal Line to ggplot2 Plot

This example explains how to use the ggplot2 package to draw a plot with a diagonal line.

We first need to install and load the ggplot2 package to RStudio:

install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package

In the next step, we can draw a ggplot2 scatterplot with default settings (i.e. without a line):

ggp <- ggplot(data, aes(x, y)) + # Draw data without line geom_point() ggp

The output of the previously shown code is illustrated in Figure 4 â€“ A ggplot2 scatterplot of our example data.

In the next step, we may use the geom_abline function as well as the intercept and slope arguments to add a diagonal line:

ggp + # Draw data with line geom_abline(intercept = 0, slope = 1)

The output of the previous R programming syntax is shown in Figure 5 â€“ A ggplot2 scatterplot with diagonal line.

In addition, we might use the col and size arguments to change the color and thickness of our line:

ggp + # Change color & thickness of line geom_abline(intercept = 0, slope = 1, col = "red", size = 5)

By running the previous R programming code, we have drawn Figure 6, i.e. a ggplot2 graph with thick and red diagonal line.

## Video & Further Resources

In case you need further information on the R programming code of this article, you may want to have a look at the following video on my YouTube channel. In the video, I explain the contents of this tutorial:

In addition, you might want to read the related tutorials on my website. A selection of tutorials is listed below:

- Add Polynomial Regression Line to Plot
- Add Regression Line to ggplot2 Plot in R
- Add Different Line to Each Facet of ggplot2 Plot
- Add Labels at Ends of Lines in ggplot2 Line Plot
- Add Fitted Line within Certain Range to Plot
- Add Vertical & Horizontal Line to gglot2 Plot
- Drawing Plots in R
- R Programming Overview

At this point you should know how to **add a diagonal line to a graph** in the R programming language. Donâ€™t hesitate to let me know in the comments below, if you have additional questions.