Warning: session_start(): open(/tmp/sess_5c38318de3f50757d5b2b564ecb0e7fa, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Use The par() Function In R - LearnShareIT

How To Use The par() Function In R

Using the par() function in R, you can change many graphical components in a plot. This guide will explain its use in detail.

par() Function In R

You can use this function to access and change graphical parameters, which can be put in the ‘tag = value’ form as passed as tagged values.

par(…, no.readonly)

There is only one optional argument (no.readonly). When this logical argument is set to TRUE without any additional argument, the par() function returns the list of all parameters that can be restored to their previous values.

Remember that you must use the correct name of the graphical parameter you want to query or reset. These parameters allow you to modify different graphical devices.

Display Multiple Plots

You can modify the mfrow and mfcol parameters with par() to split the screen into multiple parts where you can display plots. This is one of the most common applications of this function.

In particular, mfrow and mfcol can create a plotting matrix in a single space. These parameters need to be integer vectors of length two representing the number of rows and columns in the plotting space.

For instance, this command will create a 1×2 plotting matrix:

par(mfrow = c(1, 2))

You can then create plots with other R functions (such as plot() and hist()). The plots created by those functions will be placed next to each other in the plotting matrix par() has divided.

Each command below will create a histogram. But they will stand next to each other in the plotting space, not replaced:

hist(pnorm(100))

hist(rnorm(100))

You can use the mfcol parameter in the same way. The only difference is that it draws plots by columns, not rows.

If you don’t provide any argument, the par() function returns a list containing existing values of all graphical parameters. You can use the dollar operator to query any of them:

par()$mfrow

Output

[1] 1 1

When setting a parameter, you can save the previous value to a list so it can be restored later. The following example sets the layout of the plotting space to 2×2 and then changes it back.

a <- par(mfrow = c(2, 2))
par()$mfrow

Output

[1] 2 2
par(a)
par()$mfrow

Output

[1] 1 1

Change Background Color

Another common use of the par() function is to change the background color of R plots (which is white by default). You can use the predefined color names (“red”, “black”, “green”, …) and assign one of them to the bg parameter.

Notice how the background color has changed from white to gray:

plot(1:10)

par(bg = “gray”)

plot(1:10)

Other Customization

You can also customize axes and other aspects of R plots with the par() function.

For instance, assign the mar parameter to a vector of length four to specify the bottom, left, top, and right margins of the plot.

par(mar = c(4, 4, 4, 4) + 0.2)

plot(1:10)

You can also add another axis and merge another plot with the existing one. The new parameter, in particular, tells your plotting command (such as plot()) not to erase the current plot before drawing a new one.

In this example, we merge two plots (one of the linear function and one of the standard normal distribution) into one with par():

plot(-5:5, pch = 19, ylab = "First variable")
par(new = TRUE)
plot(rnorm(100),
     col = 4, pch = 19,
     bty = "n",
     axes = FALSE,
     xlab = "", ylab = "")
axis(4)
mtext("Second variable", side = 4, line = 3, col = 4)

Output

Summary

You can use the par() function in R to modify and customize your plots. There is a long list of graphical parameters that can be used to prepare the plotting space before the plotting commands are put into use.

Leave a Reply

Your email address will not be published. Required fields are marked *