Reset the Graphical Parameters Back to Default Values Without Use of Dev.Off()

Reset the graphical parameters back to default values without use of dev.off()

See ?par. The idea is that you save them as they are when you found them, and then restore:

old.par <- par(mar = c(0, 0, 0, 0))
## do plotting stuff with new settings

Now restore as they were before we changed mar:

par(old.par)

Reset par to the default values at startup

Every time a new device is opened par() will reset, so another option is simply do dev.off() and continue.

How to reset 'par' (pty='s') value in R

Before modifying the graphical parameters with par it may be useful to store the previous parameters:

old_par = par()

Then you'll be able to come back to previous settings by typing par(old_par). For your current problem, default value for pty is "m".

In any case, if you don't want to close your current graphical device to get the old_par parameter, you can still open a new one x11() then the par function will concern the new window, and then close it dev.off()

Set default graphical parameters for device

Graphics parameters last for the life of the device, that is why you see them reset when you close the graphics device and start a new plot.

Probably the best approach for what you want to do is to write a wrapper function for the devices that you want to change the defaults on. This function would start the device of interest and set the default parameters for you. You can then set your function as the default device using options(device=mygrdevice) where mygrdevice is the custom function. Then if no device is open and you issue a plotting command your function will run, open the device and set the defaults. But if you open a different device such as pdf or png then the regular defaults will be in place.

You could also use setHook to set a hook function to run when plotting, but checking on which device is current would probably be more work than it is worth. If a hook is available for when a plotting device starts, that might be a better option.

Warnings when restoring graphical parameters

The ... in my comments were merely a placeholder for whatever you intend to put in there. (I tend to think lots of code in comments can be difficult to read, so I just shortened it.)

Literally:

opar <- par(oma = c(5, 4, 0, 0) + 0.1, mar = c(0, 0, 1, 1) + 0.1)
# other code that uses those settings
# when you are ready to reset to the original settings for oma and mar,
par(opar)

This is provided in a similar example in the doc, ?par.

One example of how doing it this way is different from saving all of the parameters with opar <- par(no.readonly=TRUE) is that this only reset/restores the parameters that you explicitly changed. It is feasible that others might have changed as a side-effect of other actions (outside of this intent).

How to reset par(mfrow) in R

You can reset the mfrow parameter

par(mfrow=c(1,1))

Set default options for multiple plots

You can do something like this:

Define your function:

format_cust <- function(.x) {
.x +
geom_point(aes(shape = type, color = type, fill = type)) +
scale_shape_manual(values = c(21,22,23,25,15,17,16)) +
scale_fill_manual(values = c('#e41a1c','#377eb8','#4daf4a','#984ea3', '#ff7f00','#a65628','#f781bf','black')) +
scale_color_manual(values = c('#e41a1c','#377eb8','#4daf4a','#984ea3', '#ff7f00','#a65628','#f781bf','black')) +
scale_size_manual(values = c(1,2,2,1,2,2,1)) +
geom_line(aes(color = type)) +
xlab("Local Time")
}

Then apply it:

library(dplyr)
mtcars %>%
mutate(type = factor(carb)) %>%
ggplot(aes(mpg, cyl)) %>%
format_cust()

Automatically calling dev.off after plot in R

Edit: Add possible approach for ggplot.

Note that the reason that plot, in particular, doesn't automatically call dev.off() is that it's very common -- after calling plot -- to add additional material (annotations, supplementary plotting features, titles, legends, etc.) with additional calls before calling dev.off() to finalize the output.

But, if you really want plot to finalize the plot and prevent any further content from being added, you can do it by redefining plot:

plot <- function(...) {
graphics::plot(...)
dev.off()
}

Note that for window-based graphics devices (e.g., x11), the plot will briefly flash on the screen before disappearing, since dev.off() closes the window, but it should work fine for files:

> png("plot.png")
> plot(1:10,runif(10))
null device <-- proof that dev.off() was called
1
>

For ggplot2, I guess your best bet is to override the print method (which is how a plot normally gets displayed on the screen). So, if you define:

print.ggplot <- function(...) {
ggplot2:::print.ggplot(...)
dev.off()
}

then:

> png("plot.png")
> ggplot(mapping=aes(x=1:10,y=1:10))+geom_line()
[[ print method is implicitly called here ]]
> dev.off() # to prove that dev.off() was already called
Error in dev.off() : cannel shut down device 1 (the null device)
>

appears to work the way you want.

I don't use RStudio, so I'm not sure what it's doing differently, but I'm going to guess that it intercepts the graphics commands in such a way that it simultaneously shows the "plot in progress" before the file is finalized, rather than actually displaying the file (which, for bitmapped graphics, can't be written out at all until the plot is completely finished and dev.off() is called).



Related Topics



Leave a reply



Submit