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...:-) )
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)
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.
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.
But, doing a setDefaults
call with par
doesn't work either.
Any solution is welcome...
In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.
By default, R will use the vector names of your plot as X and Y axes labels. However, you can change them with the xlab and ylab arguments.
The different color systems available in R have been described in detail here. To change scatter plot color according to the group, you have to specify the name of the data column containing the groups using the argument groupName . Use the argument groupColors , to specify colors by hexadecimal code or by name .
Figure 5.1: Default colors in R The reason is simple. In R, the color black is denoted by col = 1 in most plotting functions, red is denoted by col = 2, and green is denoted by col = 3. So if you’re plotting multiple groups of things, it’s natural to plot them using colors 1, 2, and 3.
The reason is simple. In R, the color black is denoted by col = 1 in most plotting functions, red is denoted by col = 2, and green is denoted by col = 3. So if you’re plotting multiple groups of things, it’s natural to plot them using colors 1, 2, and 3.
Finally, the function colors () lists the names of colors you can use in any plotting function. Typically, you would specify the color in a (base) plotting function via the col argument. For both colorRamp () and colorRampPalette (), imagine you’re a painter and you have your palette in your hand.
In R, the color black is denoted by col = 1 in most plotting functions, red is denoted by col = 2, and green is denoted by col = 3. So if you’re plotting multiple groups of things, it’s natural to plot them using colors 1, 2, and 3. Here’s another set of common color schemes used in R, this time via the image () function.
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
)
In your case, just add this line to your .Rprofile:
setHook("plot.new", function() par(col = "red"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With