grantmcdermott · GitHub

Fixes #6 by adding a par_restore argument.

Defaults to par_restore = FALSE, in which case any changes to par during a plot2() call (e.g., from a legend repositioning) will persist afterwards. I initially resisted persisting changes as a default, but in hindsight I think this is more consistent with the normal base plot workflow. (And, in the case of #6, means that we can add elements after a plot is drawn in the regular manner.) Ofc users can opt out of this behaviour by switching to par_restore=TRUE.

Examples:

library(plot2)
op = par(no.readonly = TRUE)
plot2(Sepal.Width ~ Sepal.Length | Species, iris, grid = grid())
points(6,3, pch = 17, col = "red", cex = 1.5)

plot2(
  mpg ~ wt | cyl, mtcars,
  pch = 19,
  grid = grid(),
  legend.position = "right!",
  legend.args = list(title = "How many cylnders do you have?")
  )
lines(lowess(mtcars[["wt"]], mtcars[["mpg"]]), col = "red")

# Caution: Note that adjust margin persists b/c we didn't specify 
plot(1:10)

par(op)
# Try again with par_restore = TRUE
plot2(
  mpg ~ wt | cyl, mtcars,
  pch = 19,
  grid = grid(),
  legend.position = "right!",
  legend.args = list(title = "How many cylnders do you have?"),
  par_restore = TRUE
)

# Adding elements to the plot won't work properly now...
#lines(lowess(mtcars[["wt"]], mtcars[["mpg"]]), col = "red")
# But our normal defaults have been preserved
plot(1:10)

Created on 2023-04-09 with reprex v2.0.2

Read the original on github.com ↗