R par() Function - Setting Graphics Parameters

R 语言实例R Language Examples

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:

ParameterDescriptionExample
mfrowNumber of rows and columns for multiple plots per pagepar(mfrow = c(2, 2))
marPlot marginspar(mar = c(4, 4, 2, 1))
colDefault colorpar(col = "blue")
pchPoint symbol typepar(pch = 16)
cexScaling ratiopar(cex = 1.2)
lwdLine widthpar(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)

Running the above code will display four different plots in a 2x2 grid on one canvas.

R 语言实例R Language Examples

Other Extensions