The Wayback Machine - https://web.archive.org/web/20240724234449/https://www.geeksforgeeks.org/r-line-graphs/
Open In App

R – Line Graphs

Last Updated : 09 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

A line graph is a chart that is used to display information in the form of a series of data points. It utilizes points and lines to represent change over time. Line graphs are drawn by plotting different points on their X coordinates and Y coordinates, then by joining them together through a line from beginning to end. The graph represents different values as it can move up and down based on the suitable variable.

R – Line Graphs

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

Syntax: plot(v, type, col, xlab, ylab)

Parameters: 

  • v: This parameter is a contains only the numeric values
  • type: This parameter has the following value: 
    1. “p” : This value is used to draw only the points.
    2. “l” : This value is used to draw only the lines.
    3. “o”: This value is used to draw both points and lines
  • xlab: This parameter is the label for x axis in the chart.
  • ylab: This parameter is the label for y axis in the chart.
  • main: This parameter main is the title of the chart.
  • col: This parameter is used to give colors to both the points and lines.

Creating a Simple Line Graph

Approach: In order to create a line graph: 

  1. It is created using the type parameter as “o” and input vector.
  2. Below code to describe the line graph.

Example: 

R




# Create the data for the chart.
v <- c(17, 25, 38, 13, 41)
 
# Plot the bar chart.
plot(v, type = "o")


Output: 

Adding Title, Color and Labels in Line Graphs in R

Approach: To create a colored and labeled line chart. 

  1. Take all parameters which are required to make line chart by giving a title to the chart and add labels to the axes.
  2. We can add more features by adding more parameters with more colors to the points and lines.

Example: 

R




# Create the data for the chart.
v <- c(17, 25, 38, 13, 41)
 
# Plot the bar chart.
plot(v, type = "o", col = "green",
    xlab = "Month", ylab = "Article Written",
    main = "Article Written chart")


Output: 

Multiple Lines in a Line Graph in R Programming Language

Approach: To create multiple line graphs. 

  1. In above example, we created line graphs by only one line in each graph.
  2. Now creating multiple lines to describe it more clearly.

Example: 

R




# Create the data for the chart.
v <- c(17, 25, 38, 13, 41)
t <- c(22, 19, 36, 19, 23)
m <- c(25, 14, 16, 34, 29)
 
# Plot the bar chart.
plot(v, type = "o", col = "red",
    xlab = "Month", ylab = "Article Written ",
    main = "Article Written chart")
 
lines(t, type = "o", col = "blue")
lines(m, type = "o", col = "green")


Output: When we execute the above code, it shows the following result- 



Previous Article
Next Article

Similar Reads

Plotting Graphs using Two Dimensional List in R Programming
List is a type of an object in R programming. Lists can contain heterogeneous elements like strings, numeric, matrices, or even lists. A list is a generic vector containing other objects. Two-dimensional list can be created in R programming by creating more lists in a list or simply, we can say nested lists. The list() function in R programming is
2 min read
R - Charts and Graphs
R language is mostly used for statistics and data analytics purposes to represent the data graphically in the software. To represent those data graphically, charts and graphs are used in R. R - graphs There are hundreds of charts and graphs present in R. For example, bar plot, box plot, mosaic plot, dot chart, coplot, histogram, pie chart, scatter
6 min read
Draw Multiple Graphs and Lines in Same Plot in R
A visualization can sometimes make more sense when multiple graphs and line plots are combined into one plot. In this article, we will discuss how we can do the same in the R programming language. Method 1: Using base R Base R supports certain methods that can be used to generate the desired plot. In this example plot, a scatter plot, a line plot,
3 min read
Create interactive ggplot2 graphs with Plotly in R
"A Picture is worth a thousand words," and that picture would be even more expressive if the user could interact with it. Hence the concept of "interactive graphs or charts. Interactive charts allow both the presenter and the audience more freedom since they allow users to zoom in and out, hover and read information related to the marker, get toolt
6 min read
How To Put Multiple Graphs In One Plot With Ggvis in R
In this article, we will be looking at the approach to putting multiple graphs in one plot using ggvis in the R programming language. The ggplot2 library in R is used to make a graphical representation of the data provided. The package can be downloaded and installed into the working space using the following command : install.packages("ggplot2") A
3 min read
How to personalize easily ggplot2 graphs in R
In this article, we are going to learn how to personalize easily ggplot2 graphs in the R programming language. Data visualization is an essential tool for understanding and communicating complex data sets. One of the most popular and powerful visualization libraries in R is ggplot2, which offers a wide range of options for creating visually appeali
11 min read
Grouped Bar Graphs and Facet_Wrap in R
In this article, we are going to learn how to define data when using ggsignif with grouped bar graphs and facet_wrap in R programming language. ggplot2 is a popular R Language package used for data visualization. It allows users to create a wide range of plots and graphs, including bar graphs. However, adding statistical significance bars to bar gr
8 min read
Exploratory Graphs for EDA in R
Exploratory Data Analysis (EDA) is a crucial step in the data science process that helps to understand the underlying structure of a data set. One of the most efficient ways to perform EDA is through the use of graphical representations of the data. Graphs can reveal patterns, outliers, and relationships within the data that may not be immediately
6 min read
Lattice Graphs in R
Lattice graphs in R are a type of graphical representation that uses a grid-like structure to display data. They are commonly used in statistics and data visualization to show the relationship between multiple variables. In R Programming Language, lattice graphs can be created using the lattice package, which provides a variety of functions for cre
6 min read
Saving Graphs as Files in R
In this article, we are going to learn how to save graphs to files in the R programming language. R Saving Graphs as Files Graphs are descriptive R objects which facilitate the visual representation of data in R. The data points become more understandable and readable when expressed in the form of plots. The plots can also be saved within the local
4 min read
Article Tags :
three90RightbarBannerImg