There are many resources on the use of colours in R, several packages, and a number of schemes already implemented in ggplot2. In the previous part, we saw how ggplot2 selects a default colour palette according to the type of variable, discrete or continuous. There are further options, illustrated below:

Choosing colours for a graphic is often some kind of a compromise. One one hand, you want the computer, some algorithm, to choose a sensible colour scheme and pick automatically the required number of colours from this scale. On the other hand, there are always external human preferences that constrain the choices, and are not always easy to formalise.
Some choices, even prevalent in the literature such as the rainbow color scale (also known as Matlab’s flashy colorjet),

are just not good enough. They introduce artefacts, highlight regions of the data that should have a smooth transition with their surroundings, and do not degrade gracefully in black-and-white print, or when viewed by colour-impaired people.
If good colours for scientific graphics are not (entirely) in the eye of the beholder, what are the guides to make the best choice?
A recent blog post illustrates the search for a pleasing colour scheme in bar graphs. On the default HCL (Hue Chroma Luminance, pdf) choice of ggplot2 for discrete variables, the author remarks
The colour choice is not a bad one, but there’s something about the intensity of the colours that makes me want to find a new set of colours somewhat more soothing to my eyes.
and documents his heuristic search for satisfying colours,
I shuffled through many different colours on the Color Hex website, and nothing else seemed to work with me as I wasn’t selecting colours based on any theory
A good discussion is offered in the colorspace package and its accompanying vignettes and papers, e.g. Escaping RGBland: Selecting Colors for Statistical Graphics (pdf)
Despite this omnipresence of color, there is often only little guidance in statistical software packages on how to choose a palette appropriate for a particular visualization task
In this instance, I would argue that the hcl colour scale of ggplot2 is a good start for a well-balanced graphic that doesn’t draw the attention to a particular colour. If the colours are too flashy in bar plots (large areas), the saturation and luminosity can easily be muted by tuning the scale,

This basic idea of tuning the HCL colour scale to suit the application was discussed in more depth in Colour for Presentation Graphics (pdf).
Bar plots and maps can also benefit from trying a few different colour palettes from the excellent ColorBrewer website. An interface is provided in R and ggplot2 through the RColorBrewer package.
Easily accessed with scale_colour_brewer(), it is trivial to choose among 35 palettes (see RColorBrewer::display.brewer.all()).
Sequential palettes, suited to ordered data that progress from low to high. Lightness steps dominate the look of these schemes, with light colors for low data values to dark colors for high data values.

Qualitative palettes, do not imply magnitude differences between legend classes, and hues are used to create the primary visual differences between classes. Qualitative schemes are best suited to representing nominal or categorical data.

Diverging palettes, put equal emphasis on mid-range critical values and extremes at both ends of the data range. The critical class or break in the middle of the legend is emphasized with light colors and low and high extremes are emphasized with dark colors that have contrasting hues.

In the next post, we’ll look at some special cases where the user might want finer control over these scales, or define completely new colour palettes tailored for a specific graphic.
In this series of three posts, we’ll look at colours in R graphics produced with ggplot2: what are the available choices of colour schemes, and how to choose a colour palette most suitable for a particular graphic?
In kindergarten, choosing a colour was easy, palettes were limited to a few classics. As cool kids grow older and use R, the spectrum expands to present us with overwhelming choice of millions of colours, most of them with poorly defined labels such as "#A848F2" or "lavenderblush3". Inasmuch as scientific graphics resemble a paint-by-numbers game, R can help us design more elegant palettes with pertinent colour choices based on the data to display.
Base graphics rely mostly on the grDevices package for the selection of colours, with a few palettes to choose from:
(some palettes can have many more colours, this image is only an illustration of their structure)
The package also provides a number of basic operations to convert colours (adjustcolor, col2rgb, make.rgb, rgb2hsv, convertColor) and create interpolating palettes (rgb, hsv, hcl, gray, colorRamp, colorRampPalette, densCols, gray.colors).
Beyond that, a good resource is the colorspace package which provides further utilities to convert from one colorspace to another (HLS, HSV, LAB, LUV, RGB, sRGB, XYZ) and perform various operations on colours.
A special note can be made of a few palette functions, “diverge_hcl”, “diverge_hsv”, “heat_hcl”, “rainbow_hcl”, “sequential_hcl”, “terrain_hcl”, which provide an easy way to produce colour palettes following a particular path in the colour space (varying hue with constant luminosity and saturation, for example).
Other packages such as RColorBrewer, munsell and dichromat provide more colour palettes and utilities.
While the combination of these tools is quite flexible, the user interface becomes a little bit chaotic. More recently, the scales package has provided wrappers around these functions to provide some consistency in the naming schemes and organise the different categories of palettes in a structured way:
Utilities functions, such as col2hcl, fullseq, muted, rescale, rescale_mid, rescale_none, rescale_pal, seq_gradient_pal, show_col
Palettes with consistent interface, brewer_pal, dichromat_pal, gradient_n_pal , div_gradient_pal, hue_pal, grey_pal, identity_pal, manual_pal.
The ggplot2 package uses scales internally, and mirrors this structure. In this first part, we’ll review the basic commands to assign colours in ggplot2.
Let’s consider three plots for illustration:

