Line Graph in R

The line graph in R has a line that connects all the points in a diagram. With the help of plot() functions you can create a line and add the type parameter with a value of ā€œlā€ in R programming:

plot(1:20, type="l")


Line 1

Line Color

To change the color you can use col parameter, remember the color is black by default.

plot(1:20, type="l", col="red")


Line 2

Line Width

For the line width change the lwd parameter is used (1 is default, while 0.5 means 50% smaller, and 2 means 100% larger)

plot(1:20, type="l", lwd=0.5)


Line 3

Line Styles

By default, this line is solid. For specifying the line format we used the lty parameter with a value from 0-6. For example, lty=2 will display a dashed line instead of a solid line:

plot(1:20, type="l", lwd=5, lty=2)


Line 4

Available parameter values for lty:

  • 0 erase the line
  • 1 means a solid line
  • 2 means a dashed line
  • 3 means a dotted line
  • 4 means a “dot dashed” line
  • 5 means a “long dashed” line
  • 6 means a “two dashed” line

Multiple Line graph in R

To display multiple line in a graph, the plot() function can be used together with the lines() function:

l1 <- c(1,2,3,4,5,10)
l2 <- c(2,5,7,8,9,10)

plot(l1, type = "l", col = "green")
lines(l2, type="l", col = "blue")


Line 5