为设备设置默认图形参数 [英] Set default graphical parameters for device

查看:131
本文介绍了为设备设置默认图形参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常更喜欢在IDE中的深色背景colortheme上使用浅色文本。当我在R中绘制某些东西时,绘图的默认颜色方案是白色背景上的黑色文本/边框/点。我试图改变这个默认情况下,最好是默认情况下从R调用的特定设备( X11cairo RStudioGD ),同时保留输出设备的正常默认值,例如 pdf png



我的问题有两方面:(1)如何设置默认图形参数? (2)我可以只为特定设备执行此操作吗?

例如,我可以轻松设置当前设备中的colorscheme,其中 par

 面值(
bg =black,
col =白色,
col.axis =white,
col.lab =white,
col.main =white,
col.sub =white )

plot(1)

按预期创建黑色图上的白色,如预期重置设备返回到默认值:

  dev.off()
plot(1)

我试着在我的 .Rprofile 中放入以下内容:

 图形::: 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)

它会在启动时打开一个绘图窗口,这可能会引起恼人的问题,而在RStudio中,它不会打开RStudio设备,而是一个 x11 窗口。另外,如果我关闭该窗口,参数重新设置。我希望能够在每次打开绘图窗口(例如RStudio的默认设备)时默认使用此colorscheme。

解决方案

图形参数是设备使用寿命的最后一次,这就是为什么当你关闭图形设备并开始一个新的绘图时你会看到它们重置。



可能是要做什么的最佳方法是为要更改默认值的设备编写包装函数。该功能将启动感兴趣的设备并为您设置默认参数。然后,您可以使用选项(device = mygrdevice)将您的函数设置为默认设备,其中 mygrdevice 是自定义函数。然后,如果没有设备打开并且您发出绘图命令,则您的功能将运行,打开设备并设置默认值。但是,如果你打开一个不同的设备,如PDF或PNG,那么常规的默认设置就位。



您也可以使用 setHook 来设置一个钩子函数在绘图时运行,但检查哪个设备是最新的可能会比它值得的更多的工作。如果绘图设备启动时可以使用挂钩,那可能是更好的选择。


I often prefer to use a light text on dark background colortheme in IDEs. When I plot something in R the default colorscheme for plots is black text/borders/points on white background. 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.

My question is twofold: (1) How can I set default graphical parameters? and (2) Can I do this only for particular devices?

For example, I can easilly set the colorscheme in the current device with par:

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:

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. 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.

解决方案

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. 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.

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. If a hook is available for when a plotting device starts, that might be a better option.

这篇关于为设备设置默认图形参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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