Side by Side bar charts in R
In this article, we will discuss how to draw Bar Charts side by side in R Programming Language.
Method 1: Using par() function
To draw plots side by side par() function is used.
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.
The plots are drawn normally and independent of others. For drawing them side-by-side pass the number of rows and columns as if a grid is being defined.
Example: Plotting bar plots side by side using basic R
Method 2: Using ggplot2
In this grid.arrange() is used to arrange the plots on a frame.
grid.arrange(plot, nrow, ncol)
- plot- ggplot2 plot which we want to arrange
- nrow- Number of rows
- ncol- Number of columns
Here, plots are drawn normally and independently. Then, the function is called with these plots and the number of rows and columns in a way that a grid is being defined.
Example: Plotting multiple plots side by side using ggplot.