R par() Function - Setting Graphics Parameters
The R par() function is used to set and query graphics parameters, controlling the global appearance of plots.
Through par(), you can adjust multiple attributes such as plot layout, margins, fonts, and more.
The syntax format of the par() function is as follows:
par(..., no.readonly = FALSE)
Common parameter descriptions:
| Parameter | 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 ratio | par(cex = 1.2) |
| lwd | Line width | par(lwd = 2) |
Example
# Save original parameters
old_par <- par(no.readonly = TRUE)
# Set 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 original parameters
par(old_par)
old_par <- par(no.readonly = TRUE)
# Set 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 original parameters
par(old_par)
Running the above code will display four different plots in a 2x2 grid on one canvas.
Other Extensions
R Language Examples