Adding lines to a plot

There are many types of lines you can add to a ggplot. The first type that we will discuss are predefined lines: you tell R where to put them, like diagonals, horizontal lines and vertical lines.

Predefined lines like diagonals, vertical and horizontal lines

They are created by specific line geoms that have arguments to define where the line should be placed on the plot:

  • geom_abline(): any straight line that is not vertical or horizontal, you have to set the slope and the intercept (Y value where the line cuts the Y-axis).
  • geom_hline(): horizontal line, you have to set the yintercept (y value where the line cuts the Y axis)
  • geom_vline(): vertical line, you have to set the xintercept (x value where the line cuts the X axis)

For adding regression lines to a plot we refer to the lesson on regression.