In its simplest form, a palette in R is simply a vector of colors. This vector can be include the hex triplet or R color names.

The default palette can be seen through palette():

> palette("default")# you'll only need this line if you've previously changed the palette from the default> palette()[1]"black""red""green3""blue""cyan""magenta""yellow"[8]"gray"

Defining your own palettes

If you want to make your own palette, you can just create your own vector of colors. It's fine for your vector to include a mixture of hex triplets and R color names. You can use the palette function above, but generally it's best to just store each palette as a standard vector. For one thing, you can use more than one palette that way. Here's how you can define your own palette:

colors <-c("#A7A7A7","dodgerblue","firebrick","forestgreen","gold")

Now let's try using our palette. For now let's just color each bar of a histogram. This is a silly example, but I think it's the easiest way to show how to get R to utilize your palette. In the following example, there are six bars, but only five colors. You can see that R will cycle through your palette to fill all the shapes.

hist(discoveries, col = colors)

A more sensible use of color is to use a different color for each of a number of summary statistics:

Even though we have to provide brewer.pal with the number of colors we want, we won't necessarily need to use all those colors later. We can still choose a color from the vector like we have previously. When we're setting a col setting to the full palette, we'll be more concerned with how many colors are included in the palette , but even there, we can choose a subset of the whole palette:

Now that we're familiar with making our own palettes and using the built-in palettes in grDevices and RColorBrewer, I'm planning a future post about a more practical (but also more complicated) example of using palettes: making maps.