r: graphicshji/courses/statcomputing/graphics1.pdf · r: graphics 140.776 statistical computing...

36
R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Upload: others

Post on 26-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

R: Graphics

140.776 Statistical Computing

August 21, 2011

140.776 Statistical Computing R: Graphics

Page 2: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Run R program from a file

Create a file ex1.R that contains two lines:

x<-rnorm(1000)hist(x)

140.776 Statistical Computing R: Graphics

Page 3: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Run R program from a file

>source("ex1.R")

140.776 Statistical Computing R: Graphics

Page 4: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Basic graphics

Plotting in R is easy. There are many functions for plotting yourdata:

plot(): 2-D graphics

boxplot(): box plot

hist(): histogram

qqplot(): QQ plot

. . .

140.776 Statistical Computing R: Graphics

Page 5: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Histogram

> x<-rnorm(1000)> hist(x)

Histogram of x

x

Fre

quen

cy

−3 −2 −1 0 1 2 3

050

100

150

200

140.776 Statistical Computing R: Graphics

Page 6: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Box plot

> boxplot(x)

●●

●●●

●●

−3

−2

−1

01

23

140.776 Statistical Computing R: Graphics

Page 7: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Normal QQ plot

You can use qqnorm() to check whether data are collected from anormal, a long tail, or a short tail distribution

−4 −2 0 2 4

0.0

0.1

0.2

0.3

0.4

x

y

normallong tailshort tail

140.776 Statistical Computing R: Graphics

Page 8: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Normal QQ plot

> y<-rnorm(2000, mean=2, sd=3)> qqnorm(y)

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●

●●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●●

●●●

●●

●●

●●●

●●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●●

●●

●●

●●

●●

●●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

−3 −2 −1 0 1 2 3

−5

05

10

Normal Q−Q Plot

Theoretical Quantiles

Sam

ple

Qua

ntile

s

140.776 Statistical Computing R: Graphics

Page 9: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Exercise

Which one of the following is long tail? Short tail? Normal?

Histogram of w

w

Fre

quen

cy

−2 −1 0 1 2

050

100

150

200

Histogram of w

w

Fre

quen

cy

−10 −5 0 5 10

010

020

030

040

0

Histogram of w

w

Fre

quen

cy

−2 0 2 4

050

100

150

200

140.776 Statistical Computing R: Graphics

Page 10: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Exercise

Find the corresponding normal QQ plot for each histogram:

Histogram of w

w

Fre

quen

cy

−2 −1 0 1 2

050

100

150

200

Histogram of w

w

Fre

quen

cy−10 −5 0 5 10

010

020

030

040

0

Histogram of w

w

Fre

quen

cy

−2 0 2 4

050

100

150

200

●●

●●●

●●

●●

●●

● ●●●

●●

●●

●●

●●

●●●

●●●

● ●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●● ●

● ●

●●

●●

●●●

●●

●●

●●

●●

● ●● ●

●●

●●●●

●●

●●

● ●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ●● ● ●●●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

● ●

●●

●●

●●●

●●

●●

●●

●●●

●●

●●●

● ●●

●●

●●●

●●

●●

●●

● ●●

● ●

●●

●● ●

●●

●●

●●●

●●●

●●

●●

●●

●●

●● ●●●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●●●

●●

●●●

● ●

●●

●●●

●●

●●

●● ●

●●

●●

●●

●●

●●

●●●

●●

●●

●●●

●●●●

●●

●●

●●

●●●●

●●

●●●●

● ●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

●●●

●●

●●

● ●

●●

● ●●

●●

●●●

−3 −2 −1 0 1 2 3

−10

−5

05

10

Normal Q−Q Plot

Theoretical Quantiles

Sam

ple

Qua

ntile

s

●●

●●●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●●

●●

●●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

−3 −2 −1 0 1 2 3

−3

−2

−1

01

23

Normal Q−Q Plot

Theoretical Quantiles

Sam

ple

Qua

ntile

s

●●

●●

●●

●●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

−3 −2 −1 0 1 2 3

−2

−1

01

2

Normal Q−Q Plot

Theoretical Quantiles

Sam

ple

Qua

ntile

s

140.776 Statistical Computing R: Graphics

Page 11: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Normal QQ plot

