I am learning how to use R to creating multiple plots with a loop, putting them on one plot (or "canvas") and saving the plot. I want the plot to be 2x2. This is the tutorial I am following: https://bookdown.org/ndphillips/YaRrr/creating-multiple-plots-with-a-loop.html
Here is my code:
library(yarrr)
# Create the loop.vector (all the columns)
loop.vector <- 1:4
png(file="saving_plot2.png",
width = 1500, height = 1200)
par(mfrow = c(2, 2)) # Set up a 2 x 2 plotting space
for (i in loop.vector) { # Loop over loop.vector
# store data in column.i as x
x <- examscores[,i]
# Plot histogram of x
hist(x,
main = paste("Question", i),
xlab = "Scores",
xlim = c(0, 100))
}
dev.off()
The plot looks decent, but is there a way to control/increase space between the subplots and add a main title to the top of this plot? I know when creating one plot in base R, you would use: par(mar=c(5,4,4,2) + 0.1)
, but I am already using par()
here.