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 data frame add diagonal line

 

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

 

r graph figure 1 add diagonal 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))

 

r graph figure 2 add diagonal line

 

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)

 

r graph figure 3 add diagonal line

 

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

 

r graph figure 4 add diagonal line

 

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)

 

r graph figure 5 add diagonal line

 

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)

 

r graph figure 6 add diagonal line

 

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:

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top