Side by Side Line charts in R
In this article, we will discuss how to make side by side line chart in R Programming Language.
Method 1: Using par() function
Here we will use par() function to divide frames into grids.
Syntax: par(mfrow, mar, mgp, las)
- mfrow: A numeric vector of length 2, which sets the rows and column in which frame has to be divided.
- mar: A numeric vector of length 4, which sets the margin sizes in the following order: bottom, left, top, and right.
- mgp: A numeric vector of length 3, which sets the axis label locations relative to the edge of the inner plot window.
- las: A numeric value indicating the orientation of the tick mark labels and any other text added to a plot after its initialization.
Here, by changing row and column values, we can draw graphs in them. For drawing side-by-side plots we will keep row as 1 and column as the no. of plots required.
Method 2: Using ggplot2
Here we will use gridExtra contains a function called arrange() that is used to arrange plots as desired.
Syntax: grid.arrange(plot, nrow, ncol)
- plot: ggplot2 plot which we want to arrange
- nrow: Number of rows
- ncol: Number of columns
Here, by changing nrow and ncol values, we can draw graphs in them. For drawing side-by-side plots we will keep nrow as 1 and ncol as the no. of plots required.