If you want to find out, you can try t-distribution (a long taildistribution)

> w<-rt(1000,df=3)> hist(w)> qqnorm(w)

140.776 Statistical Computing R: Graphics

Page 12: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

QQ plot

qqplot() allow you compare two distributions:

> x<-rnorm(1000)> y<-rnorm(2000, mean=2, sd=3)> z<-rt(1000,df=3)> qqplot(x,y)> qqplot(x,z)

●●●

●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●

●●●●●●●●●●●

●● ●

−3 −2 −1 0 1 2 3

−5

05

10

x

y

●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●

●●●

−3 −2 −1 0 1 2 3

−10

−5

05

x

z

140.776 Statistical Computing R: Graphics

Page 13: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Plot

plot() is perhaps the most frequently used plotting function in R.Let us study Y = X + ε, where X ∼ N(10, 2.52) andε ∼ N(0, 0.252).

> x<-rnorm(1000,mean=10,sd=2.5)> y<-x+rnorm(1000,mean=0,sd=0.25)> plot(x,y)

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

● ●

●●

●●●

●●

●●●

●●

●●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

● ●

●●●

●●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

5 10 15

510

15

x

y

140.776 Statistical Computing R: Graphics

Page 14: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Plot

Now let us rotate the plot 45◦ and plot (y-x) vs. (y+x)/2. This is the socalled “M-A plot”.

> M<-y-x> A<-(y+x)/2> plot(A,M)

●●

●●

●●●

●●

●●

●●

●●

●●●

●●

● ●●●

●●

●●

●●

●●

●●

●●

●●●

●● ●

●●

●●

●●

●●●●

●●

●●

●●●

●●

●●

●●●

●● ●

● ●

●●

● ●

●●

●●

●●

●●

●●

● ●●

●● ●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

● ●

●●

●●

●● ●●

●●

●●

●●

●●●

●●

●●

● ●

●●

●●

●●

● ●

● ●

●● ●●

● ●

●●

●●

● ●

● ●

●●

●●

●●

●●

●●●

●●

●●

●●●

● ●●●

●●

●●

●●●

●●

●●

●●

● ●

●●

●●

● ●

●●

●●

●●

● ●

●●●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

● ●●

● ●

●●

●●●

●●

●●

● ●

● ●

●●

● ●

● ●●

●●

●●

● ●

●●

●●

●● ●

●●

●● ●

● ●●

5 10 15

−0.

50.

00.

5

A

M

140.776 Statistical Computing R: Graphics

Page 15: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Setting graphical parameters

The MA plot looks quite different from the original plot. Why?

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

● ●

●●

●●●

●●

●●●

●●

●●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

● ●

●●●

●●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

5 10 15

510

15

x

y

●●

●●

●●●

●●

●●

●●

●●

●●●

●●

● ●●●

●●

●●

●●

●●

●●

●●

●●●

●● ●

●●

●●

●●

●●●●

●●

●●

●●●

●●

●●

●●●

●● ●

● ●

●●

● ●

●●

●●

●●

●●

●●

● ●●

●● ●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

● ●

●●

●●

●● ●●

●●

●●

●●

●●●

●●

●●

● ●

●●

●●

●●

● ●

● ●

●● ●●

● ●

●●

●●

● ●

● ●

●●

●●

●●

●●

●●●

●●

●●

●●●

● ●●●

●●

●●

●●●

●●

●●

●●

● ●

●●

●●

● ●

●●

●●

●●

● ●

●●●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

● ●●

● ●

●●

●●●

●●

●●

● ●

● ●

●●

● ●

● ●●

●●

●●

● ●

●●

●●

●● ●

●●

●● ●

● ●●

5 10 15

−0.

50.

00.

5A

M

140.776 Statistical Computing R: Graphics

Page 16: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Plot

If you want to have the two figures on a similar scale, you can usethe xlim and ylim options of the plot() function:

> plot(A,M, xlim=c(0,20), ylim=c(-10,10))

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

● ●

●●

●●●

●●

●●●

●●

●●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

● ●

●●●

●●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

5 10 15

510

15

x

y ●● ●●

●●●●

●● ●● ● ●● ●● ●● ●●●●● ●● ●

●●

●●● ●●●●● ●●●●● ● ● ● ●● ●

