QUESTION: How do I make a grid with 2x2 plots in it using base R?

Plots are commonly used in data analysis, and sometimes we want to display two plots at the same time. How do we do that?

Data

We’ll use the “palmerpenguins” packages (https://allisonhorst.github.io/palmerpenguins/) to address this question. You’ll need to install the package with install.packages(“palmerpenguins”) if you have not done so before, call library("“palmerpenguins”), and load the data with data(penguins)

#install.packages("palmerpenguins")
#install.packages("ggpubr")
library(ggpubr)
## Loading required package: ggplot2
library(palmerpenguins)
## Warning: package 'palmerpenguins' was built under R version 4.1.2
data(penguins)
penguins
## # A tibble: 344 x 8
##    species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
##    <fct>   <fct>              <dbl>         <dbl>             <int>       <int>
##  1 Adelie  Torgersen           39.1          18.7               181        3750
##  2 Adelie  Torgersen           39.5          17.4               186        3800
##  3 Adelie  Torgersen           40.3          18                 195        3250
##  4 Adelie  Torgersen           NA            NA                  NA          NA
##  5 Adelie  Torgersen           36.7          19.3               193        3450
##  6 Adelie  Torgersen           39.3          20.6               190        3650
##  7 Adelie  Torgersen           38.9          17.8               181        3625
##  8 Adelie  Torgersen           39.2          19.6               195        4675
##  9 Adelie  Torgersen           34.1          18.1               193        3475
## 10 Adelie  Torgersen           42            20.2               190        4250
## # ... with 334 more rows, and 2 more variables: sex <fct>, year <int>

making 4 plots

we can make 4 plot with the function par()

par(mfrow = c(2,2), 
    mar = c(0,0,2,1))

plot(penguins$bill_length_mm,penguins$bill_depth_mm)
plot(penguins$bill_length_mm,penguins$bill_length_mm)
plot(penguins$bill_length_mm,penguins$bill_length_mm)

plot(penguins$bill_length_mm,penguins$bill_depth_mm)

the mfrow asks for the number of row and column, in this case is 2 row and 2 column for our four graphs

Additional Reading

For more information on this topic, see https://www.rdocumentation.org/packages/graphics/versions/3.6.2/topics/par

Keywords

data.frame
plot()
par()
c()
palmerpenguins