Jason Freels
27 September 2016
Base R graphics -
The Lattice graphics package
Implementation of Trellis graphics suite of data visualization tools
Focused on multivariate data
The ggplot2 graphics package
Plotting system based on the grammar of graphics
Tries to take the good parts of base R and lattice graphics and none of the bad parts
Takes care of many details that make plotting a hassle
Provides powerful tools to make it easy to produce complex, multi-layered graphics
15 Questions All R Users Have About Plots
plot(x, y,...)plot(x, y,...) function is executedThe following events occur
A new plot window is created -
A new plot device is activated inside the plot window
The values defined in the vector x are stored as the \(x\) coordinates of the points \((x_i,y_i), i=1,...,n\)
The values defined in the vector y are stored as the \(y\) coordinates of the points \((x_i,y_i), i=1,...,n\)
If x and y contain the same number of elements the points \((x_i,y_i), i=1,...,n\) are printed in the active plot device
Additional arguments, denoted by ... are then executed to change the active plot device settings, these may include
xlim = c(lower, upper) \(\rightarrow\) upper and lower limits of x-axis
ylim = c(lower, upper) \(\rightarrow\) upper and lower limits of y-axis
xlab = "text" \(\rightarrow\) title of x-axis
ylab = "text" \(\rightarrow\) title of y-axis
main = "text" \(\rightarrow\) title of plot
axes = TRUE \(\rightarrow\) show or suppress plot axes
yaxt = 's' or 'n' \(\rightarrow\) show or suppress y-axis values
xaxt = 's' or 'n' \(\rightarrow\) show or suppress x-axis values
ann = TRUE \(\rightarrow\) show or suppress ALL plot annotations
type = 'p' \(\rightarrow\) Change the type of objects being plotted
pch = 1 \(\rightarrow\) Set the type of plot character
lwd = 1 \(\rightarrow\) Set the line width
lty = 1 \(\rightarrow\) Change the line type
cex = 1 \(\rightarrow\) Set the character expansion value
col = 1 \(\rightarrow\) Change the color of plotted objects
The shiny app below shows how the arguments to plot(x, y,...) effect the appearance of a plot
For this app both x and y are vectors of length \(10\) with values randomly sampled from the set of integers \(1 - 50\)
x <- sample(1:50, size = 10)
y <- sample(1:50, size = 10)
It's unlikely that x = y since the number of distinct samples of size \(n=10\) that could have been generated is
\[ \begin{aligned} \binom{n}{k} &= \frac{n!}{k! \, (n - k)!}\\\\ \binom{50}{10} &= \frac{50!}{10! \, 40!} =1.0272278\times 10^{10} \;\text{possible samples} \end{aligned} \]
plot(x, y,...) function runsThe plot device remains active so additional objects can be added
Add text with text(x,...)
Add equations with expression(...)
Add points with points(x,...)
Add lines with lines(x,...) or abline(a,b, h, v, coef, reg,...)
Add a legend with legend( )
par(...)plot(x, y,...) is executed...A new plot window is created
A new plot device is activated inside the plot window
The values defined in the vector x are stored as the \(x\) coordinates of the points \((x_i,y_i), i=1,...,n\)
The values defined in the vector y are stored as the \(y\) coordinates of the points \((x_i,y_i), i=1,...,n\)
If x and y contain the same number of elements the points \((x_i,y_i), i=1,...,n\) are printed in the active plot device
Additional arguments, denoted by ... are then executed to change the active plot device settings, these may include
That can only be changed with the par(...) function
xlog = FALSE
ylog = FALSE
adj = 0.5
ann = TRUE
ask = FALSE
bg = "white"
bty = "o"
cex = 1
cex.axis = 1
cex.lab = 1
cex.main = 1.2
cex.sub = 1
cin = c(0.15, 0.20)
col = "black"
col.axis = "black"
col.lab = "black"
col.main = "black"
col.sub = "black"
cra = c(14.4, 19.2)
crt = 0
csi = 0.2
cxy = c(0.03702180, 0.08792819)
din = c(5.291667, 4.114583)
err = 0
family = ""
fg = "black"
fig = c(0, 1, 0, 1)
fin = c(5.291667, 4.114583)
font = 1
font.axis = 1
font.lab = 1
font.main = 2
font.sub = 1
lab = c(5, 5, 7)
las = 0
lend = "round"
lheight = 1
ljoin = "round"
lmitre = 10
lty = "solid"
lwd = 1
mai = c(1.02, 0.82, 0.82, 0.42)
mar = c(5.1, 4.1, 4.1, 2.1)
mex = 1
mfcol = c(1, 1)
mfg = c(1, 1, 1, 1)
mfrow = c(1, 1)
mgp = c(3, 1, 0)
mkh = 0.001
new = FALSE
oma = c(0, 0, 0, 0)
omd = c(0, 1, 0, 1)
omi = c(0, 0, 0, 0)
page = TRUE
pch = 1
pin = c(4.051667, 2.274583)
plt = c(0.1549606, 0.9206299, 0.2478987, 0.8007089)
ps = 12
pty = "m"
smo = 1
srt = 0
tck = NA
tcl = -0.5
usr = c(0, 1, 0, 1)
xaxp = c(0, 1, 5)
xaxs = "r"
xaxt = "s"
xpd = FALSE
yaxp = c(0, 1, 5)
yaxs = "r"
yaxt = "s"
ylbias = 0.2
plot(x, y,...) can be used to display much more than just sets of points
In the previous outline of what happens when plot(x, y,...) executes, some steps were purposefully skipped
plot(x, y,...) is a "generic" function
When plot(objects) is called, R first checks the class( ) and typeof( ) object being plotted
If "objects" means the vectors x and y, plot(x, y,...) calls plot.xy( )
If "objects" means a matrix, plot(x, y,...) calls plot.matrix( )
plot(object)
In the packages that install with base R, there are several specific plot functions
plot.stepfun( ) example: plot(stepfun(1:3, c(1,2,4,3)))
plot.ecdf( ) example: plot(ecdf(rnorm(24)))
plot.table( ) example: plot(Titanic)
Several "wrapper" functions use plot(x, y,...) to create special purpose plots
- `plot.function( )` example: `curve(sin, -2*pi, 2*pi, xname = "t")`