在不使用dev.off()的情况下将图形参数重置为默认值

时间:2012-02-15 11:32:59

标签: r rstudio

如边缘,方向等...

dev.off()对我不起作用。我经常使用RStudio,内置图形设备。然后我有绘图功能,我想要在默认的RStudio图形设备中绘制,或者如果我在新窗口中调用X11()

此行为不适用于dev.off()。如果我的绘图功能始终调用dev.off(),它可能会无意中关闭X11()窗口,而是在RStudio设备中绘图。如果我总是调用dev.off()后跟X11(),它将始终在新窗口中绘图,即使我想在RStudio设备中绘图。

通常情况下可以使用getOption("device")来解决,但始终会返回RStudioGD

6 个答案:

答案 0 :(得分:45)

参见?par。这个想法是你在找到它们时保存它们,然后恢复:

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

现在我们在更改mar

之前恢复原状
par(old.par)

答案 1 :(得分:25)

在RStudio中,你可以导航到'Plots'并选择'Remove plots'

答案 2 :(得分:10)

如果您在启动时已经错过了保存默认参数,并且您不想重新启动会话,那么您可以打开终端并通过(通常)键入R来运行R.

然后输入:

  

参数()

它将打印所有默认值。

您可以将它们保存在文本文件中,然后导入到当前工作的工作区中。

答案 3 :(得分:5)

包含所有默认值的简单函数可以完成这项工作:

  reset_par <- function(){
    op <- structure(list(xlog = FALSE, ylog = FALSE, adj = 0.5, ann = TRUE,
                         ask = FALSE, bg = "transparent", bty = "o", cex = 1, cex.axis = 1,
                         cex.lab = 1, cex.main = 1.2, cex.sub = 1, col = "black",
                         col.axis = "black", col.lab = "black", col.main = "black",
                         col.sub = "black", crt = 0, err = 0L, family = "", fg = "black",
                         fig = c(0, 1, 0, 1), fin = c(6.99999895833333, 6.99999895833333
                         ), font = 1L, font.axis = 1L, font.lab = 1L, font.main = 2L,
                         font.sub = 1L, lab = c(5L, 5L, 7L), las = 0L, lend = "round",
                         lheight = 1, ljoin = "round", lmitre = 10, lty = "solid",
                         lwd = 1, mai = c(1.02, 0.82, 0.82, 0.42), mar = c(5.1, 4.1,
                                                                           4.1, 2.1), mex = 1, mfcol = c(1L, 1L), mfg = c(1L, 1L, 1L,
                                                                                                                          1L), mfrow = c(1L, 1L), mgp = c(3, 1, 0), mkh = 0.001, new = FALSE,
                         oma = c(0, 0, 0, 0), omd = c(0, 1, 0, 1), omi = c(0, 0, 0,
                                                                           0), pch = 1L, pin = c(5.75999895833333, 5.15999895833333),
                         plt = c(0.117142874574832, 0.939999991071427, 0.145714307397962,
                                 0.882857125425167), ps = 12L, pty = "m", smo = 1, srt = 0,
                         tck = NA_real_, tcl = -0.5, usr = c(0.568, 1.432, 0.568,
                                                             1.432), xaxp = c(0.6, 1.4, 4), xaxs = "r", xaxt = "s", xpd = FALSE,
                         yaxp = c(0.6, 1.4, 4), yaxs = "r", yaxt = "s", ylbias = 0.2), .Names = c("xlog",
                                                                                                  "ylog", "adj", "ann", "ask", "bg", "bty", "cex", "cex.axis",
                                                                                                  "cex.lab", "cex.main", "cex.sub", "col", "col.axis", "col.lab",
                                                                                                  "col.main", "col.sub", "crt", "err", "family", "fg", "fig", "fin",
                                                                                                  "font", "font.axis", "font.lab", "font.main", "font.sub", "lab",
                                                                                                  "las", "lend", "lheight", "ljoin", "lmitre", "lty", "lwd", "mai",
                                                                                                  "mar", "mex", "mfcol", "mfg", "mfrow", "mgp", "mkh", "new", "oma",
                                                                                                  "omd", "omi", "pch", "pin", "plt", "ps", "pty", "smo", "srt",
                                                                                                  "tck", "tcl", "usr", "xaxp", "xaxs", "xaxt", "xpd", "yaxp", "yaxs",
                                                                                                  "yaxt", "ylbias"))
    par(op)
  }

使用:

来调用它

reset_par()

答案 4 :(得分:0)

规范答案仅在注释中(由Cookie发出),并且很容易被忽略:

获取当前/默认参数

old.par <- par(no.readonly = TRUE)

在您的代码中设置它们,例如

par(mai=c(0,0,0,0))

然后您可以使用重置

par(old.par)

或者,在函数中

on.exit(par(old.par))

答案 5 :(得分:0)

对于边距,par提供默认值c(5,4,4,2)+0.1。以下应将页边距重置为默认值。

par(mar=c(5,4,4,2)+0.1)