Advanced Graphing

advertisement
##################################
# Advanced graphing commands
##################################
trillium <read.table("http://users.humboldt.edu/rizzardi/Data.dir/trillium",header=T,skip=9)
attach( trillium )
2
4
6
8
# Some detailed control over graphs
par(mfrow=c(1,1))
boxplot( stem ~ site, range=0 )
# range=0 has whiskers stretch to extremes
# default is range=1.5, whiskers out 1.5 IQR from box
1
2
3
4
dev.new()
boxplot( stem ~ site, xlab="site",ylab="stem length (cm)" )
axis( side=4, at=(2.54*(1:3)), labels=1:3, tck=0.01, mgp=c(-1,0,-0.1))
# help( axis )
# help( par )
# tck is par command for tick lengths and direction
# mgp is par command to determine axis title, labels, and line location
mtext("inches",side=4,line=.7,font=2, col="magenta")
# help( mtext )
# font=2 is bold font, line is location of margin text
str(colors())
chr [1:657] "white" "aliceblue" "antiquewhite" "antiquewhite1" ...colors()
1
3
8
2
inches
6
2
1
4
stem length (cm)
1
2
3
4
site
1
2
3
Stem length (cm)
4
5
6
7
8
9
dev.new()
boxplot( stem ~ site, axes=F )
axis( side=1, at=1:4,labels=1:4, tck=0 , lty=0)
axis( side=2, at=1:9,labels=1:9 )
mtext("Site",side=1)
mtext("Stem length (cm)", side=2, line=2.2 )
axis( side=2, at=c(2,4,6,8),tck=1, lty=3, lwd=.2)
# tck=1 draws grid lines across graph, see help(par)
Site
1
2
3
4
2
dev.new()
# Controlling multiple graphs on a page without mfrow()
layout( rbind( c(1,2), c(3,4) ), height=c(1,2), width=c(1.5,1))
hist(stem)
hist(leaf)
plot(leaf, stem, pty="s"); abline( lm( stem ~ leaf ) )
title( main=expression( beta[0] + beta[1]*leaf ) )
plot(stem, leaf); fit <- lm( leaf ~ stem )
abline( fit )
title( main=substitute( paste(beta[0]==beta0," and ",beta[1]==beta1),
list(beta0=signif(fit$coef[1],3),beta1=signif(fit$coef[2],3)) ) )
0
2
4
6
150
0
150
Frequency
Histogram of leaf
0
Frequency
Histogram of stem
8
5
10
stem
20
leaf
1leaf
0
7.42 and
1
1.16
5
2
10
4
leaf
stem
15
6
8
20
0
15
5
10
15
leaf
20
2
4
6
8
stem
3
dev.new()
layout( rbind( c(1,1,2,2),c(0,3,3,0) ), width=c(1,2,2,1), height=c(3,4))
pie( table( flower) )
boxplot( stem~flower )
plot(leaf,stem, xaxt="n" ) # no x axis labels
title("Not a true Stem-and-leaf plot" )
layout( 1 )
mtext(side=3,"Three graphs", line=2.3, cex=1.5 )
Three graphs
8
p
2
4
6
s
w
p
s
w
2
4
stem
6
8
Not a true Stem-and-leaf plot
leaf
4
x <- seq(from=-pi,to=pi,length=10)
y <- seq(from=0,to=(2*pi),length=10)
xy.grid <- expand.grid(x=x,y=y)
dim(xy.grid)
z <- sin(xy.grid[,1])+ cos(xy.grid[,2])
zmat <- matrix(z,ncol=10,byrow=F)
dev.new()
layout(rbind(c(1,2),c(3,4)))
contour(x,y,zmat)
persp(x,y,zmat)
x <- seq(from=-pi,to=pi,length=50)
y <- seq(from=0,to=(2*pi),length=50)
xy.grid <- expand.grid(x=x,y=y)
dim(xy.grid)
z <- sin(xy.grid[,1])+ cos(xy.grid[,2])
zmat <- matrix(z,ncol=50,byrow=F)
contour(x,y,zmat,nlevels=15)
persp(x,y,zmat, theta=40, phi=40, r=4)
# theta and phi control angle of view
# r controls distance from box
5
1
3 4 5 6
1.5
0
0.5
-1.5
y
0 1 2
z ma t
-1
0
1
-0.5
5
0.
x
-2
-1
3
0
-1.4
-0.2
0.2
.2
-1
0.6
8
0.
0
-2
-1
0
0.4
1.4
1
x
6
1.
1
z ma t
0.2
-0.8
0.8
0.4
-0.8
-0.6
0 1 2
2
1.4
0.6
-1.8
-1.6
-3
1
2
y
0.
4
-0.
4
-0.6 -1
0.
2
0
1
3 4 5 6
-3
3
dev.new()
layout(1)
persp(x,y,zmat, theta=40, phi=40, r=4)
dev.new()
layout(c(1,2),respect=T) # respect=T forces aspect ratio to be the same
image(x,y,zmat)
plot( runif(100), runif(100) )
y
zmat
x
6
###########################
# Trellis plots
# Go online and get package "lattice"
# commercial package S-Plus beat R to the library name "trellis"
library( lattice )
candy <read.table("http://users.humboldt.edu/rizzardi/Data.dir/jollyrancher.TXT",header=T)
attach(candy)
Change <- After - Before
xyplot( After ~ Before | Treatment )
# obvious correlation
nothing
102
101
100
99
98
After
97
cool
hot
102
101
100
99
98
97
96
98
100
Before
xyplot( Change ~ Before | Treatment ) # colder people heat up more
nothing
4
3
2
1
Change
0
-1
cool
hot
4
3
2
1
0
-1
96
98
100
Before
7
cloud( Change ~ After * Before | Treatment ) # hard to interpret
nothing
Change
Before
After
cool
hot
Change
Change
Before
Before
After
After
coplot( After~Before | Change, panel=panel.smooth )
Given : Change
0
96
97
98
2
99 100
3
95
96
4
97
98
99 100
97 98 99
101
After
97 98 99
101
95
1
95
96
97
98
99 100
Before
8
# condition on one continuous variable and one categorical
coplot( After ~ Before | Change*Treatment, panel=panel.smooth)
Given : Change
0
2
95 97 99
3
4
95 97 99
97 98 99
Given : Treatment
hot
cool
101
97 98 99
After
101
97 98 99
nothing
101
95 97 99
1
95 97 99
95 97 99
95 97 99
Before
9
##############
## Bird data
############
birds <read.table("http://users.humboldt.edu/rizzardi/Data.dir/bird.txt", skip=15,
header=T)
names( birds )
attach(birds)
coplot( wt ~ tl | kl, panel=panel.smooth )
Given : kl
0.80
158
162
0.90
166
154
158
162
166
24
26
28
30
wt
24
26
28
30
154
0.85
154
158
162
166
tl
# condition on two continuous variables
coplot( wt ~ tl | kl * bh, panel=panel.smooth)
Given : kl
0.80
160
166
154
160
0.90
166
154
160
166
32.5
31.5
Given : bh
30.5
24
30.0
28
24
28
24
31.0
28
wt
24
28
24
32.0
28
24
28
33.0
154
0.85
154
160
166
154
160
166
154
160
166
tl
10
#install ggplot2 package and load library(ggplot2)
# See http://rstudio-pubs-static.s3.amazonaws.com/
# 2176_75884214fc524dc0bc2a140573da38bb.html
> str(diamonds)
'data.frame':
53940 obs. of 10 variables:
$ carat : num 0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
$ cut
: Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
$ color : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ...
$ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ...
$ depth : num 61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ...
$ table : num 55 61 65 58 58 57 57 55 61 61 ...
$ price : int 326 326 327 334 335 336 336 337 337 338 ...
$ x
: num 3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ...
$ y
: num 3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ...
$ z
: num 2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
> head(diamonds)
carat
cut color clarity depth table price
x
y
z
1 0.23
Ideal
E
SI2 61.5
55
326 3.95 3.98 2.43
2 0.21
Premium
E
SI1 59.8
61
326 3.89 3.84 2.31
3 0.23
Good
E
VS1 56.9
65
327 4.05 4.07 2.31
4 0.29
Premium
I
VS2 62.4
58
334 4.20 4.23 2.63
5 0.31
Good
J
SI2 63.3
58
335 4.34 4.35 2.75
6 0.24 Very Good
J
VVS2 62.8
57
336 3.94 3.96 2.48
> dev.new()
> qplot(carat, price, colour=clarity, alpha=I(1/3), size=depth, data=diamonds)
> dev.new()
> qplot(x=color,y=price,geom="boxplot",data=diamonds)
> dev.new()
> qplot(x=carat,y=price,colour=clarity,facets=~cut,data=diamonds)
> dev.new()
> qplot(x=carat,y=price,colour=clarity,facets=color~cut,data=diamonds)
11
12
13
14
##################################
# Advanced graphing commands
##################################
trillium <read.table("http://users.humboldt.edu/rizzardi/Data.dir/trillium",header=T,sk
ip=9)
# Some detailed control over graphs
par(mfrow=c(1,1))
boxplot( stem ~ site, range=0 )
# range=0 has whiskers stretch to extremes
# default is range=1.5, whiskers out 1.5 IQR from box
dev.new()
boxplot( stem ~ site, xlab="site",ylab="stem length (cm)" )
axis( side=4, at=(2.54*(1:3)), labels=1:3, tck=0.01, mgp=c(-1,0,-0.1))
# help( axis )
# help( par )
# tck is par command for tick lengths and direction
# mgp is par command to determine axis title, labels, and line location
15
mtext("inches",side=4,line=.7,font=2, col="magenta")
# help( mtext )
# font=2 is bold font, line is location of margin text
colors()
dev.new()
boxplot( stem ~ site, axes=F )
axis( side=1, at=1:4,labels=1:4, tck=0 , lty=0)
axis( side=2, at=1:9,labels=1:9 )
mtext("Site",side=1)
mtext("Stem length (cm)", side=2, line=2.2 )
axis( side=2, at=c(2,4,6,8),tck=1, lty=3, lwd=.2)
# tck=1 draws grid lines across graph, see help(par)
dev.new()
# Controlling multiple graphs on a page without mfrow()
layout( rbind( c(1,2), c(3,4) ), height=c(1,2), width=c(1.5,1))
hist(stem)
hist(leaf)
plot(leaf, stem, pty="s"); abline( lm( stem ~ leaf ) )
title( main=expression( beta[0] + beta[1]*leaf ) )
plot(stem, leaf); fit <- lm( leaf ~ stem )
abline( fit )
title( main=substitute( paste(beta[0]==beta0," and ",beta[1]==beta1),
list(beta0=signif(fit$coef[1],3),beta1=signif(fit$coef[2],3)) ) )
dev.new()
layout( rbind( c(1,1,2,2),c(0,3,3,0) ), width=c(1,2,2,1), height=c(3,4))
pie( table( flower) )
boxplot( stem~flower )
plot(leaf,stem, xaxt="n" ) # no x axis labels
title("Not a true Stem-and-leaf plot" )
layout( 1 )
mtext(side=3,"Three graphs", line=2.3, cex=1.5 )
x <- seq(from=-pi,to=pi,length=10)
y <- seq(from=0,to=(2*pi),length=10)
xy.grid <- expand.grid(x=x,y=y)
dim(xy.grid)
z <- sin(xy.grid[,1])+ cos(xy.grid[,2])
zmat <- matrix(z,ncol=10,byrow=F)
dev.new()
layout(rbind(c(1,2),c(3,4)))
contour(x,y,zmat)
persp(x,y,zmat)
x <- seq(from=-pi,to=pi,length=50)
y <- seq(from=0,to=(2*pi),length=50)
xy.grid <- expand.grid(x=x,y=y)
dim(xy.grid)
z <- sin(xy.grid[,1])+ cos(xy.grid[,2])
zmat <- matrix(z,ncol=50,byrow=F)
contour(x,y,zmat,nlevels=15)
persp(x,y,zmat, theta=40, phi=40, r=4)
16
# theta and phi control angle of view
# r controls distance from box
dev.new()
layout(1)
persp(x,y,zmat, theta=40, phi=40, r=4)
dev.new()
layout(c(1,2),respect=T) # respect=T forces aspect ratio to be the same
image(x,y,zmat)
plot( runif(100), runif(100) )
###########################
# Trellis plots
# Go online and get package "lattice"
# commercial package S-Plus beat R to the library name "trellis"
library( lattice )
candy <read.table("http://users.humboldt.edu/rizzardi/Data.dir/jollyrancher.TXT",hea
der=T)
attach(candy)
Change <- After - Before
dev.new()
xyplot( After ~ Before | Treatment ) # obvious correlation
dev.new()
xyplot( Change ~ Before | Treatment ) # colder people heat up more
dev.new()
cloud( Change ~ After * Before | Treatment ) # hard to interpret
dev.new()
coplot( After~Before | Change, panel=panel.smooth )
# condition on one continuous variable and one categorical
dev.new()
coplot( After ~ Before | Change*Treatment, panel=panel.smooth)
##############
## Bird data
############
birds <read.table("http://users.humboldt.edu/rizzardi/Data.dir/bird.txt",
skip=15, header=T)
names( birds )
attach(birds)
dev.new()
coplot( wt ~ tl | kl, panel=panel.smooth )
# condition on two continuous variables
dev.new()
coplot( wt ~ tl | kl * bh, panel=panel.smooth)
#install ggplot2 package and load library(ggplot2)
# See http://rstudio-pubs-static.s3.amazonaws.com/
# 2176_75884214fc524dc0bc2a140573da38bb.html
str(diamonds)
17
head(diamonds)
dev.new()
qplot(carat, price, colour=clarity, alpha=I(1/3), size=depth, data=diamonds)
dev.new()
qplot(x=color,y=price,geom="boxplot",data=diamonds)
dev.new()
qplot(x=carat,y=price,colour=clarity,facets=~cut,data=diamonds)
dev.new()
qplot(x=carat,y=price,colour=clarity,facets=color~cut,data=diamonds)
18
Download