[R] Unexpected behavior in par()

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sat Apr 2 19:45:07 CEST 2022


Hello,

I have a function using base graphics that changes some graphics 
parameters, then plots what it has to plot, then on exit puts the 
graphics parameters back as they were.
The problem is that if outside the function the graphics parameters are 
also changed, for instance mfrow, those chages are not respected by the 
function. This seems to come from the way the graphics pars are saved.

After par(mfrow = c(2, 1))

- if I call par(no.readonly = TRUE) then the second call to the function 
plots in the 1st row, it overplots what was plotted before.
- if I save the pars when they are changed, all is well.

Here is a reproducible example.


f <- function(x, ...) {
   old_par <- par(no.readonly = TRUE)    # this is the problem
   par(mar = c(4.1, 3.1, 3.1, 1.1))      # or maybe here
   on.exit(par(old_par))
   barplot(x, ...)
}

g <- function(x, ...) {
   old_par <- par(mar = c(4.1, 3.1, 3.1, 1.1))   # this is the solution
   on.exit(par(old_par))
   barplot(x, ...)
}

set.seed(2022)
b1 <- table(sample(4, 100, TRUE))
b2 <- table(sample(10, 100, TRUE))

# 1st function, unexpected behavior
old_par <- par(mfrow = c(2, 1))
f(b1, main = "1st plot")
f(b2, main = "2nd plot")
par(old_par)

# 2nd function, all is well
old_par <- par(mfrow = c(2, 1))
g(b1, main = "1st plot")
g(b2, main = "2nd plot")
par(old_par)


If I print(old_par) in any of the functions the result is the right 
mfrow setting, so I would expect the 2nd call to f() to plot in the 2nd 
row. But it doesn't.
Function f() calls par() twice but it only changes a parameter unrelated 
to the parameter set outside the function.

I'm obviously making a mistake but I don't know what. Is this expected 
(or a par() bug)?


Thaks in advance,

Rui Barradas



More information about the R-help mailing list