p1 maps the colour of points to a continuous variable, p2 maps the fill of bars to a discrete variable, and p3 maps the fill of tiles to a continuous variable.
Fill and colour scales in ggplot2 can use the same palettes. Some shapes such as lines only accept the colour aesthetic, while others, such as polygons, accept both colour and fill aesthetics. In the latter case, the colour refers to the border of the shape, and the fill to the interior.
Another common source of confusion, general to ggplot2, is the distinction between set values and mapped values in a layer. Consider the following example,
d = data.frame(x = 1:10, y = rnorm(10), z = gl(5, 2))
a = ggplot(d, aes(x, y, group=z))
grid.arrange(a + geom_path( colour = "red" ),
a + geom_path( aes(colour = z )),
nrow=1)

The default continuous scale in ggplot2 is a blue gradient, from low = "#132B43" to high = "#56B1F7" which can be reproduced as
scales::seq_gradient_pal(low = "#132B43", high = "#56B1F7", space = "Lab")

The default discrete scale in ggplot2 is a range of hues from hcl,
scales::hue_pal(h = c(0, 360) + 15, c = 100, l = 65, h.start = 0,
direction = 1)

In the next post of this series we’ll describe how one can fine-tune or change altogether these default colours, and, perhaps more importantly, give some pointers on choosing an appropriate colour scheme for a particular graphic.
A question was raised today on the mailing list: Is there an easy way to add a watermark to a ggplot?
There are several options, depending on the type of watermark and the required level of control over the output,
add a text label using annotate (the original idea of the poster)
add a custom grob (graphical object from the Grid package), using annotation_custom
In either case, the placement of a watermark at an absolute location on the plot is greatly facilitated if you use +/- Inf values, which correspond to the extreme edges of the plot panel.
Here is an example with annotate
library(ggplot2)
library(grid)
qplot(1:10, rnorm(10)) +
annotate("text", x = Inf, y = -Inf, label = "PROOF ONLY",
hjust=1.1, vjust=-1.1, col="white", cex=6,
fontface = "bold", alpha = 0.8)
where the label is placed at the bottom-right, and the justification is adjusted to make sure the label stays in the panel area.

Below is a fancier example with a custom grob, which we define such that its width spans the full plot panel, even after resizing the interactive plot window,
watermarkGrob <- function(lab = "PROOF ONLY"){
grob(lab=lab, cl="watermark")
}
## custom draw method to
## calculate expansion factor on-the-fly
drawDetails.watermark <- function(x, rot = 45, ...){
cex <- convertUnit(unit(1,"npc"), "mm", val=TRUE) /
convertUnit(unit(1,"grobwidth", textGrob(x$val)), "mm",val=TRUE)
grid.text(x$lab, rot=rot, gp=gpar(cex = cex, col="white",
fontface = "bold", alpha = 0.5))
}
qplot(1:10, rnorm(10)) +
annotation_custom(xmin=-Inf, ymin=-Inf, xmax=Inf, ymax=Inf, watermarkGrob())

You can of course replace this grob with a more complex one, e.g a table of labels to tile the panel with multiple repetitions of the watermark, or an external graphic (consider the annotation_raster function), etc.
As an example, the following function uses rpatternGrob from the gridExtra package to tile multiple copies of the R logo, imported as a raster image,
library(png)
library(gridExtra)
## import logo as raster image
m <- readPNG(system.file("img", "Rlogo.png", package="png"), FALSE)
w <- matrix(rgb(m[,,1],m[,,2],m[,,3], m[,,4] * 0.2), # adjust alpha
nrow=dim(m)[1])
qplot(1:10, rnorm(10), geom = "blank") +
annotation_custom(xmin=-Inf, ymin=-Inf, xmax=Inf, ymax=Inf,
rpatternGrob(motif=w, motif.width = unit(1, "cm"))) +
geom_point()

This time we made sure that the logo was the first layer plotted, so that it doesn’t obfuscate the data but stays in the background.