How to add straight lines to a plot in R

How to add straight lines to a plot in R, The R function abline adds straight lines to a plot.

Basic R Syntax: The abline function’s basic R programming syntax is provided below.

abline(h = 1)                 

In the following sections, Will show six examples of how to use the abline function in R.

How to add straight lines to a plot in R

Consider the following example data.

x <- rnorm(1000)
y <- rnorm(1000) + 0.4 * x

The previous R code generated two vectors, each with 1000 numeric values. The two vectors are linked.

Following that, we can plot our example data:

plot(x, y)                  

The previous R syntax produced a scatterplot with no lines, as shown in Figure 1.

It should be noted that we could use any other type of graphic in this tutorial (e.g. boxplots, barcharts, histograms, line charts and so on).

Is It Difficult to Learn Data Science » finnstats

Example 1: Using the abline Function, draw a horizontal line to plot.

In this example, I’ll show you how to use the abline function to add a horizontal line to our example plot.

For this task, we must include the h argument in the abline command:

plot(x, y)                    
abline(h = 1.3)               

We created a graph with a straight line at y-axis position 1.3, as shown in Figure 2.

Example 2: Using the abline Function, draw a vertical line to plot.

In this Section, I’ll show you how to add a vertical line to your plot. We simply need to specify the v argument within the abline function, as in Example 1.

plot(x, y)                    
abline(v = 1.3)                

Figure 3 depicts the output of the previously demonstrated syntax: An xy-plot with a vertical line at x=1.3.

Best Data Science Algorithms » finnstats

Example 3: Using the abline Function, draw multiple lines to plot.

This Section shows how to insert multiple straight lines into a graph. In this example, we will add a vertical and horizontal line to our plot:

plot(x, y)                    
abline(v = 1.3)               
abline(h = 1.3)               

The previously shown R syntax resulted in a plot with two lines, as shown in Figure 4.

Example 4: Using the abline Function, change the colour, type, and thickness of a line.

The R syntax below explains how to change the colour, line type, and line thickness.

The colour can be changed with the col argument, the line type with the lty argument, and the line width with the lwd argument:

plot(x, y)                    
abline(v = 1.3,               
col = "blue",           
lty = "dashed",  lwd = 5)             

The preceding syntax resulted in a scatterplot with a thick blue line with a dashed line type, as shown in Figure 5.

Data Science applications in Retail » finnstats

Example 5: Draw a line with an intercept and a slope. Utilizing the abline Function

So far, we’ve only used the h and v arguments to specify line positioning. In Example 5, I’ll show you how to draw a straight line using an intercept (also known as a constant) and a slope.

We can specify the intercept of the abline function with the argument and the slope with the b argument:

plot(x, y)                    
abline(a = 0, b = 0.75)       

With the previous R syntax, we created a graphic with a skewed straight line, as shown in Figure 6.

Where can I find Data Science Internships » finnstats

Example 6: Using the abline Function, draw a regression line to plot.

In the previous example, we manually defined the intercept and slope. I’ll show you how to use the intercept and slope of a linear regression model in this example.

The lm function can be used to model linear regression. We simply need to set the abline function’s reg argument to the output of the lm function:

plot(x, y)                    
abline(reg = lm(y ~ x))       

With the previous syntax, we created a scatterplot with a regression line, as shown in Figure 7.

You may also like...

Leave a Reply

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

17 − 4 =