This article aims to provide an in-depth understanding of the par()
function, its uses, and its applications in R programming.
Introduction to the par() Function
The par()
function in R helps manage the graphical parameters, which define how plots are displayed on the screen or a file. It sets or queries graphical parameters. The parameters cover everything from plot type, axes, background colors, text orientation, and much more.
The basic syntax of the par()
function in R is as follows:
par(...)
Here, ...
represents a list of named graphical parameters. This syntax allows for the setting or adjustment of the specified graphical parameters.
If no parameters are provided, the par()
function returns a list of all the current graphical parameters. If you provide names of graphical parameters, par()
will return the current values of those parameters. If you supply names and values of parameters, par()
will set the parameters to the provided values.
Common Graphical Parameters
Here is a brief list of some commonly used graphical parameters that you can control with the par()
function:
mfrow
: A numeric vector of the formc(n, m)
. This fills the device withm x n
plots in rows.mfcol
: Similar tomfrow
, but fills the device with plots in columns.mar
: A numeric vector of the formc(bottom, left, top, right)
which gives the number of lines of margin to be specified on the four sides of the plot.oma
: A numeric vector of the formc(bottom, left, top, right)
which gives the number of lines of outer margin.las
: A numeric value indicating the style of axis labels.pch
: The plotting character or symbol to be used in plots.cex
: A numeric value giving the amount by which plotting text and symbols should be scaled relative to the default.
Basic Usage of the par() Function
Suppose we want to create a plot with a larger bottom margin than the default. We can use the par()
function to accomplish this:
# Set the margin
par(mar = c(5, 4, 4, 2) + 0.1)
# Create a plot
plot(1:10, main = "Example Plot")

In this code, the par()
function is used to set the mar
parameter, which controls the size of the margins around the plot. The values are given in the form c(bottom, left, top, right)
, and they represent the number of lines of margin on the four sides of the plot.
Creating Multiple Plots in One Figure
The par()
function can also be used to create multiple plots in one figure using the mfrow
or mfcol
parameters. Here’s an example:
# Set the graphical parameter to create a 2x2 plot
par(mfrow = c(2, 2))
# Create four plots
plot(1:10, main = "Plot 1")
plot(1:10, main = "Plot 2")
plot(1:10, main = "Plot 3")
plot(1:10, main = "Plot 4")

In this code, the par()
function is used to set the mfrow
parameter to c(2, 2)
, which indicates that the plotting device should be divided into a 2×2 grid, and the plots should be filled in by rows.
Adjusting Text Size and Symbol Scaling
With par()
, you can also adjust the text size and symbol scaling for your plots:
# Set the graphical parameter for text size and symbol scaling
par(cex = 1.5, pch = 19)
# Create a scatter plot
plot(1:10, 1:10, main = "Scatter Plot")

The cex
parameter controls the amount by which text and symbols are scaled. The pch
parameter determines the symbol used in the plot.
Restoring Default Parameters
It’s important to note that changes made with par()
are permanent for the current R session. Therefore, it’s a good practice to save the original settings before making changes, and then restore the original settings after making the plot:
# Save original settings
op <- par(no.readonly = TRUE)
# Change parameters and make a plot
par(mar = c(5, 4, 4, 2) + 0.1)
plot(1:10, main = "Example Plot")
# Restore original settings
par(op)
In this code, the original settings are saved in the object op
before any changes are made. After the plot is made, the original settings are restored using par(op)
.
Conclusion
The par()
function in R is a versatile and powerful function for controlling graphical parameters. By mastering its use, you can create a wide variety of plots tailored to your specific needs. Whether it’s adjusting margins, creating multiple plots in a single figure, or scaling text and symbols, the par()
function allows for fine-grained control over your plots in R. As a good practice, remember to save and restore the original settings to ensure changes don’t persist across your R session.