15.1 C
London
Friday, July 5, 2024
HomeRDescriptive Statistics in RHow to Use the par() Function in R

How to Use the par() Function in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the par() function in R to create multiple plots at once.

This function uses the following basic syntax:

#define plot area as four rows and two columns
par(mfrow = c(4, 2))    

#create plots
plot(1:5)
plot(1:20)
...

The following examples show how to use this function in practice.

Example 1: Display Multiple Plots with par()

The following code shows how to use the par() function to define a plotting area with 3 rows and 1 column:

#define plot area as three rows and one column
par(mfrow = c(3, 1))    

#create plots
plot(1:5, pch=19, col='red')
plot(1:10, pch=19, col='blue')
plot(1:20, pch=19, col='green')

par function in r

Example 2: Specify Margins of Plots with mar()

The following code shows how to use the mar() argument to specify the margins around each plot in the following order: bottom, left, top, right.

Note: The default is mar = c(5.1, 4.1, 4.1, 2.1)

#define plot area with tiny bottom margin and huge right margin
par(mfrow = c(3, 1), mar = c(0.5, 4, 4, 20))    

#create plots
plot(1:5, pch=19, col='red')
plot(1:10, pch=19, col='blue')
plot(1:20, pch=19, col='green')

par mar function in R

Notice how the plots look less wide because we made the margin on the right so large.

Example 3: Specify Text Size of Plots with cex()

The following code shows how to use the cex.lab() and cex.axis() arguments to specify the size of the axis labels and the tick labels, respectively.

Note: The default is cex.lab = 1 and cex.axis = 1

#define plot area with large axis labels
par(mfrow = c(3, 1), mar = c(5, 10, 4, 1), cex.axis = 3, cex.lab = 3)    

#create plots
plot(1:5, pch=19, col='red')
plot(1:10, pch=19, col='blue')
plot(1:20, pch=19, col='green')

Once you’re finished using the par() function, you can use the dev.off() function to reset the par options.

#reset par() options
dev.off()

It’s a good to use dev.off() each time you’re done using the par() function. 

Additional Resources

How to Plot Multiple Columns in R
How to Draw a Legend Outside of a Plot in R
How to Create a Log-Log Plot in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories