简体   繁体   English

设置设备的默认图形参数

[英]Set default graphical parameters for device

I often prefer to use a light text on dark background colortheme in IDEs. 我经常喜欢在IDE中使用深色背景colortheme上的浅色文本。 When I plot something in R the default colorscheme for plots is black text/borders/points on white background. 当我在R中绘制某些内容时,绘图的默认颜色方案是白色背景上的黑色文本/边框/点。 I was trying to change this by default, preferably for specific devices called from R by default ( X11cairo , RStudioGD ), while keeping the normal defaults for 'output' devices such as pdf and png . 我试图默认更改此项,最好是默认情况下从R调用的特定设备( X11cairoRStudioGD ),同时保持“输出”设备(如pdfpng )的正常默认值。

My question is twofold: (1) How can I set default graphical parameters? 我的问题有两个:(1)如何设置默认图形参数? and (2) Can I do this only for particular devices? (2)我可以仅针对特定设备执行此操作吗?

For example, I can easilly set the colorscheme in the current device with par : 例如,我可以轻松地使用par设置当前设备中的colorscheme:

par(
  bg = "black",
  col = "white",
  col.axis = "white",
  col.lab = "white",
  col.main = "white",
  col.sub = "white")

plot(1)

Creates the white on black plot as expected, and as expected resetting the device returns to defaults: 按预期在黑色图上创建白色,并按预期重置设备返回默认值:

dev.off()
plot(1)

I tried putting the following in my .Rprofile : 我尝试将以下内容放入我的.Rprofile

graphics:::par(
  bg = "black",
  col = "white",
  col.axis = "white",
  col.lab = "white",
  col.main = "white",
  col.sub = "white")
graphics:::plot(1,type="n",xlab="",ylab="",axes=FALSE)
graphics:::text(1,1,"Plotting area")

Which works somewhat, except that it opens a plot window on startup which can be abit annoying and in RStudio it doesn't open the RStudio device but an x11 window. 哪个有点工作,除了它在启动时打开一个情节窗口,这可能是令人讨厌的,在RStudio中它不会打开RStudio设备而是打开x11窗口。 Also if I close that window the parameters reset again. 此外,如果我关闭该窗口,参数将再次重置。 I would prefer to be able to have this "colorscheme" used by default every time I open a plotting window with in for example RStudio's default device. 我希望每次打开一个绘图窗口时默认使用这个“colorscheme”,例如RStudio的默认设备。

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. 然后,您可以使用options(device=mygrdevice)将您的函数设置为默认设备,其中mygrdevice是自定义函数。 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. 但是如果你打开一个不同的设备,如pdf或png,那么常规默认值就会到位。

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. 您还可以使用setHook设置在绘图时运行的钩子函数,但检查哪个设备是最新的可能比它的价值更多。 If a hook is available for when a plotting device starts, that might be a better option. 如果绘图设备启动时可以使用挂钩,那么这可能是更好的选择。

I did come up with an answer myself for the RStudio device at least, but it is sort of a messy hack. 至少我为RStudio设备做了一个答案,但这有点乱。 I can just overwrite the device functions in .Rprofile to change the par settings just after opening it: 我可以在.Rprofile覆盖设备功能,在.Rprofile立即更改par设置:

RStudioGD <- function()
{
  .Call("rs_createGD")
  graphics:::par(
       bg = "black",
       col = "white",
       col.axis = "white",
       col.lab = "white",
       col.main = "white",
       col.sub = "white")
}

It seems this isn't really the most appropriate way to do this though? 看来这不是最合适的方式吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM