<<

Solutions to Selected Exercises for Braun and Murdoch’s A First Course in Statistical Programming with R

Kristy Alexander, Yiwen Diao, Qiang Fu, and Yu Han W. John Braun and Duncan J. Murdoch

November 1, 2007 Chapter 3

Programming Statistical Graphics

3.1 High level plots

1. (a) > hist()

Histogram of islands 40 30 20 Frequency 10 0

0 5000 10000 15000

islands

(c) > par(mfrow=c(1,2)) > hist(islands,breaks="Sturges",main="Sturges") > hist(islands,breaks="Scott",main="Scott")

6 Sturges Scott 40 40 30 30 20 20 Frequency Frequency 10 10 0 0

0 5000 10000 15000 0 5000 10000 15000 20000

islands islands

> par(mfrow=c(1,2)) > hist(log(islands),breaks="Sturges",main="Sturges") > hist(log(islands),breaks="Scott",main="Scott")

Sturges Scott 30 15 25 20 10 15 Frequency Frequency 10 5 5 0 0

2 4 6 8 10 2 4 6 8 10

log(islands) log(islands)

(e) > par(mfrow=c(1,2)) > dotchart(islands,main="islands") > dotchart(log(islands),main="log(islands)")

7 islands log(islands)

Victoria ● Victoria ● Vancouver ● Vancouver ● Timor ● Timor ● Tierra del Fuego ● Tierra del Fuego ● ● Tasmania ● Taiwan ● Taiwan ● ● Sumatra ● ● Spitsbergen ● Southampton ● Southampton ● ● South America ● ● Sakhalin ● Prince of Wales ● Prince of Wales ● Novaya Zemlya ● Novaya Zemlya ● ● North America ● ● Newfoundland ● New Zealand (S) ● New Zealand (S) ● New Zealand (N) ● New Zealand (N) ● ● New Guinea ● ● New Britain ● Moluccas ● Moluccas ● ● Mindanao ● Melville ● Melville ● ● Madagascar ● ● Luzon ● Kyushu ● Kyushu ● ● Java ● ● Ireland ● Iceland ● Iceland ● ● Honshu ● ● Hokkaido ● ● Hispaniola ● ● Hainan ● ● Greenland ● ● Europe ● Ellesmere ● Ellesmere ● Devon ● Devon ● Cuba ● Cuba ● Celon ● Celon ● Celebes ● Celebes ● Britain ● Britain ● ● Borneo ● Banks ● Banks ● Baffin ● Baffin ● Axel Heiberg ● Axel Heiberg ● ● Australia ● ● Asia ● Antarctica ● Antarctica ● ● Africa ●

0 5000 10000 15000 4 6 8 10

3. (a) > plot(pressure$temperature,pressure$pressure,xlab="temperature",ylab="pressure")

● 800

600 ●

400 ● pressure

200 ●

● ● ● ● ● ● ● ● ● ● ● ● ● ● 0

0 50 100 150 200 250 300 350

temperature

(c) > plot(pressure$temperature,pressure$pressure^(3/20),xlab="temperature",ylab="pressure")

8 ●

2.5 ●

● 2.0 ●

1.5 ● pressure ●

● 1.0 ● ● ● ● 0.5 ● ●

0 50 100 150 200 250 300 350

temperature

Chapter Exercises

1. (a) > hist(log(islands,10),breaks="Scott",axes=FALSE,xlab="area",main="Histogram of Areas") > axis(1,at=1:5,labels=10^(1:5)) > axis(2) > box()

Histogram of Island Areas 35 30 25 20 15 Frequency 10 5 0

10 100 1000 10000 1e+05

area

(c) > hist(round(log(islands,10)),breaks="Sturges",axes=FALSE,xlab="area", + main="Histogram of Island Areas") > axis(1,at=1:5,labels=10^(1:5)) > axis(2) > box()

9 Histogram of Island Areas 20 15 10 Frequency 5 0

10 100 1000 10000

area

10