如何在R中为所有plot.default,plot或线调用默认设置颜色 [英] How to set a color by default in R for all plot.default, plot or lines calls

查看:880
本文介绍了如何在R中为所有plot.default,plot或线调用默认设置颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了简化我的每日R互动,我想为所有的地块设置默认颜色。例如,假设我想使用红线绘制所有绘图(例如在gnuplot ... :-))

to simplify my daily R interactions, I'd like to set up default colors for all my plots. For example, let's say I want to have all plots made with red lines (like in gnuplot...:-) )

到目前为止,这里是我的一个片段.Rprofile

So far, here is a snippet of my .Rprofile

setHook(packageEvent("grDevices", "onLoad"), 
    function(...) 
        grDevices::X11.options(width = 14, height = 8, type = "Xlib", xpos = 600,     ypos = 30, canvas = "grey87"))

suppressPackageStartupMessages( require(Defaults) )
suppressPackageStartupMessages( require(utils) )
suppressPackageStartupMessages( require(graphics) )

setDefaults("plot.default",frame.plot=FALSE, type='l', col=2)

我在这里做的是:
加载 grDevices 包(通过加载 graphics 包),我调用 X11。选项与我的首选参数:一个更宽的框,浅灰色的背景,xlib调用(因为我做遥远的电话,和开罗在我目前的环境是太慢了(另一个问题要解决))
然后我默认加载3个包, Defaults utils graphics 。第二个是需要避免 find 函数错误消息。

What I do here is the following: when the grDevices package is loaded (by loading the graphics package), I call the X11.options with my prefered parameters: a wider box, light gray background, xlib calls (because I'm doing distant calls, and cairo in my current environment is just too slow (another problem to solve)) Then I silently load 3 packages, Defaults, utils and graphics. The second one is needed to avoid a find function error message.

最后,魔法函数 setDefaults 设置散布图函数 plot.default 的3个参数。第三个参数 col 不是 plot.default 的参数,而是 par )函数。

Finally, the magic function setDefaults set-up 3 parameters to the scatter plot function plot.default. The 3rd parameter col is not a parameter of plot.default but one from the par() function.

但是,使用 setDefaults >

任何解决方案都是欢迎的...

Any solution is welcome...

推荐答案

您可以使用plot.new hook设置默认 par 值。 (钩子的工作原理记录在?plot.new ?setHook

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)

在你的case,只需添加这行到你的.Rprofile:

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

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

这篇关于如何在R中为所有plot.default,plot或线调用默认设置颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