VOOZH about

URL: https://www.geeksforgeeks.org/r-language/addition-of-lines-to-a-plot-in-r-programming-lines-function/

⇱ Addition of Lines to a Plot in R Programming - lines() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Addition of Lines to a Plot in R Programming - lines() Function

Last Updated : 15 Jul, 2025

In R, the lines() function is called to add on top of already existing plot. This is particularly helpful when you want to add more lines, such as trend lines, regression lines, or special lines, on a plot. The lines() function allows flexibility in line color, line width, and line type, with multiple choices for customizing your plot.

Syntax:

lines(x, y, col, lwd, lty)

Parameters:

  • x, y: Vector of coordinates
  • col: Color of line
  • lwd: Width of line
  • lty: Type of line

Sample Scatter plot for demonstration

Here we are going to create a scatter plot using the dataset.

Output:

👁 sample-scatter-plot

Example 1: Adding a Line to a Scatter Plot

We will here begin by generating a scatter plot and then adding points connected with lines using the function lines().

Output:

👁 scatter-plot-r

Example 2: Using lines() to Connect Points in a Scatter Plot

Here we will make a scatter plot and then we will draw lines with lines() function

Output:

👁 Image

Example 3: Adding Horizontal and Vertical Lines Using abline()

In other situations, you might prefer adding vertical or horizontal lines to the plot without calling lines(). The abline() function is helpful for drawing horizontal, vertical, or diagonal lines to the plot.

Output

👁 vertical-lines-abline
Vertical lines using abline()

Related Articles:

Comment
Article Tags:
Article Tags:

Explore