How to select colors from a predefined color set in ggplot2

Alex picture Alex · Jan 31, 2014 · Viewed 7.9k times · Source

Suppose that I have a dataset like the following:

set.seed(1)
dataset <- data.frame(x = sort(rnorm(100)), 
                      y = sort(rlnorm(100))+1:4, 
                      group=rep(letters[1:4], 25))

I would like to create a plot using ggplot2. Instead of choosing colors manually, I use the pre-defined color set Paired:

ggplot(dataset, aes(x = x, colour = group)) + 
geom_line(aes(y=y)) + 
scale_colour_brewer(palette="Paired")

I get the plot as shown below: the data points for groups a and b are in two shades of blue, whereas the data points for groups c and d are in two shades of green.

enter image description here

Suppose now, I would like to only plot the data corresponding to groups c and d, and I would like to use the two shades of green. If I simply do the following:

ggplot(dataset[dataset$group %in% c("c", "d"),], aes(x = x, colour = group)) + 
geom_line(aes(y=y)) + 
scale_colour_brewer(palette="Paired")

the function will automatically select the two shades of blue (see below), since they come first in the Paired palette set.

enter image description here

So my question is: how can I select colors in a pre-defined color set when I use ggplot2

Answer

tonytonov picture tonytonov · Jan 31, 2014
require(RColorBrewer)
ggplot(dataset[dataset$group %in% c("c", "d"),], aes(x = x, colour = group)) + 
  geom_line(aes(y=y)) + 
  scale_colour_manual(values = brewer.pal(4, "Paired")[3:4])

enter image description here

For a more scalable approach, define the palette globally, outside of the plotting code. You will then be able to dynamically map the subsetted data to the corresponding colour.