Plot columns from list of dataframes in R
Last Updated :
22 Sep, 2021
In this article, we will discuss how to plot columns from a list of dataframes in R programming language.
Note: We are taking a line plot for implementation same can be applied to any other plot.
The ggplot() method is supplemented by different geometrical shapes to indicate the type of data plotting. The geom_line() method is used to plot the data in the form of lines.
Syntax:
geom_line()
We will be using the lapply() method in base R which applies a function, which may be user defined or pre-defined to the input data object.
Syntax:
lapply(list-of-data-frame, FUN)
Parameter :
- list-of-data-frame - The list of dataframes formed.
- FUN - The FUN here is the ggplot() method along with the mappings and geometrical plot visualizations to be used.
Let's move to implementation, to understand the concept better.
Example 1: Plot columns from a list of two dataframes
R
library("ggplot2")
# create first dataframe
data_frame1<-data.frame(col1=c(rep('Jan2021',5),
rep('Feb2021',5),
rep('Mar2021',5)),
col2=rnorm(15),
col3=rep(1:5,3)
)
# create second dataframe
data_frame2<-data.frame(col1=c(rep('Jan2021',5),
rep('Feb2021',5),
rep('Mar2021',5)),
col2=rnorm(15),
col3=rep(1:5,3)
)
# creating list of dataframes
data_frames<-list(data_frame1, data_frame2)
graph<-lapply(data_frames,function(x)
p<-ggplot(x,aes(x= col3,y= col2,color=factor(col1),
group=factor(col1))) +
geom_line() +
facet_wrap(~col1)
)
print (graph)
Output
Example 2: Example to show usage of three dataframes to form the list
R
library("ggplot2")
# create first dataframe
data_frame1<-data.frame(col1=c(rep('Grp1',2),rep('Grp2',2),rep('Grp3',2)),
col2=rep(letters[1:3],2),
col3=rep(1:2,3)
)
# create second dataframe
data_frame2<-data.frame(col1=c(rep('Grp1',2),rep('Grp2',2),rep('Grp3',2)),
col2=rep(letters[1:3],2),
col3=rep(1:2,3)
)
# creating list of dataframes
data_frames<-list(data_frame1, data_frame2)
graph<-lapply(data_frames,
function(x)
p<-ggplot(x,aes(x= col3,y= col2,color=factor(col1),
group=factor(col1))) +
geom_line() +
facet_wrap(~col1)
)
print (graph)
Output
Example 3: plotting different types of dataframes
R
library("ggplot2")
# create first dataframe
data_frame1<-data.frame(col1=c(rep('Jan2021',5),rep('Feb2021',5),rep('Mar2021',5)),
col2=rnorm(15),
col3=rep(1:5,3)
)
# create second dataframe
data_frame2<-data.frame(col1=c(rep('Grp1',2),rep('Grp2',2),rep('Grp3',2)),
col2=rep(letters[1:3],2),
col3=rep(1:2,3)
)
# creating list of dataframes
df<-list(data_frame1, data_frame2)
graph<-lapply(df,
function(x)
p<-ggplot(x,aes(x= col3,y= col2,color=factor(col1),
group=factor(col1))) +
geom_line() +
facet_wrap(~col1)
)
print (graph)
Output
Similar Reads
How to plot all the columns of a dataframe in R ? In this article, we will learn how to plot all columns of the DataFrame in R programming language. Dataset in use: x y1 y2 y3 1 1 0.08475635 0.4543649 0 2 2 0.22646034 0.6492529 1 3 3 0.43255650 0.1537271 0 4 4 0.55806524 0.6492887 3 5 5 0.05975527 0.3832137 1 6 6 0.08475635 0.4543649 0 7 7 0.226460
5 min read
List of Dataframes in R DataFrames are generic data objects of R which are used to store the tabular data. They are two-dimensional, heterogeneous data structures. A list in R, however, comprises of elements, vectors, data frames, variables, or lists that may belong to different data types. In this article, we will study h
7 min read
Plot Lines from a List of DataFrames using ggplot2 in R 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 - Defaul
3 min read
How to plot multiple data columns in a DataFrame? Python comes with a lot of useful packages such as pandas, matplotlib, numpy, etc. To use DataFrame, we need a Pandas library and to plot columns of a DataFrame, we require matplotlib. Pandas has a tight integration with Matplotlib. You can plot data directly from your DataFrame using the plot() met
3 min read
How to Convert a List to a Dataframe in R We have a list of values and if we want to Convert a List to a Dataframe within it, we can use a as.data.frame. it Convert a List to a Dataframe for each value. A DataFrame is a two-dimensional tabular data structure that can store different types of data. Various functions and packages, such as dat
4 min read