How to set a color by default in R for all plot.default, plot or lines calls

折月煮酒 提交于 2019-12-04 06:52:48

You can use the "plot.new" hook to set default par values each time a new graphics frame is opened. (The hook's workings are documented in ?plot.new and ?setHook)

In your case, just add this line to your .Rprofile:

setHook("plot.new", function() par(col = "red"))

The parameters such as color are set on a per device basis, so when you close one device and create a new one all the parameters are set back to their default values. To do this I would create your own device function that opens the device then sets the parameters, something like:

mydev.new <- function(...) {
  dev.new(...)
  par(col='red')
}

You could obviously replace dev.new with x11 or something else, but this is probably the most portable. Now you can open a new device using mydev.new and the default color will be set to red.

Further if you run the command

options(device=mydev.new)

Then when you don't have a graphics device open and you run a plotting command, your function will be the one run to open a new plotting device and so the default will be red in that case as well. You could expand the mydev.new function (or whatever you want to call it) to set other options, take arguments, etc. for different cases you may want to work with.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!