R

# Line Plots in R

“It is a type of graph that depicts how data changes over time. Line plots, also known as line charts or line graphs, are made by connecting multiple points using straight lines. The “x-axis” (horizontal) and the “y-axis” (vertical) are the two axes used in line graphs. This type of graph depicts quantitative data over time. A line graph can be used to determine whether the quantity on the y-axis is increasing or decreasing over time. A line graph shows whether a trend is increasing or declining.”

## How to Use Line Plot in R in Ubuntu 20.04?

To create the line plot in the R programming language, the plot() function is used. Following are some parameters that are used inside the plot function.

Syntax of plot() function: plot(v, type, col, xlab, ylab).

Where v is a vector consisting of some numerical data or values, type is used to specify whether the graph will be drawn by using only points or lines or both( “p” to draw a graph only with lines, “l” to draw a graph with lines only, and “o” to draw both points and lines ), xlab and ylab are the labels for the x-axis and y-axis respectively, main is the title of the graph, and col is used to specify colors for the lines and points both.

The subsequent are some instances that will help you to figure out how to use line plots in R.

## Example # 1: Basic Line Plot in R

We create two numeric vectors, x and y, where y depends on x. x can be a sequence representing time, and y can be random values of observations. To create a plot only with lines, we will pass both vectors and specify the type =”l” in the plot() function. If we press enter on the console, it will display the line plot on the screen. As you can see, the above plot presents the y value over the x values. We can make it more readable by specifying type=”o”. Both lines and points can be seen on the plot by specifying type =”o”. These points represent the value of y over x.

## Example # 2: Labeling the Title and Axis of the Line Plot in R

We can add a title to a line plot graph by specifying the main parameter on the plot() function. The title of the graph should be meaningful, and it should describe what is being measured/observed. We can also label the x-axis and y-axis in R by using xlab for labeling the x-axis and ylab for the y-axis. Let’s suppose, in the above example, we are playing a video game, and we have to calculate the player’s points over some time. So, we can specify the title as “My score”, also xlab and ylab as “time” and “points respectively” in the parameter of plot() function. The output when we press enter will be: ## Example # 3: Change the Color of the Line in the Line Plot

R allows you to change the color of the line in the plot graph. For changing the line color, we will specify a color in the col parameter of the plot() function. Output: Instead of color names, we can also use hex color codes which are predefined in R.

## Example # 4: Change the Thickness of the Line in the Line Plot

By using the lwd option in the plot() function parameter, the width of the line can be increased or decreased in the graph. Output: The thickness of the line can be increased or decreased by changing the lwd value; the more the value, the thicker the line will be, and vice versa.

## Example # 5: Add Points Between Lines in the Line Plot

We can add points between the lines of the line plot by changing the type from “l” to “b”. As seen in example 1, by using type = “o” the lines and the points were overlapping. To avoid overlapping and make it even better to visualize, we will use type=”b” in the plot() function. Output: As you can see, the points and the line are not overlapping; type = b has created some spaces between them to visualize it better.

## Example # 6: Add Multiple Lines on the Same Graph in a Line Plot

To create multiple lines on the plot, we will create 2 or more numeric vectors to be passed as input in the plot() function. To add these vector lines to the plot, we will use the lines() function with some parameters, just like the plot() function. As you can find out, we have identified different colors so we can distinguish between the lines. Furthermore, we can also add the legend by using the legend() function to make the graph more readable for the observers. In the legend() function’s parameters, we have specified that they will reside in the top right corner of the plots. We have also described the names and colors of the lines in the legend.

Output: ## Example # 7: Different Symbols for Points in Line Plot

With the help of the pch argument inside the plot() and line() functions, we can assign different symbols for points in each line of the line plot graph. When we press enter after specifying different values for the pch argument, we will get the following output: As you can see, we have different symbols for each line. It will be easier to know the graph if we create a legend for the point symbols. Output: The legend describes the symbols of points for each line of different colors.

## Conclusion

In this tutorial, we have tried to teach you the basic concept of line plots in the R programming language. Now you should be able to create line plots, and you should also be able to stylize them according to your requirement. In this post, we have implemented seven examples to demonstrate how to create line plots in R, how to label line plots and their components, how you can stylize them, and how to use different arguments/functions with the plot() function to have better visualization in line plots. 