R par() Function - Setting Graphical Parameters
The R par() function is used to set and query graphical parameters, controlling the global appearance of plots.
With par() you can adjust many attributes such as plot layout, margins, and fonts.
The syntax of the par() function is as follows:
par(..., no.readonly = FALSE)
Description of commonly used parameters:
| Parameters | Description | Example |
|---|---|---|
| mfrow | Number of rows and columns for multiple plots per page | par(mfrow = c(2, 2)) |
| mar | Plot margins | par(mar = c(4, 4, 2, 1)) |
| col | Default color | par(col = "blue") |
| pch | Point symbol type | par(pch = 16) |
| cex | Scaling factor | par(cex = 1.2) |
| lwd | Line width | par(lwd = 2) |
Example
Save the original parameters
old_par
<-
par
(
no.
readonly
=
TRUE
)
Set up a 2x2 plot layout
par
(
mfrow
=
c
(
2
,
2
)
)
Draw four different plots
x
<-
1
:
10
y
<-
x
^
2
plot
(
x, y, main
=
"Scatter plot"
,
col
=
"steelblue"
, pch
=
16
)
hist
(
rnorm
(
100
)
, main
=
"Histogram"
,
col
=
"lightgreen"
)
barplot
(
c
(
10
,
20
,
15
,
30
)
, main
=
"Bar chart"
,
col
=
"coral"
)
boxplot
(
rnorm
(
50
)
,
rnorm
(
50
,
2
)
,
main
=
"Box plot"
,
col
=
"gold"
)
Restore the original parameters
par
(
old_par
)
Running the above code displays four different 2x2 plots on one canvas.
R Language Examples