Open In App

Plot lines from a list of dataframes using ggplot2 in R

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to plotlines in ggplot2 from a list of data frames in the R programming language.

The ggplot2 package is used to visualize and analyze the data. The package can be downloaded and installed using the following command in R : 

install.packages("ggplot2")

The ggplot2 method in R is used to do graph visualizations using the specified data frame. It is used to instantiate a ggplot2 object. Aesthetic mappings can be created to the plot object to determine the relationship between the x and y axis respectively. Additional components can be added to the created ggplot2 object.

Syntax: ggplot(data = NULL, mapping = aes(), colour())

Arguments :

  • data – Default dataset to use for plot.
  • mapping – List of aesthetic mappings to use for plot.

Geoms can be added to the plot using various methods. The geom_line() method in R can be used to add graphical lines in the plots made. It is added as a component to the existing plot. Aesthetic mappings can also contain color attributes that are assigned differently based on different data frames. The geom_line() method has the following syntax : 

geom_line()

Example 1: 

R




# importing required libraries
library("dplyr")
library("ggplot2")
 
 
# creating the dataframes
df1 = data.frame(col1=c(1: 10), col2=rnorm(10))
df2 = data.frame(col1=c(5: 10), col2=rnorm(6))
df3 = data.frame(col1=c(2: 12), col2=rnorm(11))
 
# creating a list of dataframes
samplelist = list(df1, df2, df3)
# plotting the data
graph <- ggplot(bind_rows(samplelist, .id="data_frame"),
                 aes(col1, col2, colour=data_frame)) +
geom_line()
 
# printing the graph
print(graph)


Output

[1] "First Dataframe"

col1 col2
1 1 2.6799001
2 2 1.6732359
3 3 -0.2821830
4 4 0.6951255
5 5 0.3629730
6 6 1.6543411
7 7 0.9301622
8 8 0.6858366
9 9 1.3150289
10 10 -0.9306804

[1] "Second Dataframe"

col1 col2
1 5 -0.1813050
2 6 1.3543525
3 7 0.0810269
4 8 0.1788353
5 9 1.5264921
6 10 0.3677910

[1] "Third Dataframe"

col1 col2
1 2 -1.0602057
2 3 -0.6040208
3 4 1.9346507
4 5 0.5183120
5 6 0.7176499
6 7 0.2908290
7 8 1.4760342
8 9 0.5935123
9 10 0.3882407
10 11 0.8871490
11 12 -0.3974801
Plot lines from a list of dataframes using ggplot2 in RGeeksforgeeks

Plot lines from a list of data frames using ggplot2 in R

  • The necessary libraries, dplyr, and ggplot2, are first imported into the code. These packages each offer functions for charting and manipulating data.
     
  • The data. frame() function is used to build the three data frames df1, df2, and df3. Col1 and Col2 are the two columns that make up each data frame. The values for col2 are created at random using the rnorm() function, which generates random numbers from a normal distribution, and the values for col1 are supplied using the colon operator (:) to generate a sequence of numbers.
     
  • The three data frames df1, df2, and df3 are placed in a list called a sample list. For the purpose of producing a combined data frame, this list represents the input.
     
  • The data frames in the sample list are combined into a single data frame using the bind_rows() function from the dplyr package. The new column “data_frame” is created with the argument.id=”data_frame” to provide the source data frame for each row.
     
  • A ggplot object is initialized by calling the ggplot() function. The combined data frame is given as the data parameter, and the aesthetics are specified using aes(col1, col2, colour=data_frame). The “data_frame” column is mapped to the color aesthetic for color differentiation, while col1 and col2 are mapped to the x- and y-axes, respectively.
     
  • The data points are connected by a line layer that is added to the plot using the geom_line() function depending on their x and y values.
     

Example 2:

R




# importing required libraries
library("dplyr")
library("ggplot2")
 
 
# creating the dataframes
df1 = data.frame(col1=c(1: 10), col2=letters[1:5])
df2 = data.frame(col1=c(7: 9), col2=letters[5:7])
df3 = data.frame(col1=c(1: 6), col2=rep('e', 6))
df4 = data.frame(col1=c(5, 9, 10), col2=c('x', 'm', 'n'))
 
print("First DataFrame")
print(df1)
 
print("Second DataFrame")
print(df2)
 
print("Third DataFrame")
print(df3)
 
print("Fourth DataFrame")
print(df4)
 
# creating a list of dataframes
samplelist = list(df1, df2, df3, df4)
 
# plotting the data
graph <- ggplot(bind_rows(samplelist, .id="data_frame"),
                 aes(col1, col2, colour=data_frame)) +
geom_line()
 
# printing the graph
print(graph)


Output

[1] "First DataFrame"
> print(df1)
col1 col2
1 1 a
2 2 b
3 3 c
4 4 d
5 5 e
6 6 a
7 7 b
8 8 c
9 9 d
10 10 e
> print ("Second DataFrame")
[1] "Second DataFrame"
> print(df2)
col1 col2
1 7 e
2 8 f
3 9 g
> print ("Third DataFrame")
[1] "Third DataFrame"
> print(df3)
col1 col2
1 1 e
2 2 e
3 3 e
4 4 e
5 5 e
6 6 e
> print ("Fourth DataFrame")
[1] "Fourth DataFrame"
> print(df4)
col1 col2
1 5 x
2 9 m
3 10 n

Plot lines from a list of dataframes using ggplot2 in RGeeksforgeeks

Plot lines from a list of data frames using ggplot2 in R

  • The data frames df1, df2, df3, and df4 are created by the code and placed in a list called a sample list. The code had previously constructed each of these data frames.
     
  • The data frames in the sample list are combined into a single data frame using the bind_rows() function from the dplyr package. The new column “data_frame” is created with the.id=”data_frame” option to provide the source data frame for each row. This helps to distinguish the lines based on the sources of the data when visualizing the data.
     
  • A ggplot object is initialized by calling the ggplot() function. The data argument is the merged data frame that was produced in the previous step.
     
  • We define the plot’s aesthetics in aes(col1, col2, colour=data_frame). Here, col1 and col2 are mapped to the x and y axes, respectively, and colour=data_frame is used to map the color of the lines based on the “data_frame” column. According to which data frame it came from, each line will then have a distinct color.
     
  • An additional line layer is added to the plot using the geom_line() method. This function creates lines that reflect the data in each data frame by connecting the data points based on their x and y values.
     
  • The code enables the development of a single plot where distinct lines represent several data frames by aggregating the data frames into a single data frame and defining the relevant aesthetics.
     


Last Updated : 01 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads