Show Menu
Cheatography

R graphical Cheat Sheet (DRAFT) by

R graphical essentials

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Basic code

pdf("mygraph.pdf")
  attach(mtcars) 
  plot(wt, mpg)
  abline(lm(mpg ~ wt))
  title("Regression of MPG on Weight")
  detach(mtcars)

  x <- c(1:10)
  y <- x
  lines(x, y, type="b", pch=22, col="blue", lty=2)
dev.off()
Variations include win.me­taf­ile(), png(), jpeg(), bmp(), tiff(), xfig(), postsc­ript()
Other functions include dev.new(), dev.ne­xt(), dev.pr­ev(), dev.set()

Basic graph

Graphical parameters

par(op­tio­nna­me=­value, option­nam­e=v­alue, ...)


opar <- par(no.re­ado­nly­=TRUE)

par(lty=2, pch=17)

plot(dose, drugA, type="b­")


# type - "­b": both points and lines, "­l": lines, "­p": points, "­c": lines part of "­b", "­n": no plotting, "­o": both "­ove­rpl­ott­ed"


par(opar)

Symbols and lines

pch
Specifies the symbol to use when plotting points
cex
Specifies the symbol size. A number indicating the amount plotting symbols are scaled relative to default. 1=default, 1.5 is 50% larger, 0.5 is 50% smaller, and so forth.
lty
Specifies the line type
lwd
Specifies the line width. Expressed relative to default (=1), e.g.
lwd=2
- a line twice as wide as the default

Plot symbols

Line types

Color parameters

col
Default plotting color. Some functions (e.g. lines and pie) accept a vector of values that are recycled.
col.axis
Color for axis text
col.lab
Color for axis labels
col.main
Color for titles
col.sub
Color for subtitles
fg
The plot's foreground color
bg
The plot's background color
rainbow(#)
Produces # contiguous "­rai­nbo­w" colors
gray(0­:10/10)
Specify gray levels as a vector of numbers between 0 and 1. This produces 10 gray levels.

Text parameters

cex
Number indicating the amount by which plotted text should be scaled relative to the default
cex.axis
Magnif­ication of axis text relative to
cex
cex.lab
Magnif­ication of axis labels relative to
cex
cex.main
Magnif­ication of titles relative to
cex
cex.sub
Magnif­ication of subtitles relative to
cex
 

Font parameters

font
Integer specifying font to use for plotted text. 1=plain, 2=bold, 3 =italic, 4=bold italic, 5=symbol (Adobe symbol encoding)
font.axis
Font for axis text
font.lab
Font for axis labels
font.main
Font for titles
font.sub
Font for subtitles
ps
Font point size (roughly 1/72 inch). The text size = ps*cex.
family
Font family for drawing text. Standard values are
serif
,
sans
, and
mono
.
Mapping for font family created via window­sFont() function.
For Mac, use quartz­Fon­ts().

window­sFonts(

A=wind­ows­Fon­t("Arial Black"),

B=wind­ows­Fon­t("B­ookman Old Style"),

C=wind­ows­Fon­t("Comic Sans MS")

)

Graph and margin parameters

pin
Plot dimensions (width, height) in inches
mai
Numerical vector indicating margin size, c(bott­om,­lef­t,t­op,­right). Expressed in inches
mar
Numerical vector indicating margin size, c(bott­om,­lef­t,t­op,­right). Expressed in lines. The default is c(5,4,4,2) + 0.1.

Example code

dose <- c(20,30,40,45,60)
drugA <- c(16,20,27,40,60)
drugB <- c(15,18,25,31,40)
opar <- par(no.readonly=TRUE)
par(pin=c(2,3))
par(lwd=2, cex=1.5)
par(cex.axis=.75, font.axis=3)
plot(dose, drugA, type="b", pch=19, lty=2, col="red")
lines(dose, drugB, type="b", pch=23, lty=6, col="blue", bg="green")
par(opar)

Resulting graph

Graph text and custom­ization

ann=FALSE
Using in plot() statement or par() statement remove default titles and labels
title()
title(­mai­n="main title", col.ma­in=­"­red­", sub="su­b-t­itl­e", col.su­b="b­lue­", xlab="x­-axis label", ylab="y­-axis label", col.la­b="g­ree­n", cex.la­b=0.75)
axis()
Create custom axes. When creating a custom axis, suppress axis by using option
axes=FALSE
(suppr­esses all axes, including axis frame lines, unless
frame.p­lo­t=TRUE
),
xaxt="n­"
or
yaxt="n­"
. See Axis options.
text()
Add text within graph, typically labeling points or text annota­tions, e.g.
text(l­oca­tion, "­tex­t", pos, ...)
mtext()
Add text to margin of plot
 
e.g.
mtext(­"­tex­t", side=4, line=3, cex.lab=1, las=2, col="bl­ue")
plotmath()
Add mathem­atical symbols and formulas to graph
abline()
Add reference lines to graph e.g.
abline­(h=­yva­lues, v=xvalues)
 
abline­(h=­c(1­,5,7)
adds horizontal lines at y=1, 5 & 7
legend­(lo­cation, title, legend, ...)
location - (x,y) coordi­nate, locato­r(1), keywords, i.e.
bottom, bottom­left, left, topleft, top, topright, right, bottom­right, center
. Use
inset=
to specify amount to move legend into graph as fraction of plot region.
 
title - character string for the legend title (optional)
 
legend - character vector with the labels
 
... - Other options:
col=
,
pch=
,
lwd=
,
lty=
,
fill=
,
bty=
,
bg=
,
cex=
,
text.col=
,
horiz=TRUE
 

Example code

attach(mtcars)
plot(wt, mpg, main="Mileage vs. Car Weight", xlab="Weight", ylab="Mileage", pch=18, col="blue")
text(wt, mpg, row.names(mtcars), cex=0.6, pos=4, col="red")
detach(mtcars)

Resulting graph

Combining graphs

Used in
par()
or
layout()
function:

mfrow=­c(n­rows, ncols)
- fill by row
mfcol=­c(n­rows, ncols)
- fill by column

layout­(mat)

where
mat
is matrix object specifying location of multiple plots to combine
e.g.
layout­(ma­tri­x(c­(1,­1,2,3), 2, 2, byrow = TRUE))

- one figure placed in row 1 and two figures placed in row 2

Optional parameters for
layout()
:
widths()
- a vector of values for widths of columns
heights()
- a vector of values for heights of rows

e.g.
layout­(ma­tri­x(c(1, 1, 2, 3), 2, 2, byrow = TRUE), widths­=c(3, 1), height­s=c(1, 2))


Used in
par()
function:

fig=c(x1, x2, y1, y2), new = TRUE
- Plot within limits of (x1,x2) and (y1,y2),
new = TRUE
option adds figure to existing graph

Axis options

side
An integer indicating the side of the graph to draw the axis (1=bottom, 2=left, 3=top, 4=right)
at
A numeric vector indicating where tick marks should be drawn
labels
A character vector of labels to be placed at the tick marks (if
NULL
, the
at
values will be used)
pos
The coordinate at which the axis line is to be drawn (i.e. the value on the other axis where it crosses)
lty
Line type
col
The line and tick mark color
las
Labels are parallel (=0) or perpen­dicular (=2) to the axis
tck
Length of tick mark as a fraction of the plotting region (a '-' number is outside the graph, a '+' number is inside, 0 suppresses ticks, 1 creates gridli­nes). Default is -0.01.
Adding minor tick marks require Hmisc package.
librar­y(H­misc)

minor.t­ic­k(nx=n, ny=n, tick.r­atio=n)

where nx, ny are no. of intervals to divide major tick marks on x- and y-axis, tick.ratio is size of minor relative to major tick mark

demo(p­lot­math)