S Graphics Paul Murrell paul@stat.auckland.ac.nz 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 Outer margin 3 Outer margin 2 Outer margin 4 Figure Region 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 Figure Margin 3 Figure Margin 2 Figure Margin 4 Outer margin 2 Outer margin 4 Plot Region 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
User Coordinates Maximum y value The location ( x i , y i ) y i Minimum y value x i Minimum x value 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 11 0 0 lines 3 lines 0 lines Figure Margin 1 3 lines S Graphics – p.13/38
Outer Margin Coordinates 11 11 Current Current Plot Plot 0 0 0 11 0 0 11 1 s s e e n n i i l l 0 0 Outer Margin 1 Outer Margin 1 s s e e n n i i l l 3 3 S Graphics – p.14/38
Directing Graphics Output Plot Region Figure Margins Outer Margins text() mtext() mtext() points() axis() lines() arrows() polygon() segments() box() abline() S Graphics – p.15/38
Graphical Parameters Permanent settings par(<param>=) Temporary settings <plot.function>(..., <param>=) colour of lines, text, ... col line width lwd line type lty font face (plain, bold, italic) font type of plotting symbol pch string rotation srt S Graphics – p.16/38
Adding to Existing Plots User Coordinates are not always obvious > midpts <- barplot(1:5, density = -1) > text(midpts, 0.1, 1:5, srt = 90, adj = 0) 10 8 6 4 2 10 1 2 3 4 5 6 7 8 9 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 2.04 > par("uin") [1] 6.603704 4.659259 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 0.7 0.8 0.9 1.0 1.1 1.2 8 10 14 18 8 10 14 18 4 3 2 1 4 3 2 1 NOx 4 3 2 1 4 3 2 1 8 10 14 18 8 10 14 18 C S Graphics – p.19/38
Plots from First Principles 10 8 6 1:10 4 2 0 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
Plots from First Principles 14 12 Group 4 10 Group 3 8 6 Group 2 4 Group 1 2 0 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 plots with a design principles based on human perception experiments text is horizontal colours and symbols have easily-distinguishable defaults “banking” of plots “multipanel conditioning”, where multiple plots of versus are produced for different levels of a grouping variable . S Graphics – p.25/38
Traditional Trellis Graphics −2 −1 0 1 2 Strip Region Strip Region 2 1 0 Plot Region Plot Region −1 −2 y Strip Region Strip Region 2 1 0 Plot Region Plot Region −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 −1 0 1 2 g g 4 2 0 −2 g g 4 2 0 −2 y g g 4 2 0 −2 g g 4 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 0 2 4 g g 2 1 0 −1 −2 g g 2 1 0 −1 −2 y g g 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 1 2 g g 2 1 0 −1 g g g 2 y 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 1 2 3 g g 2 1 0 −1 −2 g g g 2 1 y 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 1 2 g g 2 1 0 −1 −2 g g g 2 1 y 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 1 2 Variable 1: Level 7 Variable 1: Level 8 2 1 0 −1 −2 Variable 1: Level 4 Variable 1: Level 5 Variable 1: Level 6 2 1 y 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 2 g g 2 0 −2 g g g 2 y 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
Recommend
More recommend