S Graphics Paul Murrell paul@stat.auckland.ac.nz The University of - - PowerPoint PPT Presentation

s graphics
SMART_READER_LITE
LIVE PREVIEW

S Graphics Paul Murrell paul@stat.auckland.ac.nz The University of - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

S Graphics

Paul Murrell

paul@stat.auckland.ac.nz

The University of Auckland

S Graphics – p.1/38

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

Annotation

Tim Hoar, Eric Gilleland, Doug Nychka

S Graphics – p.4/38

slide-5
SLIDE 5

New Plots - 1

The Bioconductor Project

S Graphics – p.5/38

slide-6
SLIDE 6

New Plots - 2

Corey A. Moffet, Texas Tech University

S Graphics – p.6/38

slide-7
SLIDE 7

Diagrams

David Scott, The University of Auckland

S Graphics – p.7/38

slide-8
SLIDE 8

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

slide-9
SLIDE 9

Outer Margins and Figure Region

Figure Region

Outer margin 1 Outer margin 2 Outer margin 3 Outer margin 4

par(oma=c(0, 0, 0, 0), omi=) par(mfrow=c(1, 1), mfcol=c(1, 1), fig=, fin=)

S Graphics – p.9/38

slide-10
SLIDE 10

Arbitrary Figure Regions

Figure 1 Figure 2

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

slide-11
SLIDE 11

Figure Margins and Plot Region

Plot Region

Figure Margin 1 Figure Margin 2 Figure Margin 3 Figure Margin 4 Outer margin 1 Outer margin 2 Outer margin 3 Outer margin 4

par(mar=c(5.1, 4.1, 4.1, 2.1), mai=) par(pty="m", pin=, plt=)

S Graphics – p.11/38

slide-12
SLIDE 12

User Coordinates

Minimum x value Maximum x value Minimum y value Maximum y value The location (xi, yi) xi yi

<plot.function>(..., xlim=, ylim=) par(xaxs="r", yaxs="r")

S Graphics – p.12/38

slide-13
SLIDE 13

Figure Margin Coordinates

Figure Margin 1 11 0 lines 3 lines Figure Margin 2 11 0 lines 3 lines

S Graphics – p.13/38

slide-14
SLIDE 14

Outer Margin Coordinates

11 Current Plot Outer Margin 1 11 l i n e s 3 l i n e s 11 11 Current Plot Outer Margin 1 1 l i n e s 3 l i n e s

S Graphics – p.14/38

slide-15
SLIDE 15

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

slide-16
SLIDE 16

Graphical Parameters

Permanent settings par(<param>=) Temporary settings <plot.function>(..., <param>=) col colour of lines, text, ... lwd line width lty line type font font face (plain, bold, italic) pch type of plotting symbol srt string rotation

S Graphics – p.16/38

slide-17
SLIDE 17

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)

2 4 6 8 10 1 2 3 4 5 6 7 8 9 10

S Graphics – p.17/38

slide-18
SLIDE 18

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

slide-19
SLIDE 19

Adding to Existing Plots

1 2 3 4 8 10 14 18 8 10 14 18 1 2 3 4 1 2 3 4 8 10 14 18 8 10 14 18 1 2 3 4

C NOx

0.6 0.7 0.8 0.9 1.0 1.1 1.2

Given : E

S Graphics – p.19/38

slide-20
SLIDE 20

Plots from First Principles

2 4 6 8 10 2 4 6 8 10 1:10

S Graphics – p.20/38

slide-21
SLIDE 21

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

slide-22
SLIDE 22

Plots from First Principles

2 4 6 8 10 12 14 Group 1 Group 2 Group 3 Group 4

S Graphics – p.22/38

slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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

slide-26
SLIDE 26

Traditional Trellis Graphics

x y Plot Region −2 −1 1 2 −2 −1 1 2 Strip Region Plot Region Strip Region Plot Region Strip Region Plot Region −2 −1 1 2 Strip Region −2 −1 1 2 S Graphics – p.26/38

slide-27
SLIDE 27

Trellis Graphics Regions and Coordinate Systems Controlling number of columns and rows of plots.

xyplot(y ˜ x | g, layout=c(2, 4))

x y −2 2 4 −2 −1 1 2 g g g −2 2 4 g −2 2 4 g g g −2 2 4 g −2 −1 1 2 S Graphics – p.27/38

slide-28
SLIDE 28

Trellis Graphics Regions and Coordinate Systems Controlling the aspect ratio of each plot.

xyplot(y ˜ x | g, aspect=0.5)

x y −2 −1 1 2 −2 2 4 g g g −2 −1 1 2 g −2 −1 1 2 g g g −2 −1 1 2 g −2 2 4 S Graphics – p.28/38

slide-29
SLIDE 29

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

x y −1 1 2 −3 −2 −1 1 2 g g −3 −2 −1 1 2 g g g −1 1 2 g −1 1 2 g g −3 −2 −1 1 2 S Graphics – p.29/38

slide-30
SLIDE 30

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

x y −2 −1 1 2 −3 −2 −1 1 2 3 g g −3 −2 −1 1 2 3 g g g −2 −1 1 2 g −2 −1 1 2 g g −3 −2 −1 1 2 3 S Graphics – p.30/38

slide-31
SLIDE 31

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

slide-32
SLIDE 32

Customising Trellis Plots

x y −2 −1 1 2 −2 −1 1 2 g g −2 −1 1 2 g g g −2 −1 1 2 g −2 −1 1 2 g g −2 −1 1 2 S Graphics – p.32/38

slide-33
SLIDE 33

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

slide-34
SLIDE 34

Customising Trellis Plots

x y −2 −1 1 2 −2 −1 1 2 Variable 1: Level 1 Variable 1: Level 2 −2 −1 1 2 Variable 1: Level 3 Variable 1: Level 4 Variable 1: Level 5 −2 −1 1 2 Variable 1: Level 6 −2 −1 1 2 Variable 1: Level 7 Variable 1: Level 8 −2 −1 1 2 S Graphics – p.34/38

slide-35
SLIDE 35

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

slide-36
SLIDE 36

Customising Trellis Plots

x y −2 2 −4 −2 2 g g −4 −2 2 g g g −2 2 g −2 2 g g −4 −2 2 S Graphics – p.36/38

slide-37
SLIDE 37

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

slide-38
SLIDE 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