●● ●●● ●●● ●●●●● ●

●●●● ●

●●● ●●● ●● ●●

●● ●● ●●●● ●● ● ●●

● ●●●●●●

●●●●

●●● ●●●● ●● ●●● ●●● ●● ●● ●

● ●● ●●●

● ●● ●●●

●●●●

●●

●● ●● ●●

●●●●

● ● ●●

●● ●● ●● ●● ●●●

●● ●●●

●●

●●●●● ●●●●● ●●

●● ●● ●●●● ●● ●● ●● ●● ●● ●●

● ●●●

●●●●

● ●●● ● ●● ● ●

●● ●● ●●●

●●● ● ●●● ●●●

●● ●●●● ●●● ● ●● ●

●●●●●● ●●

●● ●●

●●●● ●●

● ●● ●●

●●

● ●●●●●●● ●● ● ●●● ●●●● ● ●● ● ●●

●●●

● ●●●●

●● ● ●● ●

● ●●● ●●● ●● ●●

●● ●●

●●

●●

●●●

● ●● ● ● ●● ●●● ● ● ●●

● ● ●● ●● ●●●●● ●●● ●●● ● ●●

●●

●●● ●

●●

●● ●

●●● ●●

● ●●● ● ●● ●● ●● ●● ●

● ●●

●● ● ●●● ●

●● ●

●●● ●● ● ●●● ●●● ● ●● ●●

● ●● ●●●●● ●●

●●

●● ●● ●●

● ●● ●●●●

●●

●● ● ●●●●● ●● ●● ●●● ●●● ●

● ●● ●● ● ●● ●●●●●

●● ●●●●

●● ●

●●

●● ● ●●●

●● ●●●

●● ●●●●

●●●

●●● ●● ●●●

●●●●●●

●● ● ●● ●● ●● ●● ●●● ●● ●● ●● ●●●

●● ●● ● ●●● ● ●

●●●●●●

● ●● ●● ●

●● ●●● ●●●● ●●

● ●● ●●●●

● ●●● ●

●● ●

● ● ● ●● ● ●●●●

●●● ● ●●●●●●

●● ●● ●● ●

●●●

●●

●●● ●● ●● ●●

●●●●●● ●●●● ●● ●●●●● ●● ●

● ●●

●●●● ●● ●●●●●

● ●●

●●●

● ●● ●●●● ●●

●● ●

●●

●●●

●● ● ●●●● ● ●●●●●● ●●● ●

●● ●

●●

● ●●●●●

●● ●●●● ●

● ●●●

●●●● ●●● ● ●● ● ● ● ● ●

●● ●●● ●● ● ●●●

● ●● ●●●●● ● ●●● ●

●● ●●● ● ●

●●● ● ● ●● ● ●●

● ●●●●●● ●●●

● ●●● ●● ●●●●● ●

● ●● ● ●

● ● ●●●● ●

● ●●● ● ●●●

● ●● ●●●

● ●● ● ●● ●● ●● ●●

●●●●●

● ●●●

● ●●●

●● ●● ●● ●● ●● ● ●● ●

●●●

● ●●● ●●●

● ●●●●● ●

●●●

● ● ●● ●●●● ●● ● ●●● ●● ●●●● ●●● ●●

● ●● ●●●●● ● ● ●●● ●●●●

● ●●●● ●

●● ●● ●

●● ●

●●● ●●

●● ●●● ● ●●

●● ● ●● ●● ●● ●●

●●

●●●●●

● ●●●●

0 5 10 15 20

−10

−5

05

10

A

M

140.776 Statistical Computing R: Graphics

Page 17: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Plot

Indeed, there are a lot of parameters you can adjust.

> plot(A,M, xlim=c(0,20), ylim=c(-5,5), main="M-A plot")

●●

●●

● ●

●●

● ●

●●

●●

●●

●●● ●● ●●

●●

●●●

● ●●

●● ●

●●

●●

●●

●●

●●

●●

● ●

●● ●●

●●

●●

● ●●

●●

● ●

●● ●

●●

● ●

●●●

●●

● ●●

●●●●

●●●

●●

● ●●

●●

● ●●

●●

●●

● ●●●

●●

●●●

●●

● ●

●●

●●

● ●●

●●

●●

● ●

●●

●●

●●

●●●

●●

●● ●●

●●●

● ●●

●● ●● ●

●● ●●

●●

●●●●

●●

● ●●

●●

●● ●●

● ●●

●●

●●

● ●●

●●

●●

● ●

●●●

●●

● ●

●●●

●●

●●

● ●

●●●

●●

●●

●●

●● ●

●●

●●

●● ●

●●

● ●● ●

●●

●● ●●●

● ● ●●●

●●

● ●

●● ●

●●

●●

●●●

●●

●●● ●

●●

●●

●●

●●

●●

● ●

● ●●

●●

●●●

●●●●

●●

●●

●●

●●

●● ●●

●●

●●●

●●● ●

●● ●

●● ●●

●●●

● ●●●●

●●

● ● ●●

●●

●●

●●

● ●●

●●●

●●

●●●

●●●

●●

●●

● ●●

●● ●

●●●

●●

●●●

●●

● ●

●●

●● ● ●●●

●●●

●●

●●

●● ●●

●●

●●

●●

● ●●

●●

● ●●

● ●

● ●●

●●●

●●

●●●

● ●●

●●

●●●

● ●● ●

●●

●●

●●

●●

●●●

●●

●●

●●

●●●

● ●●

●●

●●

●●

●●

●● ●

●●●

●●

● ●

●● ●

●●

●●

●●

●●

● ●●

●●

●●●

●●

●●

●●

●●

●●●

●●

●●

● ●

● ●

● ●●

●●

●●●●

●●

●● ●

● ●

●●

● ●

●●●

●●

●●●●

●●

●●● ●

●●

●●●

●● ●●●

●●

●●

●●●

●●●

●● ●

●●

●●●

●●● ●

● ●●

●●●

●●●

● ●

●●

● ●●

●●●

●●

●●●

●●

●●

●● ●●

● ●●

● ●● ●

●● ●●●

●●

●●

●●

●●

●●●

●●●

●●

● ●●

●●

● ●●

●●

● ●

● ●●

●●

●●●

● ●

● ●

●●

●●●●

●●●

●●

● ●

●●

●● ●

●●

●●

●●

●●

● ●●

●● ●

● ●

●● ●

●● ●●●●

●●

● ●

●●

●●

●●

●●●

●●

●●

●●

● ●●●

●●

●●

0 5 10 15 20

−4

−2

02

4

M−A plot

A

M

140.776 Statistical Computing R: Graphics

Page 18: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Plot

Indeed, there are a lot of parameters you can adjust.

> plot(A,M, xlim=c(0,20), ylim=c(-5,5), main="M-A plot",+ sub="A simulation")

●●

●●

● ●

●●

● ●

●●

●●

●●

●●● ●● ●●

●●

●●●

● ●●

●● ●

●●

●●

●●

●●

●●

●●

● ●

●● ●●

●●

●●

● ●●

●●

● ●

●● ●

●●

● ●

●●●

●●

● ●●

●●●●

●●●

●●

● ●●

●●

● ●●

●●

●●

● ●●●

●●

●●●

●●

● ●

●●

●●

● ●●

●●

●●

● ●

●●

●●

●●

●●●

●●

●● ●●

●●●

● ●●

●● ●● ●

●● ●●

●●

●●●●

●●

● ●●

●●

●● ●●

● ●●

●●

●●

● ●●

●●

●●

● ●

●●●

●●

● ●

●●●

●●

●●

● ●

●●●

●●

●●

●●

●● ●

●●

●●

●● ●

●●

● ●● ●

●●

●● ●●●

● ● ●●●

●●

● ●

●● ●

●●

●●

●●●

●●

●●● ●

●●

●●

●●

●●

●●

● ●

● ●●

●●

●●●

●●●●

●●

●●

●●

●●

●● ●●

●●

●●●

●●● ●

●● ●

●● ●●

●●●

● ●●●●

●●

● ● ●●

●●

●●

●●

● ●●

●●●

●●

●●●

●●●

●●

●●

● ●●

●● ●

●●●

●●

●●●

●●

● ●

●●

●● ● ●●●

●●●

●●

●●

●● ●●

●●

●●

●●

● ●●

●●

● ●●

● ●

● ●●

●●●

●●

●●●

● ●●

●●

●●●

