Adjusting Key Parameters in R

R Graph Cookbook

R Graph Cookbook

Detailed hands-on recipes for creating the most useful types of graphs in R – starting from the simplest versions to more advanced applications

  • Learn to draw any type of graph or visual data representation in R
  • Filled with practical tips and techniques for creating any type of graph you need; not just theoretical explanations
  • All examples are accompanied with the corresponding graph images, so you know what the results look like
  • Each recipe is independent and contains the complete explanation and code to perform the task as efficiently as possible
        Read more about this book      

(For more resources on R, see here.)

Setting colors of points, lines, and bars

In this recipe we will learn the simplest way to change the colors of points, lines, and bars in scatter plots, line plots, histograms, and bar plots.

Getting ready

All you need to try out this recipe is to run R and type the recipe at the command prompt. You can also choose to save the recipe as a script so that you can use it again later on.

How to do it...

The simplest way to change the color of any graph element is by using the col argument. For example, the plot() function takes the col argument:

plot(rnorm(1000), col="red")


Adjusting Key Parameters in R

The code file can be downloaded from here.

If we choose plot type as line, then the color is applied to the plotted line. Let’s use the dailysales.csv example dataset. First, we need to load it:

Sales <- read.csv("dailysales.csv",header=TRUE)
type="l", #Specify type of plot as l for line


Adjusting Key Parameters in R

Similarly, the points() and lines() functions apply the col argument's value to the plotted points and lines respectively.

barplot() and hist() also take the col argument and apply them to the bars. So the following code would produce a bar plot with blue bars:


The col argument for boxplot() is applied to the color of the boxes plotted.

How it works...

The col argument automatically applies the specified color to the elements being plotted, based on the plot type. So, if we do not specify a plot type or choose points, then the color is applied to points. Similarly, if we choose plot type as line then the color is applied to the plotted line and if we use the col argument in the barplot() or histogram() commands, then the color is applied to the bars.

col accepts names of colors such as red, blue, and black. The colors() (or colours()) function lists all the built-in colors (more than 650) available in R. We can also specify colors as hexadecimal codes such as #FF0000 (for red), #0000FF (for blue), and #000000 (for black). If you have ever made any web pages¸ you would know that these hex codes are used in HTML to represent colors.

col can also take numeric values. When it is set to a numeric value, the color corresponding to that index in the current color palette is used. For example, in the default color palette the first color is black and the second color is red. So col=1 and col=2 refers to black and red respectively. Index 0 corresponds to the background color.

There's more...

In many settings, col can also take a vector of multiple colors, instead of a single color. This is useful if you wish to use more than one color in a graph. The heat.colors() function takes a number as an argument and returns a vector of those many colors. So heat.colors(5) produces a vector of five colors.
Type the following at the R prompt:


You should get the following output:

[1] "#FF0000FF" "#FF5500FF" "#FFAA00FF" "#FFFF00FF" "#FFFF80FF"

Those are five colors in the hexadecimal format.
Another way of specifying a vector of colors is to construct one:

