S Graphics - The University of Auckland

S Graphics
Paul Murrell
[email protected]
The University of Auckland
S Graphics – p.1/38
Overview
Things that are covered
Command-line Graphics
Customising Traditional S Graphics
Customising Traditional Trellis Graphics
Things that are not covered
GUI Graphics
Editable Graphics
Traditional High-Level Graphics functions
S Graphics – p.2/38
Overview
Why customize?
Annotation
New Plots
Diagrams
S Graphics Fundamentals
Graphics Regions and Coordinate Systems
Directing Graphics Output
Producing Graphics Output
Adding to Existing Plots
Plots from First Principles
Trellis Graphics
S Graphics – p.3/38
Annotation
Tim Hoar, Eric Gilleland, Doug Nychka
S Graphics – p.4/38
New Plots - 1
The Bioconductor Project
S Graphics – p.5/38
New Plots - 2
Corey A. Moffet, Texas Tech University
S Graphics – p.6/38
Diagrams
David Scott, The University of Auckland
S Graphics – p.7/38
S Graphics Fundamentals
Graphics Regions and Coordinate Systems
Outer Margins
Figure Regions
Figure Margins
Plot Regions
Directing Graphics Output
Which graphics functions to use
Producing Graphics Output
Graphical parameters
S Graphics – p.8/38
Outer Margins and Figure Region
Figure Region
Outer margin 4
Outer margin 2
Outer margin 3
Outer margin 1
par(oma=c(0, 0, 0, 0), omi=)
par(mfrow=c(1, 1), mfcol=c(1, 1), fig=, fin=)
S Graphics – p.9/38
Arbitrary Figure Regions
Figure 2
Figure 1
par(fig=c(0.1, 0.6, 0.1, 0.6))
par(new=T)
par(fig=c(0.4, 0.9, 0.4, 0.8))
S Graphics – p.10/38
Figure Margins and Plot Region
Outer margin 3
Outer margin 4
Plot Region
Figure Margin 4
Figure Margin 2
Outer margin 2
Figure Margin 3
Figure Margin 1
Outer margin 1
par(mar=c(5.1, 4.1, 4.1, 2.1), mai=)
par(pty="m", pin=, plt=)
S Graphics – p.11/38
Maximum y value
User Coordinates
Minimum y value
yi
The location (xi, yi)
Minimum x value
xi
Maximum x value
<plot.function>(..., xlim=, ylim=)
par(xaxs="r", yaxs="r")
S Graphics – p.12/38
Figure Margin Coordinates
11
Figure
Margin
2
0
0 lines
3 lines
11
Figure
Margin
1
0
3 lines 0 lines
S Graphics – p.13/38
Outer Margin Coordinates
Current
Plot
11
Current
Plot
11
0
0
11
1
lin
0
es
Outer Margin 1
lin
es
0
3
lin
0
Outer Margin 1
lin
3
0
es
11
es
0
S Graphics – p.14/38
Directing Graphics Output
Plot Region
text()
points()
lines()
arrows()
polygon()
segments()
box()
abline()
Figure Margins Outer Margins
mtext()
mtext()
axis()
S Graphics – p.15/38
Graphical Parameters
Permanent settings
par(<param>=)
Temporary settings
<plot.function>(..., <param>=)
col
lwd
lty
font
pch
srt
colour of lines, text, ...
line width
line type
font face (plain, bold, italic)
type of plotting symbol
string rotation
S Graphics – p.16/38
Adding to Existing Plots
User Coordinates are not always obvious
10
9
8
7
6
5
4
3
2
1
0
2
4
6
8
10
> midpts <- barplot(1:5, density = -1)
> text(midpts, 0.1, 1:5, srt = 90, adj = 0)
S Graphics – p.17/38
Adding to Existing Plots
Querying the current region and coordinate settings
> plot(0:1, 1:2)
> par("usr")
[1] -0.04 1.04 0.96
> par("uin")
[1] 6.603704 4.659259
2.04
Some complex plots revert region and coordinate settings
(panel functions)
> E.intervals <- co.intervals(ethanol$E, 16, 0.25)
> coplot(NOx ˜ C | E, given.values = E.intervals, data = ethanol,
+
panel = function(x, y, col, pch)
+
points(x, y);
+
axis(1, tck=1, lty=2, labels=F);
+
axis(2, tck=1, lty=2, labels=F) )
S Graphics – p.18/38
Adding to Existing Plots
Given : E
0.6
10
14
0.8
0.9
18
8
1.0
10
14
1.1
1.2
18
1
2
3
4
1
2
3
4
NOx
1
2
3
4
1
2
3
4
8
0.7
8
10
14
18
8
10
14
18
C
S Graphics – p.19/38
0
2
4
1:10
6
8
10
Plots from First Principles
0
2
4
6
8
10
S Graphics – p.20/38
Plots from First Principles
Create regions and coordinate systems
> par(omi=rep(0, 4), mar=c(5.1, 4.1, 4.1, 2.1),
mfrow=c(1, 1))
> plot(0, type="n", xlim=c(0, 10), ylim=c(0,10),
+
axes=F, xlab="", ylab="")
Draw data symbols in plot region
> par(col=1, lty=1, lwd=1, cex=1, srt=0)
> points(1:10)
Draw axes and labels in the figure margins
>
>
>
>
box()
axis(1)
axis(2)
mtext("1:10", side=2, line=3)
S Graphics – p.21/38
12
14
Plots from First Principles
Group 3
6
8
10
Group 4
4
Group 2
0
2
Group 1
S Graphics – p.22/38
Plots from First Principles
Create area for barplot, leaving room for legend.
par(fig=c(0, 0.8, 0, 1), mar=c(4, 4, 4, 2))
Draw barplot.
barplot(matrix(sample(1:4, 16, replace=T), ncol=4),
angle=45, density=1:4*10, col=1)
Stay on same page and set up region and coordinates for
legend.
par(new=T)
par(fig=c(0.8, 1, 0, 1), mar=c(4, 0, 4, 2))
plot(0, xlim=c(0, 1), ylim=c(0, 5), axes=F, xlab="", ylab="",
type="n")
S Graphics – p.23/38
Plots from First Principles
Figure out what 0.5” is in user coordinates.
size <- par("cxy")/par("cin")*.5
Draw legend elements and a dashed border. .
box(lty=2)
for (i in 1:4)
polygon(c(0.5 - size[1]/2, 0.5 - size[1]/2,
0.5 + size[1]/2, 0.5 + size[1]/2),
c(i, i + size[2], i + size[2], i),
angle=45, density=i*10)
text(0.5, i-0.2, paste("Group", i))
S Graphics – p.24/38
Traditional Trellis Graphics
are
“multipanel conditioning”, where multiple plots of versus
produced for different levels of a grouping variable .
plots with a design principles based on human perception
experiments
text is horizontal
colours and symbols have easily-distinguishable defaults
“banking” of plots
S Graphics – p.25/38
Traditional Trellis Graphics
−2
Strip Region
−1
0
1
2
Strip Region
2
1
Plot Region
Plot Region
0
−1
y
−2
Strip Region
Strip Region
Plot Region
Plot Region
2
1
0
−1
−2
−2
−1
0
1
2
x
S Graphics – p.26/38
Trellis Graphics Regions and Coordinate Systems
Controlling number of columns and rows of plots.
xyplot(y ˜ x | g, layout=c(2, 4))
−2
g
−1
0
1
g
2
4
2
0
−2
4
g
g
g
g
2
0
y
−2
4
2
0
−2
g
4
g
2
0
−2
−2
−1
0
1
2
x
S Graphics – p.27/38
Trellis Graphics Regions and Coordinate Systems
Controlling the aspect ratio of each plot.
xyplot(y ˜ x | g, aspect=0.5)
−2
g
0
2
4
g
2
1
0
−1
−2
g
g
g
g
2
1
0
y
−1
−2
2
1
0
−1
−2
g
g
2
1
0
−1
−2
−2
0
2
4
x
S Graphics – p.28/38
Trellis Graphics Regions and Coordinate Systems
Controlling the position of the entire plot.
myplot <- xyplot(y ˜ x | g)
print.trellis(myplot, position=c(0, 0.5, 1, 1))
−3
−2
−1
0
g
g
g
g
1
2
2
1
0
−1
g
y
2
1
0
−1
g
g
g
2
1
0
−1
−3
−2
−1
0
1
2
−3
−2
−1
0
1
2
x
S Graphics – p.29/38
Trellis Graphics Regions and Coordinate Systems
Controlling the scales on the axes of the plot regions.
xyplot(y ˜ x | g, scales=list(limits=c(-4, 4)))
−3 −2 −1
0
g
g
g
g
1
2
3
2
1
0
−1
−2
g
2
y
1
0
−1
−2
g
g
g
2
1
0
−1
−2
−3 −2 −1
0
1
2
3
−3 −2 −1
0
1
2
3
x
S Graphics – p.30/38
Customising Trellis Plots
The panel argument.
xyplot(y ˜ x | g,
panel=function(x, y, ...)
panel.xyplot(x, y, ...);
abline(0, 0)
)
The arguments to the panel function depend on the trellis
function; for trellis function <name>, look at the default panel
function panel.<name>
S Graphics – p.31/38
Customising Trellis Plots
−2
−1
0
g
g
g
g
1
2
2
1
0
−1
−2
g
2
y
1
0
−1
−2
g
g
g
2
1
0
−1
−2
−2
−1
0
1
2
−2
−1
0
1
2
x
S Graphics – p.32/38
Customising Trellis Plots
The strip argument.
xyplot(y ˜ x | g,
strip=function(which.given, which.panel,
var.name, factor.levels,
shingle.intervals,
par.strip.text,
strip.names, style)
text(0, 0.5,
paste("Variable ", which.given, ": Level ",
which.panel[which.given], sep=""), adj=0)
)
S Graphics – p.33/38
Customising Trellis Plots
−2
−1
0
Variable 1: Level 7
Variable 1: Level 8
Variable 1: Level 4
Variable 1: Level 5
1
2
2
1
0
−1
−2
Variable 1: Level 6
2
y
1
0
−1
−2
Variable 1: Level 1
Variable 1: Level 2
Variable 1: Level 3
2
1
0
−1
−2
−2
−1
0
1
2
−2
−1
0
1
2
x
S Graphics – p.34/38
Customising Trellis Plots
The prepanel argument.
xyplot(y ˜x|g,
prepanel=function(x ,y)
list(xlim=range(x)+c(-1,1),
ylim=range(y)+c(-1,1))
)
S Graphics – p.35/38
Customising Trellis Plots
−4
−2
0
g
g
g
g
2
2
0
−2
g
y
2
0
−2
g
g
g
2
0
−2
−4
−2
0
2
−4
−2
0
2
x
S Graphics – p.36/38
Trellis Graphical Parameters
Temporary settings:
> xyplot(y ˜ x | g, pch=16)
Permanent settings:
> trellis.par.get("plot.symbol")
$cex:
[1] 0.8
$col:
[1] 2
$font:
[1] 1
$pch:
[1] 1
> ps <- trellis.par.get("plot.symbol")
> ps$pch=16
> trellis.par.set("plot.symbol", ps)
S Graphics – p.37/38
CD Contents
base.pdf
"Traditional S Graphics" notes
base.S
S code for examples in base.pdf
slide.pdf
"Traditional S Graphics" slides
ex.pdf
"Traditional S Graphics" exercises
ex.S
S code for creating data for exercises
model.pdf
"Traditional S Graphics" answers to exercises
model.S
S code for answers to exercises
S Graphics – p.38/38