Subplots in R
Explore how to create multiple subplots in R using base R's par() function and ggplot2's grid.arrange(). Understand layout settings like rows, columns, margins, and proportions to display several charts in a single view. This lesson helps you compare datasets visually and present data more comprehensively.
We'll cover the following...
In data analysis, we frequently need to create more than one chart to compare different datasets side by side, which may help us spot useful trends and patterns.
Also, we use supplementary charts to provide additional information to our audience. This helps us to convey our story in a more comprehensive style.
Subplots in base R
We use the par() function before the plot functions to output different charts next to each other. It allows us to design a layout for the charts that we will create. We use the plot functions after the par() function as usual. Here are the arguments that the function can take:
-
mfrowandmfcol: These set the layout of the plotting region in terms of rows and columns. The plots are filled into the matrix by rows as per themfrowvalue and by columns as per themfcolvalue.
If we setmfcol = c(2,3), there will be columns and ...