barplot(as.matrix(sales[,2:4]), beside=T,

In the example, we set the value of col to c("red","blue","green","orange","pink"), which is a vector of five colors.

We have to take care to make a vector matching the length of the number of elements, in this case bars we are plotting. If the two numbers don’t match, R will 'recycle’ values by repeating colors from the beginning of the vector. For example, if we had fewer colors in the vector than the number of elements, say if we had four colors in the previous plot, then R would apply the four colors to the first four bars and then apply the first color to the fifth bar. This is called recycling in R:

barplot(as.matrix(sales[,2:4]), beside=T,

In the example, both the bars for the first and last data rows (Seattle and Mumbai) would be of the same color (red), making it difficult to distinguish one from the other.

One good way to ensure that you always have the correct number of colors is to find out the length of the number of elements first and pass that as an argument to one of the color palette functions. For example, if we did not know the number of cities in the example we have just seen; we could do the following to make sure the number of colors matches the number of bars plotted:

barplot(as.matrix(sales[,2:4]), beside=T,

We used the length() function to find out the length or the number of elements in the vector sales$City and passed that as the argument to heat.colors(). So, regardless of the number of cities we will always have the right number of colors.

See also

In the next four recipes, we will see how to change the colors of other elements. The fourth recipe is especially useful where we look at color combinations and palettes.

        Read more about this book      

(For more resources on R, see here.)

Setting plot background colors

The default background color of all plots in R is white, which is usually the best choice as it is least distracting for data analysis. However, sometimes we may wish to use another color. We will see how to set background colors in this recipe.

Getting ready

All you need to try out this recipe is to run R and type the recipe at the command prompt. You can also choose to save the recipe as a script so that you can use it again later on.

How to do it...

To set the plot background color to gray we use the bg argument in the par() command:



Adjusting Key Parameters in R

How it works...

The par() command's bg argument sets the background color for the entire plotting area including the margins for any subsequent plots on the same device. Until the plotting device is closed or a new device is initiated, the background color stays the same.

There's more...

It is more likely that we want to set the background color only for the plot region (within the axes) but there is no straightforward way to do this in R. We must draw a rectangle of the desired color in the background and then make our graph on top of it:

rect(x[1],x[3],x[2],x[4],col="lightgray ")

First we draw the plot with type set to "n" so that the plotted elements are invisible. This does not show the graph points or lines but sets the axes up, which we need for the next step. par("usr") gets us the co-ordinates of the plot region in a vector of form c(xleft, xright, ybottom, ytop). We then use the rect() function to draw a rectangle with a fill color that we wish to use for the plot background. Note that rect() takes a set of arguments representing the xleft, ybottom, xright, ytop co-ordinates. So we must pass the values we obtained from par("usr") in the correct order. Then, finally we redraw the graph with the correct type (points or lines).

Adjusting Key Parameters in R

Setting colors for text elements: axis annotations, labels, plot titles, and legends

Axis annotations are the numerical or text values placed beside tick marks on an axis. Axis labels are the names or titles of axes, which tell the reader what the values on a particular axis represent. In this recipe, we will learn how to set the colors for these elements and legends.

Getting ready

All you need to try out this recipe is to run R and type the recipe at the command prompt. You can also choose to save the recipe as a script so that you can use it again later on.

How to do it...

Let's say we want to make the axis value annotations black, the labels of the axes gray, and the plot title dark blue, you should do the following:

main="Plot Title",


Adjusting Key Parameters in R

How it works...

Colors for axis annotations, labels, and plot titles can be set either using the par() command before making the graph or in the graph command such as plot() itself. The arguments for setting the colors for axis annotations, labels, and plot titles are col.axis, col.lab, and col.main respectively.

They are similar to the col argument and take names of colors or hex codes as values, but do not take a vector of more than one color.

There's more...

If we use the par() command, the difference is that par() will apply these settings to every subsequent graph, until it is reset either by specifying the settings again or starting a new graphics device:


The col.axis argument can also be passed to the axis() function, which is useful for making a custom axis if you do not want to use the default axis. The col.lab argument does not work with axis() and must be specified in par() or the main graph function such as plot() or barplot().

The col.main argument can also be passed to the title() function, which is useful for adding a custom plot title if you do not want to use the default title:

title("Sales Figures for 2010", col.main="blue")

Axis labels can also be specified with title():


This is handy because you can specify two different colors for the X and Y axes:

title(xlab="X axis",col.lab="red")
title(ylab="Y axis",col.lab="blue")

When setting the axis titles with the title() command, we must set xlab and ylab to empty strings "" in the original plot command to avoid overlapping titles.

Choosing color combinations and palettes

We often need more than one color to represent various elements in graphs. Palettes are combinations of colors which are a convenient way to use multiple colors without choosing individual colors separately. R provides inbuilt color palettes as well as the ability to make 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. It helps in separating the presentation settings of a graph from the construction.

Getting ready

All you need to try out this recipe is to run R and type the recipe at 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. For example:


To use the colors in the current palette, we can refer to them by the index number. For example, palette()[1] would be red.

How it works...

R has a default palette of colors which can be accessed by calling the palette() function. If we run the palette() command just after starting R, we get the default palette:

[1] "black" "red" "green3" "blue" "cyan" "magenta"
[8] "gray"

To revert back to the default palette type:


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 the number of colors specified by the argument n. For example, heat.colors(10) produces a palette of 10 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. It is an R implementation of the RColorBrewer palettes, which provides three types of palettes: sequential, diverging, and qualitative. More information is available at Colorbrewer 2.0

To use RColorBrewer, we need to install and load it:


To see all the RColorBrewer palettes run the following command at the R prompt:



Adjusting Key Parameters in R

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:

[1] "#FFFFB2" "#FED976" "#FEB24C" "#FD8D3C" "#FC4E2A" "#E31A1C"

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 by using the display.brewer.pal() command:


To use a specific color of the palette we can refer to it 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 previous one by using the palette() function:


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")

Setting fonts for annotations and titles

For most data analysis we can just use the default fonts for titles. However, sometimes we may want to choose different fonts for presentation and publication purposes. Selecting fonts can be tricky as it depends on the operating system and the graphics device. We will see some simple ways to choose fonts in this recipe.

Getting ready

All you need to try out this recipe is to run R and type the recipe at the command prompt. You can also choose to save the recipe as a script so that you can use it again later on.

How to do it...

The font family and face can be set with the par() command:


How it works...

A font is specified in two parts: a font family (such as Helvetica or Arial) and a font face within that family (such as bold or italic).

The available font families vary by operating system and graphics devices. So R provides some proxy values which are mapped on to the relevant available fonts irrespective of the system. Standard values for family are "serif", "sans", and "mono".

The font argument takes numerical values: 1 corresponds to plain text (the default), 2 to bold face, 3 to italic, and 4 to bold italic.

For example, par(family="serif",font=2) sets the font to a bold Times New Roman on Windows. You can check the other font mappings by running the windowsFonts() command at the R prompt.

The fonts for axis annotations, labels, and plot main title can be set separately using the font.axis, font.lab, and font.main arguments respectively.

There's more...

The choice of fonts is very limited if we just use the proxy family names. However, we can use a wide range of fonts if we are exporting our graphs in the PostScript or PDF formats. The postscriptFonts() and pdfFonts() functions show all the available fonts for those devices. To see the PDF fonts, run the following command:

[1] "serif" "sans" "mono"
[4] "AvantGarde" "Bookman" "Courier"
[7] "Helvetica" "Helvetica-Narrow" "NewCenturySchoolbook"
[10] "Palatino" "Times" "URWGothic"
[13] "URWBookman" "NimbusMon" "NimbusSan"
[16] "URWHelvetica" "NimbusSanCond" "CenturySch"
[19] "URWPalladio" "NimbusRom" "URWTimes"
[22] "Japan1" "Japan1HeiMin" "Japan1GothicBBB"
[25] "Japan1Ryumin" "Korea1" "Korea1deb"
[28] "CNS1" "GB1"

To use one of these font families in a PDF, we can pass the family argument to the pdf() function:

pdf(family="AvantGarde") pdf(paste(family="AvantGarde")


In this article we saw how to set colors of points, lines, and bars. We also took a look at setting plot background colors and setting colors for text elements, choosing color combinations and palettes, and setting fonts for annotations and titles.

In the next article, we will see how to choose styles for various graph elements in R.

Further resources on this subject:

You've been reading an excerpt of:

R Graphs Cookbook

Explore Title