● ●● ●

●●

●●

●●

●●

●●●

●●

●●

●●

●●●

● ●●

●●

●●

●●

●●

●● ●

●●●

●●

● ●

●● ●

●●

●●

●●

●●

● ●●

●●

●●●

●●

●●

●●

●●

●●●

●●

●●

● ●

● ●

● ●●

●●

●●●●

●●

●● ●

● ●

●●

● ●

●●●

●●

●●●●

●●

●●● ●

●●

●●●

●● ●●●

●●

●●

●●●

●●●

●● ●

●●

●●●

●●● ●

● ●●

●●●

●●●

● ●

●●

● ●●

●●●

●●

●●●

●●

●●

●● ●●

● ●●

● ●● ●

●● ●●●

●●

●●

●●

●●

●●●

●●●

●●

● ●●

●●

● ●●

●●

● ●

● ●●

●●

●●●

● ●

● ●

●●

●●●●

●●●

●●

● ●

●●

●● ●

●●

●●

●●

●●

● ●●

●● ●

● ●

●● ●

●● ●●●●

●●

● ●

●●

●●

●●

●●●

●●

●●

●●

● ●●●

●●

●●

0 5 10 15 20

−4

−2

02

4

M−A plot

A simulationA

M

140.776 Statistical Computing R: Graphics

Page 19: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Plot

Indeed, there are a lot of parameters you can adjust.

> plot(A,M, xlim=c(0,20), ylim=c(-5,5), main="M-A plot",+ sub="A simulation",+ xlab="Intensity", ylab="log2 Fold Change")

●●

●●

● ●

●●

● ●

●●

●●

●●

●●● ●● ●●

●●

●●●

● ●●

●● ●

●●

●●

●●

●●

●●

●●

● ●

●● ●●

●●

●●

● ●●

●●

● ●

●● ●

●●

● ●

●●●

●●

● ●●

●●●●

●●●

●●

● ●●

●●

● ●●

●●

●●

● ●●●

●●

●●●

●●

● ●

●●

●●

● ●●

●●

●●

● ●

●●

●●

●●

●●●

●●

●● ●●

●●●

● ●●

●● ●● ●

●● ●●

●●

●●●●

●●

● ●●

●●

●● ●●

● ●●

●●

●●

● ●●

●●

●●

● ●

●●●

●●

● ●

●●●

●●

●●

● ●

●●●

●●

●●

●●

●● ●

●●

●●

●● ●

●●

● ●● ●

●●

●● ●●●

● ● ●●●

●●

● ●

●● ●

●●

●●

●●●

●●

●●● ●

●●

●●

●●

●●

●●

● ●

● ●●

●●

●●●

●●●●

●●

●●

●●

●●

●● ●●

●●

●●●

●●● ●

●● ●

●● ●●

●●●

● ●●●●

●●

● ● ●●

●●

●●

●●

● ●●

●●●

●●

●●●

●●●

●●

●●

● ●●

●● ●

●●●

●●

●●●

●●

● ●

●●

●● ● ●●●

●●●

●●

●●

●● ●●

●●

●●

●●

● ●●

●●

● ●●

● ●

● ●●

●●●

●●

●●●

● ●●

●●

●●●

● ●● ●

●●

●●

●●

●●

●●●

●●

●●

●●

●●●

● ●●

●●

●●

●●

●●

●● ●

●●●

●●

● ●

●● ●

●●

●●

●●

●●

● ●●

●●

●●●

●●

●●

●●

●●

●●●

●●

●●

● ●

● ●

● ●●

●●

●●●●

●●

●● ●

● ●

●●

● ●

●●●

●●

●●●●

●●

●●● ●

●●

●●●

●● ●●●

●●

●●

●●●

●●●

●● ●

●●

●●●

●●● ●

● ●●

●●●

●●●

● ●

●●

● ●●

●●●

●●

●●●

●●

●●

●● ●●

● ●●

● ●● ●

●● ●●●

●●

●●

●●

●●

●●●

●●●

●●

● ●●

●●

● ●●

●●

● ●

● ●●

●●

●●●

● ●

● ●

●●

●●●●

●●●

●●

● ●

●●

●● ●

●●

●●

●●

●●

● ●●

●● ●

● ●

●● ●

●● ●●●●

