Open In App

Plot Lines from a List of DataFrames using ggplot2 in R

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

For data visualization, the ggplot2 package is frequently used because it allows us to create a wide range of plots. To effectively display trends or patterns, we can combine multiple data frames to create a combined plot.

Syntax:

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

Parameters:

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

Installation of Required Packages

To get started, you need to install and load the necessary libraries:

R
install.packages("ggplot2")
install.packages("dplyr")
library(ggplot2)
library(dplyr)

Example 1: Plotting Multiple DataFrames

Here, we will create a list of data frames and plot them combined using ggplot2. Each data frame will be a set of values, and the lines from each data frame will be separated by colors

R
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))

samplelist = list(df1, df2, df3)

# plotting the data
graph <- ggplot(bind_rows(samplelist, .id="data_frame"), 
                 aes(col1, col2, colour=data_frame)) +
geom_line()

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_line

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

Explanation:

  • bind_rows() binds the data frames together, and .id creates a new column, data_frame, to specify the source of each row.
  • aes() maps col1 to the x-axis, col2 to the y-axis, and data_frame to the color aesthetic.
  • geom_line() is used to place lines on the plot according to the data points. 

Example 2: Handling Non-Numeric Data

Here, we deal with non-numeric data like characters or letters in the columns and apply geom_line() to plot the data.

R
d1 = data.frame(col1=c(1: 10), col2=letters[1:5])
d2 = data.frame(col1=c(7: 9), col2=letters[5:7])
d3 = data.frame(col1=c(1: 6), col2=rep('e', 6))
d4 = data.frame(col1=c(5, 9, 10), col2=c('x', 'm', 'n'))

print("First DataFrame")
print(d1)
print("Second DataFrame")
print(d2)
print("Third DataFrame")
print(d3)
print("Fourth DataFrame")
print(d4)

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()

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_line

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

Explanation:

  • The columns contain alphabetic characters instead of numeric values,but the method to combine and plot them is exactly the same.
  • The plot created will display lines joining the points according to character data, where each line has a different color.

Related Articles:



Next Article
Article Tags :

Similar Reads