Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a color by default in R for all plot.default, plot or lines calls

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

like image 216
David Bellot Avatar asked Sep 27 '12 16:09

David Bellot


People also ask

How do you set color in R?

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.

What is the default plot in R?

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.

How do I change the color of a scatter plot in R?

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 .

What are the default colors in R plot?

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.

Why does R plot with Col 1 2 and Col 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.

How do you use colors in a plot?

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.

What are the different color schemes used in R?

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.


1 Answers

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"))
like image 81
Josh O'Brien Avatar answered Sep 21 '22 22:09

Josh O'Brien