Line Graph represents relation between two variables.

## Plot a line graph in R

We shall learn to plot a line graph in R programming language with the help of plot() function.

- Syntax of Plot Function
- Examples

### Syntax of plot() function

plot(x, y, ...) |

where

is any R object with a plot method. Ex : numeric vector**x**is any R object with a plot method. Ex : numeric vector**y**- … is the extra arguments that could be provided, which may contain any of the following
– type could be any of the below values**type**- ‘p’ – points
- ‘l’ – lines
- ‘b’ – both points and lines
- ‘c’ – for the lines part alone of both points and lines
- ‘o’ – for both points and lines overplotted
- ‘h’ – generates kind of histogram view
- ‘s’ – for stair step look
- ‘S’ – other steps
- ‘n’ – no plotting

is the main title for the plot**main**is the sub title for the plot**sub**is the x label i.e., title for x-axis**xlab**is the y label i.e., title for y-axis**ylab**is the aspect ratio whose value should be given by y/x**asp**is the line width**lwd**is the point character. There are 25 symbols to choose from including alphabetic characters**pch**gives the color to points and line**col**

### Example 1 : Line Graph

# R program to plot line graph x = c(1,2,3,4,5,6,7,8,9,10,11) y = c(22,13,5,9,25,22,26,1,9,10,2) # plot function # except x,y ; rest are optional plot(x, y, type='b', main='First Plot Example', sub='Line Graph', xlab='time', ylab='cost', asp=1) |

When the above program is run, plot would be generated as a pdf file, by default.

### Example 2 : Line, No points, Colored line

# R program to plot line graph x = c(1,2,3,4,5,6,7,8,9,10,11) y = c(22,13,5,9,25,22,26,1,9,10,2) # plot function # except x,y ; rest are optional plot(x, y, type='l', col='#ff0000') |

Output Line Graph Plot

### Example 3 : Points, No Line, Colored points

# R program to plot line graph x = c(1,2,3,4,5,6,7,8,9,10,11) y = c(22,13,5,9,25,22,26,1,9,10,2) # plot function # except x,y ; rest are optional plot(x, y, type='p', col='#0000ff') |

Output Point Graph Plot

### Example 4 : Stair Step Graph

# R program to plot stair graph x = c(1,2,3,4,5,6,7,8,9,10,11) y = c(22,13,5,9,25,22,26,1,9,10,2) # plot function plot(x, y, type='s', col='#0000ff') |

Output Stair Graph Plot

## Conclusion

In this R Tutorial, we have leaned R plot function and some of the examples like plotting with both line and points, coloring the graph, drawing only points or lines on to the graph, etc.