●●

● ●

●●

●●

●●

●●●

●●

●●

●●

● ●●●

●●

●●

0 5 10 15 20

−4

−2

02

4

M−A plot

A simulationIntensity

log2

Fol

d C

hang

e

140.776 Statistical Computing R: Graphics

Page 20: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Plot

Indeed, there are a lot of parameters you can adjust.

> plot(A,M, xlim=c(0,20), ylim=c(-5,5), main="M-A plot",+ sub="A simulation",+ xlab="Intensity", ylab="log2 Fold Change"),+ pch=20)

●●

● ●

●●

● ●

●●

●●

●●

●●●● ●●

●●

●●

● ●●

● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●● ●

●●

● ●

●●

● ●

●●●

●●

● ●●

●●

●●

●●

● ●

●●

●●

●●

● ●●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

● ●

●● ●●

●●●

● ●

●● ●

● ●

●● ●

●●●

●●

●●

● ●●

●●

●●●

● ●●

●● ●

●●

●●

●●

●●●

●●

● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●● ●

●●

● ●● ●

●● ●

●●

● ● ●●

●●

● ●

● ●●

●●

●●

●●

●●

● ●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●●

●●

●●●

●●● ●

●● ●

●●●

●●

● ●●

●●

●●

●● ●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

● ●

● ●

●●●

●●

●● ●

●●

●● ● ●●●

●●●

●●

●●

● ●●

●●

●●

● ●

● ●

● ●

● ●

●●

●●

●●

●●●

● ●

● ●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●● ●

●●●

● ●

● ●

●●

●●

● ●

●●

●●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●●●

●●

● ●

● ●

●●

● ●

●●

●●

●●

●●

●●

●●● ●

●●

●●

●● ●●

●●

●●

●●

●●●

●●

●●

●●●

●●

●●

●●

●●

●●● ●

●●

● ●●

●●

●●

●●

●●

●●

●● ●●

● ●●

● ●● ●

●●

●●

●●

●●

●●

●●●

●●●

●●

●●

●●

●●

●●

●●

● ●●

●●

●●

● ●

● ●

●●

●●

●●

●●●●

● ●

●●

● ●

●●

●●

●●

●●

●● ●

●●

● ●●● ●●●

●●

●●

●●

●●

●●●

●●

● ●●

●●

0 5 10 15 20

−4

−2

02

4M−A plot

A simulationIntensity

log2

Fol

d C

hang

e

140.776 Statistical Computing R: Graphics

Page 21: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Plot

Indeed, there are a lot of parameters you can adjust.

> plot(A,M, xlim=c(0,20), ylim=c(-5,5), main="M-A plot",+ sub="A simulation",+ xlab="Intensity", ylab="log2 Fold Change"),+ pch=20, col="blue")

●●

● ●

●●

● ●

●●

●●

●●

●●●● ●●

●●

●●

● ●●

● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●● ●

●●

● ●

●●

● ●

●●●

●●

● ●●

●●

●●

●●

● ●

●●

●●

●●

● ●●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

● ●

●● ●●

●●●

● ●

●● ●

● ●

●● ●

●●●

●●

●●

● ●●

●●

●●●

● ●●

●● ●

●●

●●

●●

●●●

●●

● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●● ●

●●

● ●● ●

●● ●

●●

● ● ●●

●●

● ●

● ●●

●●

●●

●●

●●

● ●

●●

●●

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●● ●●

●●

●●●

●●● ●

●● ●

●●●

●●

● ●●

●●

●●

●● ●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

● ●

● ●

●●●

●●

●● ●

●●

●● ● ●●●

●●●

●●

●●

● ●●

●●

●●

● ●

● ●

● ●

● ●

●●

●●

●●

●●●

● ●

● ●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●● ●

●●●

● ●

● ●

●●

●●

● ●

●●

●●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●●●

●●

● ●

● ●

●●

● ●

●●

●●

●●

●●

●●

●●● ●

●●

●●

●● ●●

●●

●●

●●

●●●

●●

●●

●●●

●●

●●

●●

●●

●●● ●

●●

● ●●

●●

●●

●●

●●

●●

●● ●●

● ●●

● ●● ●

●●

●●

●●

●●

●●

●●●

●●●

●●

●●

●●

●●

●●

●●

● ●●

●●

●●

● ●

● ●

●●

●●

●●

●●●●

● ●

●●

● ●

●●

●●

●●

●●

●● ●

●●

● ●●● ●●●

●●

●●

●●

●●

●●●

●●

● ●●

●●

0 5 10 15 20

−4

−2

02

4M−A plot

A simulationIntensity

log2

Fol

d C

hang

e

140.776 Statistical Computing R: Graphics

Page 22: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Plot

Indeed, there are a lot of parameters you can adjust.

> plot(A,M, xlim=c(0,20), ylim=c(-5,5), main="M-A plot",+ sub="A simulation",+ xlab="Intensity", ylab="log2 Fold Change"),+ pch=20, col="blue",+ cex=1.2, cex.lab=1.2, cex.main=3, cex.sub=2)

●●

●●

● ●

●●

● ●

●●

●●

●●

●● ●● ●●

●●

●●

● ●●

● ●

●●

●●

●●

●●

●●

● ●

●●●●

●●

●●

● ●

●●

●●

● ●

●●

● ●

●●●

●●

● ●●

●●

●●●

●●

●●

● ●●

●●

●●

●●

● ●●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

●●

●● ●●

●●●

● ●

●● ●

● ●

●● ●●

●●●

●●

●●

● ●●

●●

● ●●

● ●●

●●

● ●●

●●

●●

●●

●●●

●●

● ●

●●●

●●

●●●

●●

●●

●●

●● ●

●●

●●

●● ●

●●

● ●● ●

●●● ●

●●

● ● ●●

●●

● ●

●● ●

●●

●● ●

●●

●●● ●

●●

●●

●●

● ●

● ●●

●●

●●

●●●●

●●

●●

●●

●●

●● ●●

●●

●●●

●●● ●

●● ●

●●●

●●●

● ●●

●●●

●●

●● ●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

● ●

●● ●

●●●

●●

●●

●● ●

●●

●● ● ●●●

●●●

●●

●●

● ●●

●●

●●

●●

● ●●

● ●

● ●

● ●

●●

●●

●●●

●●

●●●

● ●

● ●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●●

●●

●●

●●

●● ●

●●●

●●

● ●

●● ●

●●

●●

● ●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

● ●

● ●

● ●

●●

●●●●

●●

● ●●

● ●

●●

● ●

●●

●●

●●

●●

●●

●●● ●

●●

●●

●● ●●

●●

●●

●●

●●

●●●

●●

●●

●●●

●●

●●

● ●

●●●

●●● ●

●●

● ●●

●●●

●●

●●

●●

●●

●● ●●

● ●●

● ●● ●

●●

●●●

●●

●●

●●

●●●

●●●

●●

●●

●●

●●

●●

●●

● ●

● ●●

●●

●●

● ●

● ●

●●

●●●●

●●●●

● ●

●●

●● ●

●●

●●

●●

●●

●● ●

● ●

●● ●

●● ●●●●

●●

●●

●●

●●

●●●

●●

●●

● ●●

●●

0 5 10 15 20

−4

−2

02

4M−A plot

A simulationIntensity

log2

Fol

d C

hang

e

140.776 Statistical Computing R: Graphics

Page 23: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Plot

Another example (draw lines instead of points):

> x<-seq(0, 2*pi, by=0.01)> y<-sin(x)> plot(x,y,type="l")

0 1 2 3 4 5 6

−1.

0−

0.5

0.0

0.5

1.0

x

y

140.776 Statistical Computing R: Graphics

Page 24: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

par()

You can also access and modify the list of graphics parameters for thecurrent graphics device using the function par():

par() returns a list of all graphics parameters.

par(c(“col”,“lty”)) returns only the named graphics parameters.

par(col=4,lty=2) sets the value of the named parameters, returnsthe old values as a list.

For example:

> par(c("col","lty"))$col[1] "black"$lty[1] "solid"> oldpar<-par(col=4,lty=2)> par(oldpar) ## restores the original setting

140.776 Statistical Computing R: Graphics

Page 25: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

par()

Differences between par() and setting parameters in plot() (andother high-level plotting functions):

Setting parameters using par() result in permanent changes ofthe values for the current graphics device.

Parameter values set in plot() etc. are only effective whenexecuting that particular command.

