You are on page 1of 4

03/10/13

Quick-R: Graphical Parameters

Home

Interface

Input

Manage

Stats

Adv Stats

Graphs

Adv Graphs

Blog

Quick-R
accessing the power of R

Search

R Interface
Graphical Parameters Axes and Text Combining Plots Lattice Graphs ggplot2 Graphs Probability Plots Mosaic Plots Correlograms Interactive Graphs

Graphical Parameters
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, ...)

#S e tag r a p h i c a lp a r a m e t e ru s i n gp a r ( ) p a r ( ) #v i e wc u r r e n ts e t t i n g s o p a r< -p a r ( ) #m a k eac o p yo fc u r r e n ts e t t i n g s p a r ( c o l . l a b = " r e d " )#r e dxa n dyl a b e l s h i s t ( m t c a r s $ m p g ) #c r e a t eap l o tw i t ht h e s en e ws e t t i n g s p a r ( o p a r ) #r e s t o r eo r i g i n a ls e t t i n g s

R in Action

A second way to specify graphical parameters is by providing the optionname=value pairs directly to a high level plotting function. In this case, the options are only in effect for that specific graph.

#S e tag r a p h i c a lp a r a m e t e rw i t h i nt h ep l o t t i n gf u n c t i o n h i s t ( m t c a r s $ m p g ,c o l . l a b = " r e d " )

R in Action significantly expands upon this material. Use promo code ria38 for a 38% discount.

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.

Top Menu
Home The R Interface Data Input Data Management Basic Statistics Advanced Statistics Basic Graphs Advanced Graphs Blog cex.axis cex.lab

Text and Symbol Size


The following options can be used to control text and symbol size in graphs.

option cex

description number indicating the amount by which plotting text and symbols should be scaled relative to the default. 1=default, 1.5 is 50% larger, 0.5 is 50% smaller, etc. magnification of axis annotation relative to cex magnification of x and y labels relative to cex

cex.main magnification of titles relative to cex cex.sub magnification of subtitles relative to cex

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=).

www.statmethods.net/advgraphs/parameters.html

1/4

03/10/13

Quick-R: Graphical Parameters

Lines
You can change lines using the following options. This is particularly useful for reference lines, axes, and fit lines.

option description lty lwd line type. see the chart below. line width relative to the default (default=1). 2 is twice as wide.

Colors
Options that specify colors include the following.

option col col.axis col.lab

description Default plotting color. Some functions (e.g. lines) accept a vector of values that are recycled. color for axis annotation color for x and y labels

col.main color for titles col.sub fg bg color for subtitles plot foreground color (axes, boxes - also sets col= to same) 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.

www.statmethods.net/advgraphs/parameters.html

2/4

03/10/13

Quick-R: Graphical Parameters

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 font font.axis font.lab

description Integer specifying font to use for text. 1=plain, 2=bold, 3=italic, 4=bold italic, 5=symbol font for axis annotation font for x and y labels

font.main font for titles font.sub ps family font for subtitles font point size (roughly 1/72 inch) text size=ps*cex 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.

#T y p ef a m i l ye x a m p l e s-c r e a t i n gn e wm a p p i n g s p l o t ( 1 : 1 0 , 1 : 1 0 , t y p e = " n " ) w i n d o w s F o n t s ( A = w i n d o w s F o n t ( " A r i a lB l a c k " ) , B = w i n d o w s F o n t ( " B o o k m a nO l dS t y l e " ) , C = w i n d o w s F o n t ( " C o m i cS a n sM S " ) , D = w i n d o w s F o n t ( " S y m b o l " ) ) t e x t ( 3 , 3 , " H e l l oW o r l dD e f a u l t " ) t e x t ( 4 , 4 , f a m i l y = " A " , " H e l l oW o r l df r o mA r i a lB l a c k " ) t e x t ( 5 , 5 , f a m i l y = " B " , " H e l l oW o r l df r o mB o o k m a nO l dS t y l e " ) t e x t ( 6 , 6 , f a m i l y = " C " , " H e l l oW o r l df r o mC o m i cS a n sM S " ) t e x t ( 7 , 7 , f a m i l y = " D " ," H e l l oW o r l df r o mS y m b o l " )

www.statmethods.net/advgraphs/parameters.html

3/4

03/10/13

Quick-R: Graphical Parameters

click to view

Margins and Graph Size


You can control the margin size using the following parameters.

option description mar mai pin numerical vector indicating margin size c(bottom, left, top, right) in lines. default = c(5, 4, 4, 2) + 0.1 numerical vector indicating margin size c(bottom, left, top, right) in inches plot dimensions (width, height) in inches

For complete information on margins, see Earl F. Glynn's margin tutorial.

Going Further
See help(par) for more information on graphical parameters. The customization of plotting axes and text annotations are covered next section.

Copyright 2012 Robert I. Kabacoff, Ph.D. | Sitemap Designed by WebTemplateOcean.com

www.statmethods.net/advgraphs/parameters.html

4/4

You might also like