idriss tsafack

# How to use abline function in R for machine learning

Hi there and welcome to this new post!

In this post, I will show you how to use the abline() function in R. So let's get started!

Indeed, the abline() function is commonly used for adding a straight line throught the current plot or graph with R software. This is practical tool when you want to highlight a trend or the correlation between two variables in the context of data science or machine learning, even in artificial intelligence. Also the abline() function can be used to add horzontal line, vertical line or an oblic line.

**The Function usage**

Basically, the function is presented as follows

where in arguments:

"a" and "b" represent the intercept and slope values, respectively. It is used when you know the intercept and the slope of the line.

"untf" is a logical option asking if you want to untransform the data. This can be useful when we need to transform data to log type for a better display. If "untf" is TRUE, one or both axes are log-transformed.

"h" is the option that permit to display an horizontal line (the y-values)

"v" is the option that permit to display a vertical line (the x-values)

"coef" is a vector of 2 values representing the intercept and the slope.

We can also add the "reg" option in replacement of "coef" function.

**abline() function for displaying horizontal line**

Here is a simple way to display a horizontal line to a graph. Basically, here is the following code.

Basically, for the abline function, we need only to specify the value of "h" option. For the example here we specify the horizontal line at the value zero. Here is the obtained plot.

**abline() function for displaying vertical line**

Similiarly, if we want to display a vertical line on a graph, we need to use the option "v" in replacement of "h". For this case, we will display the vertical line at the value zero. Then, we use the following code.

Here is the graph we obtain

**abline() function for displaying a regression line**

To display a regression line or a specific oblic line, we need to know the intercept and the slope of the line. Here we can use the options "a" and "b" in the abline function or the "coef" option in the argument. Here is the code

Here is the plot of the graph

To display the regression line, we use the following code

Hence, we obtain the following graph

**abline() function : modify the color, width and type of the line**

In the previous section, we shown how to add a line to a plot. For a better presentation, one would like to highlight the trend or regression line. Then, we can use additional options like "col" for the color of line, "lty" for the line type, "lwd" for the width of line. Here is the code.

Here is the graph.

*Okay, we that's all for this post. I hope you enjoyed it and it helped you. If you liked it, please share with your data science or machine learning community. If you have any question, feel free to write a comment. Also you can *__contact me via the contact page__* for any collaboration. *