Discussion Forum
1

# How to reset par in r?

In this article, I’ll show how to set the par options back to default in the R programming language.

Let’s dive right into the exemplifying R syntax.

Let’s first define some par options for a plot.

Now, we can draw a plot with these par options as shown below:

Figure 1 shows the output of the previous R syntax: A simple scatterplot in R. You can also see that the borders of the plot show a lot of white space (as specified in par).

The following R programming code illustrates how to reset the par specifications back to the default settings. In the following R code, we are using the dev.off function to reset par:

Let’s draw our plot again:

The output of the previous R programming syntax is shown in Figure 2 – As you can see, the par options were set back to default values (i.e. additional white space was removed).

In case you need further information on the R code of this article, you might want to watch the following video of my YouTube channel. In the video, I show the R programming code of this tutorial.

Furthermore, I can recommend to have a look at the other tutorials on this homepage. I have released several posts already:

[4]
Edit
Query
Report
INSPECTOR FURNITURE DECALS

Let’s return to the basic plot we made previously in this Chapter. This was a simple scatterplot to examine the relationship between the shootarea and weight variables in the flowers data frame.

Whilst this plot is adequate for data exploration it’s not going to cut the mustard if we want to share it with others. At the very least it could do with a better set of axes labels, more informative axes scales and some nicer plotting symbols.

Let’s start with the axis labels. To add labels to the x and y axes we use the corresponding ylab = and xlab = arguments in the plot() function. Both of these arguments need character strings as values.

OK, that looks a little better but the units (cm2) looks a little ugly as we should format the 2 as a superscript. To convert to a superscript we need to use a combination of the expression() and paste() functions. The expression() function allows us to format the superscript (and other mathematical expressions - see ?plotmath for more details) with the ^ symbol and the paste() function pastes together the elements "shoot area (cm"^"2" and ) to create our axis label.

But now we have a new problem, the very top of the y axis label gets cut off. To remedy this we need to adjust the plot margins using the par() function and the mar = argument before we plot the graph. The par() function is the main function for setting graphical parameters in base R and the mar = argument sets the size of the margins that surround the plot. You can adjust the size of the margins using the notation par(mar = c(bottom, left, top, right) where the arguments bottom, left, top and right are the size of the corresponding margins. By default R sets these margins as mar = c(5.1, 4.1, 4.1, 2.1) with these numbers specifying the number of lines in each margin. Let’s increase the size of the left margin a little bit and decrease the size of the right margin by a smidge.

That looks better. Now let’s increase the range of our axes scales so we have a bit of space above and to the right of the data points. To do this we need to supply a minimum and maximum value using the c() function to the xlim = and ylim = arguments. We’ll set the x axis scale to run from 0 to 30 and the range of the y axis scale from 0 to 200.

And while we’re at it let’s remove the annoying box all the way around the plot to just leave the y and x axes using the bty = "l" argument.

OK, that’s looking a lot better already after only a few adjustments. One of the things that we still don’t like is that by default the x and y axes do not intersect at the origin (0, 0) and both axes extend beyond the maximum value of the scale by a little bit. We can change this by setting the xaxs = "i" and yaxs = "i" arguments when we use the par() function. While we’re about it let’s also rotate the y axis tick mark labels so they read horizontally using by setting the las = 1 argument in the plot() function and make them a tad smaller with the cex.axis = argument. The cex.axis = argument requires a number giving the amount by which the text will be magnified (or shrunk) relative to the default value of 1. We’ll choose 0.8 making our text 20% smaller. We can also make the tick marks just a little shorter by setting tcl = -0.2. This value needs to be negative as we want the tick marks to be outside the plotting region (see what happens if you set it to tcl = 0.2).

We can also change the type of plotting symbol, the colour of the symbol and the size of the symbol using the pch =, col = and cex = arguments respectively. The pch = argument takes an integer value between 0 and 25 to define the type of plotting symbol. Symbols 0 to 14 are open symbols, 15 to 20 are filled symbols and 21 to 25 are symbols where you can specify a different fill colour and outside line colour. Here’s a summary table displaying the value and corresponding symbol type.

The col = argument changes the colour of the plotting symbols. This argument can either take an integer value to specify the colour or a character string giving the colour name. For example, col = "red" changes the plotting symbol to red. To see a list of all 657 preset colours available in base R use the colours() function (you can also use colors()) or perhaps even easier see this link. More colour options are available with other packages (see the excellent RColorBrewer package) or you can even ‘mix’ your own colours using the colorRamp() function (see ?colorRamp for more details).

The cex = argument allow you to change the size of the plotting symbol. This argument works in the same way as the other cex arguments we’ ve already seen (i.e. cex.axis) and requires a numeric value to indicate the proportional increase or decrease in size relative to the default value of 1.

Let’s change the plotting symbol to a filled circle (16), the colour of the symbol to “dodgerblue1” and decrease the size of the symbol by 10%.

The last thing we’ll do is add a text label to the plot so we can identify it. Perhaps this plot will be one of a series of plots we want to include in the same figure (see the section on plotting multiple graphs to see how to do this) so it would be nice to be able to refer to it in our figure title. To do this we’ll use the text() function to add a capital ‘A’ to the top right of the plot. The text() function needs an x = and a y = coordinate to position the text, a label = for the text and we can use the cex = argument again to change the size of the text.

[2]
Edit
Query
Report
DEAN OF STUDENTS