YouTip LogoYouTip

R Func Abline

# R abline() Function - Add Reference Lines [![Image 3: R Language Examples](#) R Language Examples](#) The R abline() function is used to add straight reference lines to an existing plot. It can draw horizontal, vertical or diagonal lines, commonly used to mark means, thresholds or trend lines. The abline() function syntax is as follows: abline(a = NULL, b = NULL, h = NULL, v = NULL, col = "black", lty = 1, lwd = 1) **Parameter Description:** * **a, b** The intercept and slope of the line, i.e., y = a + b*x. * **h** The y value of the horizontal line. * **v** The x value of the vertical line. * **lty** Line type: 1=solid, 2=dashed, 3=dotted. * **lwd** Line width. ## Example # Scatter Plot x <-1:20 y <- x *3+rnorm(20, 0, 5) plot(x, y, main ="Data and Trend Line", xlab ="X", ylab ="Y", col="blue", pch =16) # Add Regression Trend Line abline(lm(y ~ x), col="red", lwd =2) # Add Horizontal Reference Line (mean of y) abline(h =mean(y), col="green", lty =2, lwd =2) # Add Vertical Reference Line abline(v =10, col="purple", lty =3, lwd =2) # Legend legend("topleft", legend=c("Regression Line", "Mean", "x=10"), col=c("red", "green", "purple"), lty =c(1, 2, 3), lwd =2) Executing the above code will display a scatter plot containing a regression line, mean line and vertical reference line. [![Image 4: R Language Examples](#) R Language Examples](#)
← R Func AovCodex Permission Settings β†’