Base R provides several functions to add segments and arrows to the plots. In this guide we are going to use the following sample plot:
abline
function
The abline
function allows drawing lines, such as horizontal lines (h
argument), vertical lines (v
argument), lines based on a intersection and a slope (a
and b
arguments) or for plotting a regression line.
Horizontal line
The h
argument allows you to set the Y-axis value where to draw a horizontal line.
Vertical line
The v
argument allows you to set the X-axis values where to draw vertical lines.
Horizontal and vertical line at the same time
You can also add vertical and horizontal lines at the same time specifying both arguments.
Intercept and slope
In addition to horizontal or vertical lines, you can also specify an intercept with the argument a
and the slope of the line with the argument b
.
Regression line
The abline
function also allows drawing linear regression lines from a model.
Line customization
The same customization of lines in base R can be applied to this and the other functions of this guide.
segments
function
This function is very similar to abline
, but you can specify the starting and end points of the lines with x0
, x1
, y0
and y1
arguments.
arrows
function
You can set the arrows the same way as segments, specifying the start and end of the arrow on each axis.
In addition to modifying the color, width or line type, the arrows
function also allows customizing the length and the angle of the arrowhead.
See also