When you are making plots in base R sometimes you will want to create several plots side-by-side or in a grid format. To do that you use
par(mfrow = c(nr, nc)) where
nr is the number of rows you want, and
nc is the number of columns. You can also use
mfcol if you want your plots to first go down the columns and then fill in the rows.
Let’s say you wanted to create three plots side-by-side and used
par(mfrow = c(1, 3)), but now you want to create a single plot again. Creating this plot, you will find that R will only use the left-hand side of the plotting area. For example, the following code will produce the chart shown in Figure 1.
par(mfrow = c(1, 3))
# some other charts and code herex <- rnorm(1000, 5, 2)
box(which = "figure") # draws a box around the figure
We would like the see our histogram take up the full space for the plot, and we don’t want subsequent plots to appear in this figure. To do that we can simply reset the value of the
mfrow parameter to
par(mfrow = c(1, 1))
We could alternatively shut down our graphics device by calling the function
dev.off(). The next time we created a plot R would start up a new graphics device with the default settings. However, this solution will remove any unsaved plots you’ve made in your session so far.
Either way, the next time we call
hist(x) we will see something like Figure 2.
This plot takes up the full area as we wanted.