Choosing color combinations and palettes
We often need more than one color to represent various elements in graphs. Palettes are combinations of colors, which is a convenient way to use multiple colors without choosing individual colors separately. R provides inbuilt color palettes as well as the ability to create our own custom palettes. Using palettes is a good way to avoid repeatedly choosing or setting colors in multiple locations, which can be a source of error and confusion. This helps in separating the presentation settings of a graph from the construction.
Getting ready
All you need to try out in this recipe is to run R and type the recipe in the command prompt. You can also choose to save the recipe as a script so that you can use it again later on. One new library needs to be installed, which is also explained.
How to do it...
We can change the current palette by passing a character vector of colors to the palette()
function as shown in the following code:
palette(c("red","blue","green","orange"))
To use the colors in the current palette, we can refer to them by the index number. For example, palette()[1]
will be red
.
How it works...
R has a default palette of colors that can be accessed by calling the palette()
function. If we run the palette()
command just after starting R, we get the default palette:
palette() [1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow" [8] "gray"
To revert to the default palette type, use the following code:
palette("default")
When a vector of color names is passed to the palette()
function, it sets the current palette to those colors. We must enter valid color names; otherwise, we will get an invalid color name error.
There's more...
Besides the default palette provided by the palette()
function, R has many more built-in palettes and additional palette libraries. One of the most commonly used palettes is the heat.colors()
palette, which provides a range of colors from red through yellow to white, based on number of colors specified by the n
argument. For example, heat.colors(10)
produces a palette of ten warm colors from red to white.
Other palettes are rainbow()
, terrain.colors()
, cm.colors()
, and topo.colors
, which take the number of colors as an argument.
RColorBrewer
is a very good color palette package that creates nice-looking color palettes, especially for thematic maps. This is an R implementation of the RColorBrewer
palettes, which provides three types of palettes: sequential, diverging, and qualitative. More information on this is available at http://www.colorbrewer.org.
To use RColorBrewer
, we need to install and load it:
install.packages("RColorBrewer") library(RColorBrewer)
To see all the RColorBrewer
palettes, run the following command on the R prompt:
display.brewer.all()
The names of the palettes are displayed in the left-hand margin, and the colors in each palette are displayed in each row running to the right.
To use one of the palettes, let's say YlOrRd
(which, as the names suggests, is a combination of yellows and reds), we can use the brewer.pal()
function:
brewer.pal(7,"YlOrRd") [1] "#FFFFB2" "#FED976" "#FEB24C" "#FD8D3C" "#FC4E2A" "#E31A1C" "#B10026"
The brewer.pal
function takes two arguments: the number of colors we wish to choose and the name of the palette. The minimum number of colors is three but the maximum varies from palette to palette.
We can view the colors of an individual palette using the display.brewer.pal()
command:
display.brewer.pal(7,"YlOrRd")
To use a specific color of the palette, we can refer to the color by its index number. So, the first color in the palette is brewer.pal(7,"YlOrRd")[1]
, the second is brewer.pal(7,"YlOrRd")[2]
, and so on.
We can set the current palette to the preceding one using the palette()
function:
palette(brewer.pal(7,"YlOrRd"))
Now, we can refer to the individual colors as palette()[1]
, palette()[2]
, and so on. We can also store the palette as a vector:
pal1<- brewer.pal(7,"YlOrRd")
See also
We will see the use of a lot of color palettes throughout the recipes in this book, starting from Chapter 4, Creating Scatter Plots.