Set default graphics settings for a device

I often prefer to use light text on a dark colortheme background in the IDE. When I draw something in R, the default colors for the graphs are black text / borders / dots on a white background. I tried to change this by default, preferably for certain devices called from R by default ( X11cairo , RStudioGD ), while preserving the standard default values ​​for the "output" devices, such as pdf and png .

My question is twofold: (1) How to set the default graphics settings? and (2) Can I do this only for certain devices?

For example, I can easily set the color scheme in the current device using par :

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

Creates white on a black graph, as expected, and as expected, rebooting the device will revert to the default values:

 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 the plot window at startup, which can be annoying, and in RStudio it does not open the RStudio device, but the window is x11 . Also, if I close this window, the parameters reset again. I would rather have this default color scheme every time I open the graphing window, for example, using the default RStudio device.

+4
source share
2 answers

Graphics settings are saved throughout the life of the device, so you see them reset when you close the graphics device and start a new schedule.

Probably the best approach to what you want to do is to write a wrapper function for the devices to which you want to change the default values. This function will launch the device of interest and sets the default parameters for you. You can then set your function as the default device using options(device=mygrdevice) , where mygrdevice is a custom function. Then, if the device is not open, and you issue a command to build a graph, your function will work, open the device and set the default values. But if you open another device, for example, pdf or png, then the default values ​​will be set.

You can also use setHook to set the hook function that will be executed when plotting, but checking which device is current is likely to be more efficient than it costs. If a hook is available when the printing device starts, this might be the best option.

+2
source

I even came up with an answer to the RStudio device, but it's kind of a dirty hack. I can simply overwrite the device functions in .Rprofile to change the par settings right after it opens:

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

This doesn't seem to be the most suitable way to do this, though?

0
source

Source: https://habr.com/ru/post/1447217/


All Articles