library(DAAG) data(ais) help(ais) is.data.frame(ais) plot(ais) head(ais) names(ais) attach(ais) ################## star plots ######################################### x <- ais[,"bmi"] y <- ais[,"pcBfat"] plot(x,y,type="n",xlab="Body mass index",ylab="Percent body fat") ########### ray length corresponds to variables in star. Starts at 3 o'clock, goes counter-clockwise sport.name <- levels(sport) n <- length(sport.name) for (j in 1:n) { I <- sport==sport.name[j] x <- ais[I,"bmi"] y <- ais[I,"pcBfat"] star <- matrix(unlist(ais[I,1:5]),ncol=5) symbols(x,y,stars=star,add=TRUE,fg=j) } legend(x="topleft",legend=sport.name,pch=16,col=1:n) x <- ais[,"bmi"] y <- ais[,"pcBfat"] n <- length(x) plot(x,y,type="n",xlab="Body mass index",ylab="Percent body fat") ########################## convex hull ######################### genders <- levels(sex) n <- length(genders) for (j in 1:n) { I <- sex==genders[j] x <- ais[I,"bmi"] y <- ais[I,"pcBfat"] star <- matrix(unlist(ais[I,1:5]),ncol=5) symbols(x,y,stars=star,add=TRUE,fg=j) hull <-chull(x,y) polygon(x[hull],y[hull],border=j) } legend(x="topleft",legend=genders,pch=16,col=1:n) ################## conditioning plots library(lattice) xyplot(bmi~ssf|sport,groups=sex,pch=16,col=c(2,3),data=ais,xlab="Skin-fold",ylab="Body mass") legend(x="topleft",legend=c("Females","Males"),pch=16,col=c(2,3)) ########################## 3-D perspective ######################### library(scatterplot3d) Weight <- ais$wt Height <- ais$ht BMI <- ais$bmi ### kg/m^2 HG <- ais$hg scatterplot3d(Weight,Height,BMI,type="h") coplot(BMI~Weight|Height) coplot(BMI~Height|Weight) RedCells <- ais$rcc scatterplot3d(RedCells,BMI,HG,type="h") coplot(HG~RedCells|BMI) ####################### box plots ########################### library(mlbench) data(Ozone) help(Ozone) Ozone <- na.omit(Ozone) names(Ozone) <- c("month","daym","dayw","ozone","pressure","windspeed", "humidity","tempS","tempEM","inversion","grad","inverstemp","visibility") ozone <- Ozone$ozone boxplot(ozone~Ozone$month,ylab="Ozone ",xlab="Month") library(lattice) stripplot(ozone~Ozone$month,ylab="Ozone ",xlab="Month",pch=16,col=1) bwplot(ozone~Ozone$month,ylab="Ozone ",xlab="Month",col=1) coplot(Ozone$ozone~Ozone$tempS|Ozone$windspeed) coplot(Ozone$ozone~Ozone$tempS|Ozone$tempEM, panel=function(x,y,col,pch)panel.smooth(x,y,span=1)) ########################### lowess smooth ################# n <- length(ozone) cor(ozone[1:(n-1)],ozone[2:n]) acf(ts(ozone)) # plot of auto-correlation function plot(ozone[1:(n-1)],ozone[2:n],xlab="Ozone at time t",ylab="Ozone at time t+1") lines(lowess(ozone[2:n],ozone[1:(n-1)],f=2/3)) lines(lowess(ozone[2:n],ozone[1:(n-1)],f=1/3)) lines(lowess(ozone[2:n],ozone[1:(n-1)],f=1/6)) par(mfrow=c(2,1)) symbols(ozone[1:(n-1)],ozone[2:n],circles = Ozone$inversion[1:(n-1)],inches=0.15,xlab="Ozone, day t", ylab="Ozone at time t+1",main="Circles are inversion height, day t") symbols(ozone[1:(n-1)],ozone[2:n],circles = Ozone$inversion[2:n],inches=0.15,xlab="Ozone, day t", ylab="Ozone at time t+1",main="Circles are inversion height, day t+1") symbols(ozone[1:(n-1)],ozone[2:n],circles = Ozone$inversion[2:n],inches=0.15,xlab="Ozone, day t", ylab="Ozone at time t+1",main="Circles are inversion height, day t+1") invers <- max(Ozone$inversion[2:n])-Ozone$inversion[2:n] symbols(ozone[1:(n-1)],ozone[2:n],circles = invers[2:n],inches=0.15,xlab="Ozone, day t", ylab="Ozone at time t+1",main="Circles are inversion depth, day t+1") par(mfrow=c(1,1)) lm.obj <- lm(ozone[2:n]~ozone[1:(n-1)]) summary(lm.obj) residuals <- lm.obj$residuals - min(lm.obj$residuals) symbols(Ozone$tempS[2:n],ozone[2:n],circles = Ozone$inversion[2:n],inches=0.15,xlab="Temperature at Sandburg", ylab="Detrended Ozone ar t+1",main="Circles are inversion height, day t+1",cex.main=0.9) ################# Count data ################### # Outcomes from open and ultrasound surgery of kidney stones stones <- array(c(81,6,234,36,192,71,55,25),dim=c(2,2,2), dimnames=list(Success=c("yes","no"),Method=c("open","ultrasound"), Size=c("<2cm",">2cm"))) stones mosaicplot(stones,sort=3:1)