Saturday, October 27, 2007

Graphics

> dev.list(), dev.cur(), dev.set(2)
> dev.copy(3), dev.prev(), dev.next()
> dev.off, dev.off(3), graphics.off()

# Pairs plots
pairs(trees, pch=15) # pch: plotting character

# histogram with density
hist(ChickWeight$weight, probability=T)
lines(density(ChickWeight$weight))

# histogram & densityplot
attach(ChickWeight)
histogram(~weight | Diet)
densityplot(~weight | Diet)

# barplot
barplot(tapply(weight, Diet, mean, na.rm=T))

tt<-t(tapply(warpbreaks$breaks,warpbreaks[,2:3],mean))
barplot(tt,beside=T,legend=c("L", "M", "H"))

barplot(VADeaths)

ymean=c(1.25,2.65,3.45)
ysd=c(0.35,0.65,0.50)
xpos=barplot(ymean,ylim=c(0,max(ymean)+max(ysd)),col='yellow')
arrows(xpos,ymean-ysd,xpos,ymean+ysd)

# pie
pie(tapply(weight, Diet, mean, na.rm=T))

# bwplot
bwplot(weight~factor(Time), ChickWeight)
bwplot(weight~factor(Diet), ChickWeight)
bwplot(weight~factor(Time)|Diet, ChickWeight)

# X-Y plot
attach(Puromycin)
plot(rate~conc)
lines(lowess(conc,rate),col="blue")

# Extensions
plot(conc,rate,pch=as.numeric(state))
legend(3300,35,legend=c("treated","untreated"),pch=1:2)

# Coplots
coplot(rate~conc|state)

# Density plot
plot(density(rate))
qqnorm(rate)
qqline(rate)

hist(x)
hist(x, break=14) // # of classes is 14
hist(x, break=seq(60, 140, by=8), col =2) // color =2
hist(x, break=c(70,80,90,100,140), xlab="X label", ylab="Y label", main="Title", ylim=c(0,0.05)) // ylim

stem(x)
stem(x, scale=2) // double # of stems

qqplot(x, y)
qqnorm(x); qqline(x)
ppoints(x) - plot( qt(ppoints(x),9), sort(x) )

# boxplot
male <- c(6,0,2,1,2,4.5,8,3,17,4.5,4,5)
female <- c(5,13,3,2,6,14,3,1,1.5,1.5,3,8,4)
boxplot(list(male=male,female=female))
hours <- c(male,female)
sex <- c(rep("male",12), rep("female",13))
x <- data.frame(hours=hours, sex=sex)
attach(x) // add the names of the variables to the search path
boxplot(split(hours, sex))

# 2 by 2 pictures
par(mfrow=c(2,2), pch=16)

# back to 1
par(mfrow=c(1,1), pch=1)


### plots
x <- (1:100)/10
plot(x, x^3-13*x^2+39*x)
plot(x, x^3-13*x^2+39*x, type='l') # line
plot(x, x^3-13*x^2+39*x, type='l', xlab='Time', ylab='Intensity') # label

# mathematical symbols
plot(x, x^3-13*x^2+39*x, type='l', xlab='Time', ylab=expression(Intensity==x^3-13*x^2+38*x))
plot(x, pi*x^2, type='l', xlab="Radius", ylab=expression(Area==pi*r^2))

# add points, arrows, text and lines to existing plots
points(2, 34, col='red', pch=16, cex=2)
arrows(4, 50, 2.2, 34.5)
text(4.15, 50, 'local max', adj=0, col='blue', cex=1.5)
lines(x, 30-50*sin(x/2), col='blue')

# add legend
legend(0, 80, c('poly', 'sine'), col=c('black', 'blue'), lwd=2)

# change options before plotting
windows(width=8, height=5, pointsize=14)
par(mai=c(1,1,0.1,0.1), lwd=3)

No comments: