r - 恢复图形参数时的警告

标签 r plot warnings par

我正在编写我的第一个 R 程序包,目前正在开发一个使用某些特定图形参数绘制绘图的函数。我希望在绘图完成后恢复用户定义的图形参数,但总是收到相同的警告消息:

opar <- par()
par(oma = c(5, 4, 0, 0) + 0.1, mar = c(0, 0, 1, 1) + 0.1)
par(opar)

Warning messages:
1: In par(opar) : graphical parameter "cin" cannot be set
2: In par(opar) : graphical parameter "cra" cannot be set
3: In par(opar) : graphical parameter "csi" cannot be set
4: In par(opar) : graphical parameter "cxy" cannot be set
5: In par(opar) : graphical parameter "din" cannot be set
6: In par(opar) : graphical parameter "page" cannot be set

有更好的方法吗?我知道 suppressWarnings() 函数,但 1. 我不希望消息被隐藏 和 2. 如果该函数被调用两次,则会出现一条警告消息:

> There were 12 warnings (use warnings() to see them)

最佳答案

您可以在保存图形参数时通过提供 no.readonly = TRUE 来绕过这些警告,如下所示:

opar <- par(no.readonly = TRUE)
par(oma = c(5, 4, 0, 0) + 0.1, mar = c(0, 0, 1, 1) + 0.1)
par(opar)

此外,您可以使用 dev.off() 恢复默认的 par 参数值。

希望对您有所帮助。

关于r - 恢复图形参数时的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53699637/

相关文章:

c++ - 帮助解决这些警告。 [遗产]

r - 查找两个数据帧之间匹配的行以及它们在 R 中的索引

r - 将额外的图插入多面网格,ggplot2

r - R中的大型固定效应二项式回归

r - 如何在 ggplot2 中绘制函数族

python - 在 3D 图上绘制两个 2D 图

xcode - 如何在新的 xCode 中禁用 "Add explicit braces to avoid dangling else"?

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

r - ggplot 中的自定义形状(geom_point)

xml - R 从 data.frame 获取列名