简体   繁体   中英

Set default graphical parameters for device

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.

I did come up with an answer myself for the RStudio device at least, but it is sort of a messy hack. I can just overwrite the device functions in .Rprofile to change the par settings just after opening it:

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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