140.776 Statistical Computing R: Graphics

Page 26: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

par()

For example:

> x<-seq(0, 2*pi, by=0.01)> y<-sin(x)

> oldpar<-par(col="blue")> plot(x,y,type="l")> plot(x,y^2,type="l")> par(oldpar)

0 1 2 3 4 5 6

−1.

0−

0.5

0.0

0.5

1.0

x

y

0 1 2 3 4 5 6

0.0

0.2

0.4

0.6

0.8

1.0

x

y^2

140.776 Statistical Computing R: Graphics

Page 27: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

par()

> plot(x,y,type="l",col="blue")> plot(x,y^2,type="l")

0 1 2 3 4 5 6

−1.

0−

0.5

0.0

0.5

1.0

x

y

0 1 2 3 4 5 6

0.0

0.2

0.4

0.6

0.8

1.0

x

y^2

140.776 Statistical Computing R: Graphics

Page 28: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Types of plotting commands

plot(), hist(), etc. are high-level plotting functions. Sometimes,you want to add points or lines to an existing plot. To do this, youneed low-level plotting functions.

In general, there are three types of plotting commands:

High-level: create a new plot on the graphics device, withaxes, labels, titles etc.

Low-level: add information to an existing plot.

Interactive: interactively add or extract information to orfrom an existing plot using a pointing device (e.g. mouse)

140.776 Statistical Computing R: Graphics

Page 29: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Low-level plotting functions

Examples are:

points(): add points

lines(): add connected lines

text(): add texts

abline(): add straight lines

legend(): add legend

title(): add titles

. . .

140.776 Statistical Computing R: Graphics

Page 30: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Low-level plotting functions

> x<-seq(0,2*pi,by=0.5)> y<-sin(x)> z<-cos(x)> plot(x,y,type="o",col="blue",lwd=2,pch="s")

s

s

s

ss

s

s

s

s

s s

s

s

0 1 2 3 4 5 6

−1.

0−

0.5

0.0

0.5

1.0

x

y

140.776 Statistical Computing R: Graphics

Page 31: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Low-level plotting functions

> lines(x,z,type="o",col="red",lty=2,lwd=2,pch="c")

s

s

s

ss

s

s

s

s

s s

s

s

0 1 2 3 4 5 6

−1.

0−

0.5

0.0

0.5

1.0

x

yc

c

c

c

c

c

cc

c

c

c

c

c

140.776 Statistical Computing R: Graphics

Page 32: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Low-level plotting functions

> abline(h=0, lty=2)

s

s

s

ss

s

s

s

s

s s

s

s

0 1 2 3 4 5 6

−1.

0−

0.5

0.0

0.5

1.0

x

yc

c

c

c

c

c

cc

c

c

c

c

c

140.776 Statistical Computing R: Graphics

Page 33: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Low-level plotting functions

> text(3,0.5,"sin(x)=0")

s

s

s

ss

s

s

s

s

s s

s

s

0 1 2 3 4 5 6

−1.

0−

0.5

0.0

0.5

1.0

x

y

c

c

c

c

c

c

cc

c

c

c

c

c

sin(x)=0

140.776 Statistical Computing R: Graphics

Page 34: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Low-level plotting functions

> legend("bottomleft", cex=1.25,+legend = c("sin(x)", "cos(x)"), pch = c("s", "c"),+ col=c("blue","red"))

s

s

s

ss

s

s

s

s

s s

s

s

0 1 2 3 4 5 6

−1.

0−

0.5

0.0

0.5

1.0

x

y

c

c

c

c

c

c

cc

c

c

c

c

c

sin(x)=0

sc

sin(x)cos(x)

140.776 Statistical Computing R: Graphics

Page 35: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Interactive plotting functions

Try the locator() function:

> plot(x,y)> text(locator(1),"y=sin(x)")

140.776 Statistical Computing R: Graphics

Page 36: R: Graphicshji/courses/statcomputing/Graphics1.pdf · R: Graphics 140.776 Statistical Computing August 21, 2011 140.776 Statistical Computing R: Graphics

Exercise

−2 −1 0 1 2

−2

−1

01

2

Panda

140.776

Exe

rcis

e

Hello, my name is panda!

140.776 Statistical Computing R: Graphics