R Line Graphs

Line graph or line chart is a graph that connects a series of points by drawing segments between them. A line graph represents the relationship between 2 variables. These points are ordered in one of their coordinates (usually the x- coordinate) value.

The plot() function is R is used to create the line graph.

Syntax:

v is a vector containing the numeric values.

type is used to set the value if it is ‘p’ then it draws only the points, if ‘I’ is used to draw the lines and if “o” then is used to draw both points and lines.

xlab is the label for the x-axis

ylab is the label for the y-axis

main is the title of the chart

col is for the color to both points and lines

Example 1:

Let’s create a simple line chart using the input vector and type parameter as “o”.

Output:

R Line Graph Example 1

Example 2:

Let’s create a simple line chart using the input vector and type parameter as “p”.

Output:

R Line Graph Example 2

Line Chart with Title, Color, and Label

We can add more parameters to expand the feature of a line chart. We can add the color parameter, give the title to the chart and we can also add labels to the axes.

R Line Graph Example 3

Multiple Lines in a Line Graph

We can draw more than one line on the same chart by using the lines() function. Let’s see an example.

Example:

Output:

R Line Graph example 4