I'm trying to use a named character vector to hold a custom color palette, so I can say, e.g. palette['red'] instead of repeating "#dc322f" all over the place.
However, I don't seem to be able to use an element of that vector as an argument to par()
(though it can be used elsewhere).
Here's an example. It'll create a graph with green dots, but the par() call fails and the background is white. Note that I can set parameters using the palette vector from within the plot()
call:
> palette <- c('#002b36','#dc322f','#859900')
> names(palette) <- c('black','red','green')
> par(bg=palette['red'])
Warning message:
In par(bg = palette["red"]) : "bg.red" is not a graphical parameter
> plot(1:10,1:10,col=palette['green'])
> # (White graph with green dots appears)
When I use a named numeric vector, however, it works:
> palette <- 1:3
> names(palette) <- c('black','red','green')
> par(bg=palette['red'])
> # (no error here -- it worked.)
> plot(1:10,1:10,col=palette['green'])
> # (Red graph with green dots appears)
I am fairly new to R, and it seems that I might be missing something fundamental. Any idea what's happening here?