1

Suppose I want to plot a vector in R with different colors. I write:

    x <- 1:10
    plot(x,col=x)

or,

    x <- 1:5
    plot(x,col=x)

From the plot see that in both the cases "red" is the second color which R chooses, black being the first. I want to make "red" the first.

Please note that defining 5 or 10 colors is not the solution since 5 and 10 are arbitrary numbers.

Any help would be helpful.

My original problem was defining user specific colors in ggplot2 using:

    aes(color=factor(Variable))

In this I wanted to make red the first color.

1 Answers1

1

There are two separate problems here.

When you plot using base R, colours are chosen from the default palette:

palette()
[1] "black"   "red"     "green3"  "blue"    "cyan"    "magenta" "yellow"  "gray"

Now you see why black is first and red is second. In your example, to get red first you might try:

plot(1:5, col = 2:6)

but really, neither that plot nor the colour scheme make a lot of sense.

Now to ggplot2: it uses its own colour palette as described in these answers. If you want to colour by a factor variable and override that palette, you might use scale_color_brewer() with a defined palette e.g.:

+ scale_color_brewer(palette = "Dark2")

or to use your own colours, scale_color_manual(), e.g. for the first colour to be red:

+ scale_color_manual(values = c("red", "blue", "green", ...)
neilfws
  • 32,751
  • 5
  • 50
  • 63