Graphical Parameters in R
Graphical Parameters in R
You can customize many features of your graphs (fonts, colors, axes, titles) through
graphic options.
One way is to specify these options in through the par( ) function. If you set
parameter values here, the changes will be in effect for the rest of the session or
until you change them again. The format
is par(optionname=value, optionname=value, ...)
# Set a graphical parameter using par()
hist(mtcars$mpg, col.lab="red")
See the help for a specific high level plotting function (e.g. plot, hist, boxplot) to
determine which graphical parameters can be set this way.
The remainder of this section describes some of the more important graphical
parameters that you can set.
Plotting Symbols
Use the pch= option to specify symbols to use when plotting points. For symbols 21
through 25, specify border color (col=) and fill color (bg=).
Lines
You can change lines using the following options. This is particularly useful for
reference lines, axes, and fit lines.
option description
lty line type. see the chart below.
lwd line width relative to the default (default=1). 2 is twice as wide.
Colors
Options that specify colors include the following.
option description
col Default plotting color. Some functions (e.g. lines) accept a
vector of values that are recycled.
col.axis color for axis annotation
col.lab color for x and y labels
col.main color for titles
col.sub color for subtitles
fg plot foreground color (axes, boxes - also sets col= to same)
bg plot background color
You can specify colors in R by index, name, hexadecimal, or RGB.
For example col=1, col="white", and col="#FFFFFF" are equivalent.
The following chart was produced with code developed by Earl F. Glynn. See
his Color Chart for all the details you would ever need about using colors in R.
You can also create a vector of n contiguous colors using the
functions rainbow(n), heat.colors(n), terrain.colors(n), topo.colors(n),
and cm.colors(n).
colors() returns all available color names.
Fonts
You can easily set font size and style, but font family is a bit more complicated.
option description
font Integer specifying font to use for text.
1=plain, 2=bold, 3=italic, 4=bold italic, 5=symbol
font.axis font for axis annotation
font.lab font for x and y labels
font.main font for titles
font.sub font for subtitles
ps font point size (roughly 1/72 inch)
text size=ps*cex
family font family for drawing text. Standard values are "serif",
"sans", "mono", "symbol". Mapping is device dependent.
In windows, mono is mapped to "TT Courier New", serif is mapped to"TT Times New
Roman", sans is mapped to "TT Arial", mono is mapped to "TT Courier New", and
symbol is mapped to "TT Symbol" (TT=True Type). You can add your own
mappings.
plot(1:10,1:10,type="n")
windowsFonts(
A=windowsFont("Arial Black"),
B=windowsFont("Bookman Old Style"),
D=windowsFont("Symbol")
click to view
option description
mar numerical vector indicating margin size c(bottom, left, top,
right) in lines. default = c(5, 4, 4, 2) + 0.1
mai numerical vector indicating margin size c(bottom, left, top,
right) in inches
pin plot dimensions (width, height) in inches
For finer control or for modularization, you can use the functions described below.
Titles
Use the title( ) function to add labels to a plot.
title(main="main title", sub="sub-title",
Many other graphical parameters (such as text size, font, rotation, and color) can
also be specified in the title( ) function.
# Add a red title and a blue subtitle. Make x and y
Text Annotations
Text can be added to graphs using the text( ) and mtext( ) functions. text( ) places
text within the graph while mtext( ) places text in one of the four margins.
(To practice adding text to plots in R, try this interactive exercise.)
text(location, "text to place", pos, ...)
option description
location location can be an x,y coordinate. Alternatively, the text can
be placed interactively via mouse by specifying location
as locator(1).
pos position relative to location. 1=below, 2=left, 3=above,
4=right. If you specify pos, you can specify offset= in
percent of character width.
side which margin to place text. 1=bottom, 2=left, 3=top, 4=right.
you can specify line= to indicate the line in the margin
starting with 0 and moving out. you can also
specify adj=0 for left/bottom alignment or adj=1 for top/right
alignment.
Other common options are cex, col, and font (for size, color, and font style
respectively).
Labeling points
You can use the text( ) function (see above) for labeling point as well as for adding
other text annotations. Specify location as a set of x, y coordinates and specify the
text to place as a vector of labels. The x, y, and label vectors should all be the same
length.