The lines() function is used to add lines to an existing plot in R.

The following method shows how you can do it with syntax.

Method: Use lines() Function

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

x: Vector of x-coordinates y: Vector of y-coordinates col: Color of line lwd: Line width lty: Type of line

The following example shows how to use lines() function in R:

Use lines() Function to Add Line in Plot

Let’s see how we can add line to plot using lines() function:

# Define x and y coordinate
Temperature <- c(71, 72, 73, 74, 75, 76)
Pressure <- c(11.23, 22.48, 33.78, 54.58, 65.25, 76.96)

# Create chart 
plot(Temperature,Pressure)

# Define x and y coordinate for new line
Temperature <- c(70, 71.5, 72, 73, 73.5, 74)
Pressure <- c(11.80, 22.58, 23.78, 50.58, 61.25, 66.96)

# Create line
lines(Temperature,Pressure,col='red',lwd=6,lty="dashed")

Output:

Line chart

In the above code we use plot() function to create scatter chart plot and then add new red line to plot using lines() function.